17

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.

beatgammit
  • 7,583

4 Answers4

7

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 every time you use sudo; but when you use sudo -i, the path is changed after that by the root user's RC files.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Arrowmaster
  • 1,684
  • 1
    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! – beatgammit Jul 09 '11 at 05:29
5

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.

Johan
  • 4,583
  • 1
    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? – beatgammit Jul 05 '11 at 09:16
  • sudo whoami returns root, like expected. What could be causing my sudo env to be different than my root env? – beatgammit Jul 05 '11 at 09: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 05 '11 at 09: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)? – beatgammit Jul 05 '11 at 09: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 05 '11 at 09:24
  • Still didn't fix it. I tried adding a line to the end of '/etc/bashrcand/etc/profile`, neither of which changed the PATH output from sudo env. – beatgammit Jul 05 '11 at 09:37
  • http://stackoverflow.com/questions/257616/sudo-changes-path-why – Johan Jul 05 '11 at 09:40
3

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.

M'vy
  • 186
0

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