140

I need to run a program installed on /opt/godi/sbin (a custom directory). If I add that directory to my PATH, by adding the following line to my .bashrc file

export PATH=$PATH:/opt/godi/bin:/opt/godi/sbin

then I can try to run the command just fine (except that it fails because it needs sudo). However, when I try to use sudo:

sudo godi_console

I get the following error

sudo: godi_console: command not found

Inspecting the PATH variable after using sudo reveals that its not including the same PATH I have as a normal user:

$ sudo sh
# echo $PATH                 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Why is the PATH not the same? Am I doing something wrong? I'm on Debian Jessie and unfortunately I cannot get around the issue by passing sudo the the absolute link to godi_console because godi_console itself also depends on the PATH being correctly set.

171

You can always do:

sudo env "PATH=$PATH" godi_console

As a security measure on Debian, /etc/sudoers has the secure_path option set to a safe value.

Note that:

sudo "PATH=$PATH" godi_console

Where sudo treats leading arguments containing = characters as environment variable assignments by itself, would also work at running godi_console with your $PATH (as opposed to the secure_path) in its environment, but would not affect sudo's search path for executable, so wouldn't help sudo in finding that godi_console.

| improve this answer | |
  • 5
    I like this answer best since it avoids need to change settings globally (i.e. preserves principle of least privilege) – Alois Mahdal Mar 11 '15 at 0:31
  • 5
    sudo "PATH=$PATH" godi_console did not work in CentOs7 by the way. Needed the env – Hakan Baba Jan 25 '18 at 6:29
  • 1
    Is it okay to use sudo env "PATH=$PATH" as an alias for sudo? What kind of problems could this create? – derricw Apr 12 '18 at 16:22
  • 2
    @ballsatballsdotballs. As that alias should only affect your interactive shells, that should be relatively harmless. – Stéphane Chazelas Apr 15 '18 at 13:43
  • 2
    I just create an alias called psudo for these types of cases, where: alias psudo="sudo env \"PATH=$PATH\"". Then my normal sudo use is unaffected. – mikeTronix May 18 '18 at 18:38
50

You can also set the default PATH at /etc/sudoers

edit the file using visudo

and update the line to what ever you wish: Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

| improve this answer | |
22

SUDO is doing env variables reset by default.

Check out its manual and option called env_reset.

You just need to disable it in /etc/sudoers.

| improve this answer | |
  • 7
    Cool! In Ubuntu you can visudo and comment out the secure_path and env_reset lines. Makes the system considerably less secure, so beware. – RawwrBag Mar 29 '16 at 18:29
  • Disabling env_reset seems not to affect sudo's behaviour w/r/t PATH. – Zanna Apr 13 '18 at 11:40
  • 1
    You have to comment secure_path as well, as mentioned by @RawwrBag – Prateek Joshi Dec 21 '19 at 6:18
7

This works :

sudo $(which your_command)

Example calling my gps script which lists Nvidia GPU's processes :

$ sudo gps
sudo: gps: command not found
$ sudo $(which gps)
  PID TTY          TIME CMD
 9922 tty7     02:42:47 Xorg

Explanation :

$ set -x;sudo $(which gps);set +x
++ which gps
+ sudo /home/xyztuv/myScripts/shl/gps
  PID TTY          TIME CMD
 9922 tty7     02:42:39 Xorg
+ set +x
| improve this answer | |
4
sudo --preserve-env=PATH env [command]

this ovverrides secure_path on my end

| improve this answer | |
1

This worked:

sudo "PATH=$PATH" [your command]

Do not change $PATH with your path value, you just write it this way

example: $ sudo env "PATH=$PATH" ant -f webAppConfig.xml regenWebAppConf....

| improve this answer | |
0

Maybe not precisely what OP asks for, but this might help:

sudo -u the_user PATH=$PATH:/opt/godi/bin sh -c 'echo $PATH'

This changes the PATH inside the sudoed command.

Edit: I am not sure what I meant by this, as the above is pretty much nonsense. Use the following instead:

sudo -u the_user sh -c 'PATH=$PATH:/opt/godi/bin echo $PATH'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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