3

Recently I have had to make some changes on my Macbook that required me to not only have just sudo privileges but root privileges. When I searched how to do this the answer which seemed most appropriate for me was to run the commands I needed to run with root privileges in AppleScript using "with administrator privileges" after the shell script I wanted to run. This accomplished what I needed to do, but in general I would prefer not to have to use AppleScript when I want to run a few commands in the shell.

Therefore, I was wondering what the shell version of "with administrator privileges" is that would work for my mac. I am looking for something that would provide me with not only sudo privileges but root privileges as well. I would prefer it if I could do this in a way that was "sh-compatible" so it would work on zsh, bash, etc. But, I would also accept a solution that only works with zsh (my interactive shell of choice) if there is not a good "sh-compatible" solution for me.

I understand that I can run AppleScripts in the terminal using osascript in a similar vein as the solution provided in How to open a shell script in a new Terminal window and run it with administrator privileges but I was hoping for a more "pure sh" solution if possible.

2 Answers 2

2

sudo is the command-line equivalent of AppleScript's with administrator privileges. That is, when you run sudo somecommand, then somecommand will run as root. Here's a quick demo:

$ sudo whoami
Password: [I entered my password here]
root

One thing you have to be aware of, though, is that sudo only runs the actual command as root, not things like redirections. So if you use something like this:

sudo somecommand <inputfile >outputfile

...opening inputfile and outputfile will be done by your current shell, as the current user, before sudo and the command run. There are standard ways to get around this, like running a root shell to do the redirections and run the command:

sudo sh -c 'somecommand <inputfile >outputfile'

(Warning: if the command you want to run contains single-quotes and/or any shell variables, things are more complicated.) Another standard trick is to use sudo cat to read files as root and/or sudo tee to write files as root:

sudo cat inputfile | somecommand | sudo tee outputfile >/dev/null

(Note that in this example, cat and tee are run as root, but somecommand runs as your normal user.)

You can also use sudo -s to open an interactive root shell, and then run a bunch of commands as root (and then use exit to close that shell and go back to normal). But be careful when running as root; it's fairly easy to make a complete mess if you don't know exactly what you're doing.

1

Apple never had the windows security model of admin scripts so there is less effectiveness of the similar command in my experience. Your research was good, but the effect is underwhelming.

Since unix and Apple defense is layered and more about access control lists, you may need to attack each specific problem and apply the appropriate user or change needed. Instead of group policy and “as admin” Apple implemented MDM commands and the concept of a managed system.

However, if you are lucky your problem may be amenable to “as admin”. The clearest official documentation is no longer maintained documentation on running a shell script as an administrator.

  • do shell script "command" with administrator privileges
do shell script "command" with administrator privileges

For a pure shell experience, sudo in all its glory is the way forward as that works with all shells. The only down side would be objective-c bindings and native GUI script ability that Automator and AppleScript allow.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .