MacOS – Run sudo From Non-Admin Account

administratormacospermissionterminal

Okay, so I'm gearing up to get a new machine so I'm looking at how I can organise things better. Now, currently I use an admin account as my main login account, but I know that that's probably not best practice, so I'd like to switch to a separate admin and non-admin account on my machine.

However, I'm a developer, and I do a lot of work in the terminal, so sometimes I do need to run commands via sudo, but of course that won't work the way I would normally run it (sudo foo, type current account's password). What I'd like to know is, knowing the details for an admin account on my machine, is it possible to run commands via sudo (or similar) from a non-admin account, without having to login to a different account each time? For example, could I trigger the enter admin password dialogue somehow?

Best Answer

Seems like you could use the SU command to switch to another user in Terminal. Once you've switched to the admin user you can use sudo commands coupled with the admin account's password.

For example, when I'm at one of my end user's Macs and I need to run something with elevated privileges (chown for example) without logging out and into the admin account (ladmin) I would use something like this (in bold):

Mr-Rabbits-Mac:~ notadminuser$ su ladmin

This will prompt for the ladmin account's password, once entered you will see a bash command prompt. From here I can run any sudo commands desired, entering ladmin's password as needed...

bash-3.2$ sudo chown user2 /some/folder

This will run the chown command using the now logged in ladmin user, prompting for the ladmin account's password. Once finished you can type exit to return to your logged in user's prompt.

Screenshot of similar process, only using sudo to run the ls command (just for example). enter image description here