Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command not found with sudo, but works without sudo

Tags:

bash

command

sudo

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?

like image 323
Shiri Avatar asked Jan 03 '19 16:01

Shiri


People also ask

How do I fix sudo command not found?

Step 1: Install the 'sudo' command To achieve this, log in or switch to root user and use the APT package manager to update the system package list. Then install sudo as shown. When prompted to continue. hit 'Y' to proceed.

How do I enable sudo commands?

To configure the sudo command, you can edit the sudoers file by using the visudo command. To enable the user to run the commands, in the sudoers file, under the user privilege specification, specify the username and commands. The user can run only the commands specified in the user privilege section for the user.

How do I fix sudo command not found Mac?

Before attempting to fix this error double check the syntax to ensure the command you're trying to run is correct. If you haven't done so yet you'll need to enable the root user, this may be all that's needed to fix the error.

How do I use sudo instead of root?

Using “sudo,” you can do pretty much the same things you can with “su.” To use it, you just have to add “sudo” in front of all root commands. Having root user privileges can be dangerous, but using sudo instead of su can help you keep your system more secure.


1 Answers

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.

like image 200
Nadav Har'El Avatar answered Oct 05 '22 23:10

Nadav Har'El