Why can't I sudo some commands? (e.g., vim)

16

5

I have sudo access on my friends server, running centos-6.3, but when I try to run some commands like sudo vim /var/www/html/index.html I get an error sudo: vim: command not found I can, however, run sudo su and then vim /var/www/html/index.html and it works as expected.

echo $PATH and sudo echo $PATH both yield:

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/jared/bin

sudo which vim however yields:

which: no vim in (/sbin:/bin:/usr/sbin:/usr/bin)

I've tried adding

export PATH=$PATH:/usr/local/bin

to the /root/.bashrc which as fixed the issue when using sudo su but not just sudo <command>.

How do I get sudo <command> to work?

JaredMcAteer

Posted 2013-02-08T17:07:04.900

Reputation: 481

Have you already added that user to the sudoers in the Centos OS? – AAlvz – 2013-02-08T17:26:53.877

Answers

18

When running sudo, many systems are configured to clear the environment of all non-whitelisted values, and to reset the PATH variable to a sanitized value.

You will find the former as Defaults env_reset and several Defaults env_keep += "SOME_VARIABLE_NAME" in /etc/sudoers. The latter "secure" PATH override is specified as Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin – delete this line to remove this behavior when sudoing.


How which environment variable is handled is printed when you run sudo -V as root.


If you don't want to get rid of these defaults, you can always specify programs using their full path (sudo /usr/local/bin/vim).

Alternatively, you can allow your account to SETENV in the sudoers file, for example:

%wheel  ALL=(ALL)       SETENV: ALL

This allows you to override environment defaults like this: sudo PATH=$PATH which vim, as the variable is intepreted by your shell before the command is executed, resulting in an inherited PATH (which will likely not include /sbin etc. though).

Daniel Beck

Posted 2013-02-08T17:07:04.900

Reputation: 98 421

Thanks. I'll use Dennis' work around until I get the owner to admend the sudoers file. – JaredMcAteer – 2013-02-08T17:36:31.580

6

sudo echo $PATH

does not do what you think. $PATH gets replaced by the (your) shell before execuitng the command.

To accomplish the desired behavior, you can use sudo -i.

From man sudo:

-i [command]

The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution.

Dennis

Posted 2013-02-08T17:07:04.900

Reputation: 42 934

1This is a suitable workaround until I can get the owner to adjust the sudoers file. – JaredMcAteer – 2013-02-08T17:38:11.747

1

How do I get sudo <command> to work?

Until you resolve the problem with paths use a full pathname

  sudo /usr/local/bin/vim /var/www/html/index.html

RedGrittyBrick

Posted 2013-02-08T17:07:04.900

Reputation: 70 632

5I'm not going to downvote you, but I'm never fond of just working around problems rather than getting right to the point of figuring them out and fixing them. – Nicole Hamilton – 2013-02-08T17:24:53.590