I have an executable that needs to run every time a user logs in. It needs to run in the context of that user, but with root permissions, as if the user had run it with sudo.

I have looked through Apple's documentation as well as the website launchd.info for a way to do this, but haven't been able to find anything.

If I create a Launch Agent (in ~/Library/LaunchAgents or /Library/LaunchAgents), or a Launch Daemon with the UserName property set, it runs as the user (as desired), but I cannot do anything that requires elevated privileges.

If I create a Launch Daemon (in /Library/LaunchDaemons) it runs as root (with the desired privileges), but without any user context. If I use su <username> -c "<command>" or sudo -u <username> <command> from the Launch Daemon, I can then run a command as the user, but it runs without sudo privileges.

Is there a way to run a launch agent with sudo permissions?

  • a few suggestions: this .plist generator suggests that you can specify the username. You could try <key>UserName</key> <string>root</string> (I haven't). Alternatively, you could run as root from within the script, as suggested in various ways here: askubuntu.com/questions/425754/… – anon01 Jun 27 '17 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.