13

I have a lua script, running on the Mac, that needs to call sudo.

I'd hoped that Mac OS would automatically bring up a password request dialog, but instead it the command fails by returning 256.

Is there anyway that I can achieve my goal?

Tim

tarmes
  • 15,366
  • 10
  • 53
  • 87
  • Possible duplicate of [Is there any graphical "sudo" for Mac OS X?](https://stackoverflow.com/questions/1517183/is-there-any-graphical-sudo-for-mac-os-x) – tripleee Jun 11 '19 at 11:45

1 Answers1

23

Quick and easy way: run it like this

/usr/bin/osascript -e 'do shell script "/path/to/myscript args 2>&1 etc" with administrator privileges'

Proper and configurable way: use AuthorizationExecuteWithPrivileges API from Authorization Services (in Security.framework).

Both will display standard Mac OS X GUI asking for administrator password and then execute the command as root, the same way as sudo does except that SUDO_USER environment variables will not be set.

If you need to execute individual commands from under user account when you're already elevated to root, you can prepend them with /usr/bin/sudo -u $USER.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
  • Hey @hamstergene by the last sentence you mean like this? `osascript -e 'do shell script "sudo -u $USER /path/to/myscript args 2>&1 etc" with administrator privileges'` Because this does not work on my end. I get this: `execution error: sudo: a terminal is required to read the password; either use thsudo: a password is required (1)d input or configure an askpass helper` – Ickata Aug 26 '22 at 06:47