Setting the PATH so it applies to all users, including root/sudo

123,663

Solution 1

Update:

Setting global environment variables should still be performed in /etc/environment, but as you pointed out, using sudo -s has the effect of these variables are "vanished".

The reason for that is sudo has a policy of resetting the Environment and setting a secure path. It is enabled by default:

/etc/sudoers:

Defaults  env_reset
Defaults  secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Thus whatever is set in the /etc/environment for the path is overridden by sudo.

The manual page for sudoers states:

   env_reset       If set, sudo will reset the environment to only contain
                   the LOGNAME, MAIL, SHELL, USER, USERNAME and the SUDO_*
                   variables.  Any variables in the caller's environment
                   that match the env_keep and env_check lists are then
                   added.  The default contents of the env_keep and
                   env_check lists are displayed when sudo is run by root
                   with the -V option.  If the secure_path option is set,
                   its value will be used for the PATH environment
                   variable.  This flag is on by default.

As a workaround, you can use sudo su that will provide a shell with root privileges but containing the right PATH.


Original Answer

You should set it in /etc/environment.

Try sudo YOUR_TEXT_EDITOR /etc/environment (make sure to create a backup first).

For more information: EnvironmentVariables

System-wide environment variables

Environment variable settings that affect the system as a whole (rather then just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

/etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Not recommended:

/etc/profile - This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.

/etc/bash.bashrc - This is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.

Solution 2

sudo -i will simulate a login (in a similar way to sudo su -) and thus give you these environment variables.

Share:
123,663

Related videos on Youtube

Subbu
Author by

Subbu

I'm an active developer for LaTeX, most notably with the package siunitx for formatting numbers and units. In my day job I work as a chemist, and so you'll see I write a number of chemistry-related LaTeX packages. I'm also a member of the LaTeX3 Project, working on improving LaTeX with the eventual aim of producing LaTeX3. To keep people up to date with my TeX work and anything else TeX-related I see, I have a blog on TeX matters. I'm also keen on helping other TeX users, and so I try to watch the various places that people go for help. I'm also on the committee of the UK TeX Users' Group. So I pop up all over the TeX world!

Updated on September 18, 2022

Comments

  • Subbu
    Subbu over 1 year

    The instructions in How do I set PATH variables for all users on a server? work to set the PATH for all 'normal' users. However, if I do sudo -s and then printenv PATH the updated path is not shown. I've taken a look at for example Setting TeX Live path for root, but this does not seem to make sense to me, perhaps as I'm from a Windows background. Is there any way to set the truly system-wide path, such that the entries are inherited by absolutely every process running on the system?

  • Subbu
    Subbu about 12 years
    Ah well: to me this is all the wrong way around (everyone should inherit the same path, period), but I guess that's just how it is.
  • Martijn Heemels
    Martijn Heemels about 9 years
    It does not work that way. At least PATH gets reset on my Ubuntu Trusty test-vm.
  • Elliot Labs LLC
    Elliot Labs LLC over 7 years
    You can use visudo (needs root permissions) to edit the secure path settings. this is not a recommended practice. It is just here for education.