Search code examples
sudocentos6

centos 6.4, sudo -u -H doesn't work


I have problems using some commands from sudo.

I tried below command on Centos 6.4 and got an error.

sudo -u someuser -H bundle install
sudo: bundle: command not found

However when I try same command on Ubuntu 14.04 it works fine.

Not only bundle but also many commands get same error. For example ruby, node. However some commands work fine. For example echo, mkdir, touch ...

Anyone have some ideas?


Solution

  • secure_path in /etc/sudoers ensures that your path is clean when running binaries under sudo. In CentOS you should have something like

    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
    

    That explains why some commands are found (echo, mkdir, touch ...).

    You can keep your environment variables (as $PATH) in this form

    sudo -u someuser "PATH=$PATH" bundle install
    

    From man sudo

    Environment variables to be set for the command may also be passed on the command line in the form of VAR=value, e.g. LD_LIBRARY_PATH=/usr/local/pkg/lib. Variables passed on the command line are subject to the same restrictions as normal environment variables with one important exception. If the setenv option is set in sudoers, the command to be run has the SETENV tag set or the command matched is ALL, the user may set variables that would otherwise be forbidden. See sudoers(5) for more information.

    Depending on your system's security policy, you could use -E instead of the method written above.

    -E The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.