How to make `sudo` preserve $PATH?

I have a program that is installed in a custom directory under /opt. To make it easier to run it, I edited my bashrc to add said directory to my path:

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

This works fine if I want to run the program without sudo. However, if I try to run it with sudo it fails with a "command not found" error.

$ sudo godi_console
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, if it makes a difference.

One thing I tried was to invoke /opt/godi/sbin/godi_console directly, passing the absolute path to the executable. Unfortunatelly, that didn’t help in this particular case because godi_console itself depends on the PATH being correctly set.

Asked By: hugomg

||

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.

Answered By: Stéphane Chazelas

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.

Answered By: MAQ

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

Answered By: michaelbn

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

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

This changes the PATH inside the sudoed command.

Answered By: phil294

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
Answered By: SebMa

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

Answered By: ikch
sudo --preserve-env=PATH env [command]

this ovverrides secure_path on my end

Answered By: untore

Here’s what worked for me on Ubuntu.

Put this near the bottom of your ~/.bashrc:

mysudo() {
        cmd=$(which $1)
        shift
        sudo "$cmd" $@
}

alias sudo="mysudo"

Then log out and log back in, or do:

source ~/.bashrc

After that sudo works properly, like it does on other distros such as Debian.

On second thought, this is not supporting sudo’s command line switches, so if you need those, you could expand on this and make something a bit more complex to support it.

Answered By: Defcronyke

Whilst this might not entirely be what op wants, I believe the best option is to call

sudo /opt/godi/sbin/godi_console

That way you are certain you are calling the correct binary, and not one that someone sneaked into your path somewhere.

Answered By: Jens Timmerman
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.