command not found when using sudo

There are a few question on the site that seem related to my problem but I could not find a solution in any of them.

My operating system is Ubuntu 12.04. I have mvn installed in /tools/noarch/apache-maven-3.1.1 and I have added the following lines at the end of my /etc/profile:

export M2_HOME=/tools/noarch/apache-maven-3.1.1
export PATH=$M2_HOME/bin:$PATH

After that I execute source /etc/profile.

Now my problem is: when I run mvn --version the command succeeds and mvn executable is found, while if I execute: sudo mvn --version I get the output: sudo: mvn: command not found. I know that PATH may be different when I execute a command with sudo and that is why I tried this:

$>sudo echo $PATH
/tools/noarch/apache-maven-3.1.1/bin:... some other stuff ...

Another thing I tried is to execute sudo su - and then type mvn --version. In this case mvn is successfully found and the command succeeds. What is going on here?


Solution 1:

$PATH is evaluated by your shell, so your check doesn't work as you expect it to.

/etc/sudoers is configured to replace your PATH with a default one.

sudo does not load a login shell environment before executing the command, so the default PATH from /etc/sudoers is used. su - does open a login shell, which involves loading /etc/profile. See man bash, section INVOCATION.

Just remove the PATH reset in /etc/sudoers. It's likely a rule called secure_path.


CentOS

In CentOS you can add PATH to the Defaults env_keep section:

Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY PATH"

Solution 2:

Give sudo your current PATH with:

sudo env "PATH=$PATH" your_command

Solution 3:

Elaborating on @opyate's answer, I am using the following shell script (which may be named mysudo, for example):

#!/bin/bash
sudo -E env "PATH=$PATH" "$@"
  • -E tells sudo to preserve the environment.
  • env "PATH=$PATH" is expanded outside the sudo call, making the external PATH available inside the sudo too (this is required in addition to the -E as the PATH usually receives special treatment in addition to the treatment the entire environment receives).
  • "$@" passes the arguments our script receives to the sudo line.

Save the script in a file in a directory in the PATH, give it +x permissions, et voilà.