7

I have some problems understanding sudo. I am logged in on a terminal as an non-admin/non-root user. This "normal" user is not in the sudoers file (and shouldnt be, in my opinion).

Now I try to execute a command that needs admin/root privileges and also access to directories of my normal user – therefore I am not able to simply su into an admin or root user.

In my understanding sudo -u root should do the trick – however it doesn't accept the password for root (or admin if I try with my normal admin user). It only accepts the password of the "normal" user which seems to indicate that the -u username option doesn't work the way I expect it to work.

My expectation is that sudo -u root some_command executes some_command with the privileges of root and therefore it asks also for the password of root. Obviously not.

TL;DR: How do I execute any command that requires admin privileges AND has access to the files of the "logged in (normal) user" without adding the normal user to the sudoers file?

I have enabled the root user under Mac OS X 10.7.

4 Answers 4

13

sudo always requires the executing user's password (and requires that you have specific permissions to do this, i.e. are one of the sudoers).

su requires the password of the target account (root by default, but root account has no password on OS X by default). If you use su instead, you can enter the destination account's password and execute a command using that user's privileges.

su -c some_cmd # as root
su username -c some_cmd # as username

This works by passing all arguments after the user name to the destination account's login shell. Shells usually support -c <commands> arguments. In GNU coreutils su, there's an actual -c command argument to su that can be placed before the user name.


You can su to another user account (using the other user account's password), and sudo from there, provided that other account is a sudoer.

If you want neither to enter another account's password, nor give your regular account sudoers permissions, you're pretty much out of options unless you consider SSH with key authentication or something like that.

8
  • Hi, thanks for the answer. I am not sure if I do understand you correct. As far as I can tell if I su into root I will loose access to the files of the initially logged in user - however this is exactly what I need. Let me be more concrete: I am using npm (node package manager) to install a self written package into the global package registry on my machine. For this to do i need access to "/usr/local/lib/node_modules/..." which the loggedin user (let say "robert") doesnt have. The package resides in "~/my_package". su doesnt help as it has no access to this folder (next comment)
    – robertj
    Sep 13, 2011 at 21:29
  • and adding "robert" to the sudoers file seems like total overkill to me.
    – robertj
    Sep 13, 2011 at 21:31
  • @robertj Root can access all users' files. So if you su without specifying a user, you're good.
    – Daniel Beck
    Sep 14, 2011 at 8:03
  • I'm running OS X 10.8.3 and 'su -c' is not recognized as a valid option for me...: "su: illegal option -- c"
    – Motsel
    May 23, 2013 at 7:23
  • @Daps0l Fixed, try the changed command.
    – Daniel Beck
    May 23, 2013 at 17:40
6

Unless you really "need" to maintain a separate root user on Mac OS X, it's often much easier to just get a root shell using sudo -s

In your case, non-admin users are not included in /etc/sudoers - so you would have to add by hand the users/groups you intend to have sudo accept.

# User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL

Once that's done, sudo will inherits almost all of the nice things from your shell like PATH and other variables, but elevates your non-admin user instantly to root. The downside of enabling root is time and the security risk of someone actually logging in locally or remotely as root.

Keep in mind, sudo is asking for the password of the user that logged in to the loginwindow screen. su - wants the password of the root account, sudo -s uses privilege escalation to use the current user's password to become root without needing any (or in your case, the actual) root password.

2

If you are using a standard account (usually for security reasons) and you know an administrator username and password, you can do su -l admin_user. After entering the password, you are now, for all effects, acting as that administrator. Then you can sudo to your heart's content. Now, just logout to go back to your original plain account.

0

my terminal executable cannot achieve what it will itself, but by doing that annoying operation:

Open App>Utility>Terminal.app;

~ user$ sudo /PATH_TO_TERMINAL_EXEC

Get administrator privileges;

Done.

... That's what is inside terminal_executable

#!/bin/bash

echo PATCHING HOSTS FILE...!
echo  >> /etc/hosts
echo \# IsPatched >> /etc/hosts
echo 127.0.0.1 somewebsite.com >> /etc/hosts

... What I have already tried is

#!/bin/bash

tmpvar=$(whoami)
echo TRYING!
su $tmpvar echo 127.0.0.1 somesite.com >> /etc/hosts

or

su cuskus -c sudo -s echo 127.0.0.1 somesite.com >> /etc/hosts

just got #!/bin/bash window after asking for password but cannot execute any code because of privileges.

1

You must log in to answer this question.

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