5

I set PATH in /etc/profile. I do not have any problems in the case of the general user, but PATH does not go when I become the root authority in sudo.

What would you do to override the PATH which is set by /etc/profile even if I become the root authority in sudo?

1

4 Answers 4

9

Sudo resets PATH, along with many other environment variables, for security reasons. It doesn't matter what is set in /etc/profile, unless you run sudo -i.

The sudo manual page, under SECURITY NOTES, gives an insight about the security implications, along with the options you have to fiddle in your /etc/sudoers file to modify these restrictions, after you understand the implications of doing so.

2

root's path is not changed by /etc/profile (security ...)

you have to set it up separately, (e.g. in root's ~/.bash_profile)

2
  • In what world is it insecure to have root's shell affected by a file which is only editable by root, but instead affected by a different file also only editable by root? :D
    – dannysauer
    Apr 12, 2023 at 15:05
  • This is not only incorrect re: how bash works, but also incorrect in terms of how sudo works. The shell init files are not read by a regular sudo invocation at all. If sudo is launched with the -i option, then both /etc/profile and ~/.bash_profile are read, following bash's normal rc file loading rules for a login shell.
    – dannysauer
    Apr 12, 2023 at 15:17
1

update:

If you want to add a specific PATH to the default PATH of your "root" account when using sudo, or sudo -i:

With Ubuntu 10.04.x LTS:

Modify accordingly both the /etc/environment and the etc/login.defs files.

With Ubuntu 12.04.x LTS:

Modify accordingly both the /etc/environment and the /etc/login.defs files as well as the line Defaults secure_path of the /etc/sudoers file (using visudo).

If you're using a GUI and aren't comfortable with the nano editor in text mode, use the following command to edit the /etc/sudoers file with gedit:

sudo EDITOR=gedit visudo

When you'll save the /etc/sudoers file, visudo will check the syntax of your modifications.

If all is OK, you'll go back to the terminal session.

If there is an error, press the letter "e" to edit the /etc/sudoers file again and correct your modification.

I didn't try with Ubuntu 12.10 yet but I guess (and hope) it's the same thing than for Ubuntu 12.04.x!

Cheers.

0

In case if your Linux distributions has security policy plugin enabled, the PATH can be overridden by secure_path in your sudoers file, so you need to edit it by sudo visudo and add it to the list.

You can check that if that's the case by:

$ sudo sudo -V | grep PATH
Value to override user's $PATH with: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Alternatively for one-time use, try:

sudo "PATH=$PATH" foo

See: Why are PATH variables different when running via sudo and su? at Unix SE

You must log in to answer this question.

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