17

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?

1
  • Have you already added that user to the sudoers in the Centos OS?
    – AAlvz
    Feb 8, 2013 at 17:26

3 Answers 3

19

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

1
  • Thanks. I'll use Dennis' work around until I get the owner to admend the sudoers file. Feb 8, 2013 at 17:36
8
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.

1
  • 1
    This is a suitable workaround until I can get the owner to adjust the sudoers file. Feb 8, 2013 at 17:38
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
1
  • 5
    I'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. Feb 8, 2013 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .