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:


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?

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

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

|improve this answer|||||
  • Thanks. I'll use Dennis' work around until I get the owner to admend the sudoers file. – JaredMcAteer Feb 8 '13 at 17:36
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.

|improve this answer|||||
  • 1
    This is a suitable workaround until I can get the owner to adjust the sudoers file. – JaredMcAteer Feb 8 '13 at 17:38

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
|improve this answer|||||
  • 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. – Nicole Hamilton Feb 8 '13 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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