Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I run sudo, what exactly happens to my environment?

When I run sudo command, it doesn't seem to see my or root's environment. For example, my path for both includes /usr/local/bin, but if I try to run one of the program's without the full path, it fails.

I thought sudo ran as root, and hence got root`s environment. Is there a different way that bash executes under sudo than under root or my normal user?

EDIT:

I have been using sudo -i lately, but recently it has been causing problems because my current working directory gets set to /root. This is as expected (sorta), but I still don't understand why sudo isn't recognizing my executables in /usr/local/bin.

EDIT:

I am running Fedora 15.

share|improve this question
    
What OS are you using (if Linux, what distribution)? There are (too) many ways to configure PATH, and sometimes it's not easy to find who overrides it last. It could be some system setting (e.g. in PAM), sudo itself, or a subsequent profile script. –  Gilles Jul 5 '11 at 10:04
    
@Gilles- Good point. I added my OS. I have only really noticed this on Fedora (I used to use Ubuntu), but I thought that was because they didn't add a lot of convenience setup. –  tjameson Jul 5 '11 at 15:09
add comment

4 Answers

up vote 4 down vote accepted

I don't know about the defaults on Fedora, but on Debian sudo defaults to using the secure_path option with a default value of /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin.

This means the path is changed to that value everytime you use sudo, but when you use sudo -i the path is changed after that by the root users RC files.

share|improve this answer
    
I didn't know about the secure_path option. The default did not include /usr/local/bin on my install. Thanks, this really helped clear stuff up! –  tjameson Jul 9 '11 at 5:29
add comment

You can check this quite easy with things like

Compare the output from

sudo env 
env

And things like

sudo whoami 
whoami

That way you can try to find out what is missing in your setup.

share|improve this answer
    
Cool, didn't know about that. Anyway, /usr/local/bin isn't in my sudo env, but it is in my root env and user env. Why is this? –  tjameson Jul 5 '11 at 9:16
    
sudo whoami returns root, like expected. What could be causing my sudo env to be different than my root env? –  tjameson Jul 5 '11 at 9:19
    
Don't know, but there must be some missing setup somewhere, /usr/local/bin is usually included. Maybe you cleared the $PATH env by mistake in some configfile? –  Johan Jul 5 '11 at 9:20
    
Does sudo source /root/.bashrc? That's where I add /usr/local/bin to root`s path (I had to do this manually, unfortunately)? –  tjameson Jul 5 '11 at 9:21
    
I think you should modify your shells global config and see what happens. If you are running bash it would be something like /etc/bash.bashrc –  Johan Jul 5 '11 at 9:24
show 2 more comments

The sudo -i command simulate initial login. On my Debian system, it also states that:

This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the contents of /etc/environment on Linux and AIX systems. All other environment variables are removed.

share|improve this answer
add comment

You can adjust sudo's env_keep parameter to include PATH, though you should keep in mind the security implications of doing that.

visudo

...will launch your $EDITOR, then you can make changes to Defaults env_keep =... as appropriate.

Also, you can add /usr/local/bin to the system's path by adding files to /etc/profile.d.

e.g.:

cat <<EOF > /etc/profile.d/tjameson.sh
export PATH=$PATH:/usr/local/bin
EOF
share|improve this answer
    
sudo has special handling of PATH so you shouldn't do this. –  Arrowmaster Jul 5 '11 at 17:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.