Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some aliases and modifications to ls that I'd like available at all times. Is it possible to set environment variables for use when I am logged in as root after I sudo up with sudo su?

share|improve this question
by the way, "sudo su" is not the right way to switch to the root user, see my answer below. – demianturner Jan 23 '10 at 12:28
up vote 1 down vote accepted

Aside from the fact that sudo su is a terrible idea because it bypasses the auditing that sudo provides, you could do this in one of two ways: either modify root's .profile, which affects all sudo users, or set up a shell script that you run after sudo su to give you the variables and aliases you need.

share|improve this answer
Modifying root's .profile doesn't help. If I sudo su and then su - my aliases work, but is there a .profile file that just su would reference? I realize that su's shell is sh, not bash, but don't know where the environment files for sh are. – ridogi Jan 21 '10 at 2:44
I ended up increasing the default timeout in the sudoers file so I don't have to type in my password all the time to use sudo, which was really what I was trying to solve. That approach will be better than me using sudo su as you say, and I'm not concerned about the longer timeout as this is my home computer, and there is always sudo -k. – ridogi Jan 22 '10 at 20:17
That sounds like an excellent idea. Sorry, I was out for a bit, but there's no profile for sudo per se; you're executing with your current environment in place when you use sudo. If you do 'su -' you will get a total new login, in which you need to edit root's .profile, which resides in /root. You can check your environment as well by typing 'sudo env' and verify you have what you need. – atroon Jan 23 '10 at 4:14

There isn't an .profile for only su, but you could write a .profile which detects if you're su'ing.

If you are su'd, then "who am i| awk '{print $1}'" will be the user you came from, while "whoami" will be the user you became.

If these are different, then you su'd, so do what you need to.

share|improve this answer
So would this be a shell script in .bash_profile? Can you give an example to set an alias after using su? – ridogi Jan 22 '10 at 20:32
.bash_profile is effectively a shellscript, so treat it like any other script. Set a couple of variables, one to each command, and compare the output. – gorilla Jan 23 '10 at 0:32

The "su" in the su and sudo commands, although appearing similar, stands for different things.

su stands for substitute user, and is more commonly referred to as switch user. sudo stands for super user do, or "do this action as the super user".

If you execute a command that's reserved for root by using sudo, your environment (paths, variables, etc) will be used, ie that which is set in /Users/$your_username/.profile file.

If you type su, you switch to the root user and can access files and execute binaries limited to this user, but you will do so with your last standard user's environment.

If you type su- (note the minus) you will switch to the root user and get his environment.

On the Mac the root user, located at /var/root, does NOT have a .profile file. The workaround is to create one (you can copy your user's .profile file), place it in /var/root/, and add any aliases, paths, etc, you need to this file.

In order to invoke the file you will need to need to logout as the root user, and re-login again.

share|improve this answer

As jRandomUser, typing

sudo bash

(or whatever shell you are using) makes you 'root' with jRandomUser's shell profile. Type


when you're done to dump your privileges and return to working as jRandomUser. Obviously, use with care. My .bashrc sets my prompt to a garish color whenever my UID == 0.

share|improve this answer

You must log in to answer this question.

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