10

I've installed a binary dep in my GOPATH at /home/me/go/bin to be used.

Running dep successfully executes the binary, however running sudo dep results in sudo: dep: command not found:

$ dep
Dep is a tool for managing dependencies for Go projects

Usage: "dep [command]"
...

Use "dep help [command]" for more information about a command.

$ sudo dep
sudo: dep: command not found

The paths are not the issue here:

$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin

$ sudo echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin

The paths are identical as me and as superuser both referencing the key directory /home/me/go/bin.

Why does running dep without sudo succeed but with sudo results in command not found?

1
  • 3
    $PATH is expanded before sudo ever runs; try sudo sh -c 'echo "$PATH"' instead.
    – chepner
    Commented Jan 3, 2019 at 16:30

1 Answer 1

9

By default, sudo does NOT pass the user's original PATH into the superuser process, and it gets some default PATH defined on the system. That's easy to see if you run "sudo env" to see the entire environment of the sudo'ed process:

$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin

The command you tried, "sudo echo $PATH" doesn't check anything, because the shell first translates the $PATH to whatever value this variable has - and only then calls the command (sudo), so it just prints your outer environment's value :-)

To get your PATH to pass inside sudo, you can do something like this:

$ sudo PATH=$PATH sh -c env | grep PATH
PATH=/usr/share/Modules/bin:/usr/lib64/ccache:/home/nyh/gaps:/home/nyh/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/sbin:/sbin:/usr/games:/usr/local/android-sdk-linux/tools:/usr/local/android-sdk-linux/platform-tools:/home/nyh/google-cloud-sdk/bin

Basically the command I passed for sudo to run starts by setting PATH to $PATH (remember that $PATH is expanded by the outer shell, before sudo ever runs, so is the real path I want!) and running a shell (which will use this new PAT) to "env". As you can see, env did get the right path. You can replace "env" by whatever program you wanted to run.

1
  • Good answer. With that said (and if you don't mind 'littering' in a system path area), just dropping them in /usr/local/bin for custom binaries and /usr/local/sbin for custom scripts that require sudo is an easy solution. That particular area /usr/local/- is a common 'first hit' in many distros $PATH statements. Meaning it's looked in first before /bin/ or /sbin/.
    – B. Shea
    Commented Oct 15, 2020 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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