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?


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.


I am running Fedora 15.

  • 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. Jul 5, 2011 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.
    – beatgammit
    Jul 5, 2011 at 15:09
  • Possible duplicate of Why are PATH variables different when running via sudo and su?
    – kenorb
    Dec 24, 2015 at 17:24

4 Answers 4


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.

  • 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 9, 2011 at 5:29

You can check this quite easy with things like

Compare the output from

sudo env 

And things like

sudo whoami 

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

  • 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 5, 2011 at 9:16
  • sudo whoami returns root, like expected. What could be causing my sudo env to be different than my root env?
    – beatgammit
    Jul 5, 2011 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, 2011 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)?
    – beatgammit
    Jul 5, 2011 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, 2011 at 9:24

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.


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


...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.


cat <<EOF > /etc/profile.d/tjameson.sh
export PATH=$PATH:/usr/local/bin
  • sudo has special handling of PATH so you shouldn't do this. Jul 5, 2011 at 17:47

You must log in to answer this question.

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