29

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?

2
  • im not sure if this would work with osx, but if seen solutions on linux systems (e.g. ubuntu) where the user is allowed to run 'su' via 'sudo'. by this way you can just type 'sudo su', enter your users password and you end up with a root shell. i love this solution because no seperate admin account and password is required
    – weberik
    Commented Apr 3, 2014 at 15:28
  • I think it's worth mentioning that on UNIX like systems, if you are the administrator, it's perfectly fine to be running in a sudoer account. The equivalent of Windows-Xp-esque admin all the time would be being logged into root.
    – Linuxios
    Commented Apr 3, 2014 at 22:44

4 Answers 4

36

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

2
  • 1
    Aha! This seems like exactly what I need, thanks! No need to add to sudoers file, I had a feeling there was a way to do it, just couldn't for the life of me think of it, thanks for the great answer!
    – Haravikk
    Commented Apr 3, 2014 at 16:54
  • 1
    A clarification: if the need is to install SW packages for the non-admin user, does su ensures that all the environment variables of the non-admin are correctly set etc.?
    – geguze
    Commented Aug 16, 2022 at 17:32
10

In Terminal type login, a space, your admin name and hit return. Then type your admin password, hit return and you are logged in as administrator that can issue sudo commands with no additional configuration or files to change.

1
  • Perfect, worked nicely
    – Pierre
    Commented Mar 11, 2022 at 2:08
7

Add your existing user name to the sudoers file, and what you need to do is use the visudo command and add lines like this:

User privilege specification

root ALL=(ALL) ALL

<randomuser> ALL=(ALL) ALL

%admin ALL=(ALL) ALL

Or whatever perms you want to give yourself. then you can run sudo whatever to your hearts extent. That's /etc/sudoers by the way.

1
  • What would the differences be between this user account and an admin account? Can this user perform anything?
    – Rafs
    Commented Jan 24, 2023 at 10:09
-4

Go to the Directory Utility, unlock it and then from the Edit menu option select "Enable Root User". Type in a unique password for the root account and re-enter it for verification. Now with that done from any account you can open the Terminal.app from you can run "login" and enter root at the login and then enter the root password you just set up. You should be good to go! ;)

3
  • 9
    There is no need to enable the root user for this.
    – Édouard
    Commented Apr 3, 2014 at 14:29
  • So i could go into any Mac which is booted, open Terminal.app and enter the login command. Enter root and then what as a password? Please enlighten me :/
    – Andrew U.
    Commented Apr 4, 2014 at 10:19
  • 3
    I mean you don’t need to enable the root user to do what the OP is asking. Actually, if you go and use login, you might as well log into an admin account instead of root.
    – Édouard
    Commented Apr 4, 2014 at 10:29

You must log in to answer this question.

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