Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to sudo some binaries that lies in a custom path. That custom path is removed when I run sudo though, but sudo -E should preserve my path. Why doesn't it work?

$ env | egrep ^PATH

$ sudo env | egrep ^PATH

$ sudo -E env | egrep ^PATH

I know how to work around it, I just want to know why sudo -E doesn't work

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You can set the exempt_group option to tell sudo to keep the PATH for users in that group.

For example, say your user is in the group 'sys'. Add the following to your sudoers file

Defaults exempt_group="sys"

Now your user will not have PATH reset for sudo commands (-E is not needed for this to work).
See the man page for more details.

EDIT: Going to have to note this as a bad answer. It is true that it works, but it has a side effect I didnt notice while playing with it. It also exempts users in that group from having to type their password. Seems you cant get PATH preservation without allowing this. Bit stupid I think...

share|improve this answer
add comment

Proposing another solution in addition to the one I already entered. This works for bash only (but can be modified for other shells).

The following is a wrapper around sudo that will look for the command youre passing to it. Once it finds the command it changes it to the fully qualified path.
So in effect sudo echo hello becomes sudo /bin/echo hello.

Put the following in your ~/.bashrc

function sudo() {
   local ARGS
   declare -a ARGS=()
   while (( $# )); do
      if [[ "${1:0:1}" == "-" ]]; then
         # the argument is a dash-arg; '-s' for example
         if [[ "$1" =~ ^-[pugD]$ ]]; then
            # these are the arguments that take a parameter, so skip the next
      elif [[ "$1" == *"="* ]]; then
         # the argument is a env var to set, not a command
         # finally, a command
         CMD="$(which --skip-alias --skip-functions "$1")"
         [[ -z "$CMD" ]] && ARGS[${#ARGS[@]}]="$1" || ARGS[${#ARGS[@]}]="$CMD"
   command sudo "${ARGS[@]}" "$@"

Note, it wont properly handle it if you have a command with an = in its name. This is extremely unlikely, so I accepted this caveat to keep it simple.

share|improve this answer
add comment

I'd look to the options in the sudoers file as they control some of the sudo behavior.

And of course I'd consult the man page for the sudoers file.

share|improve this answer
All the sudoers file does is set Defaults env_reset and the define some user privileges. Shouldn't sudo -E override the defaults? –  Codemonkey Feb 3 '12 at 23:46
@Codemonkey No, the philosophy behind sudoers is to provide policy, not merely "defaults". So an ordinary user cannot override with the command line switches the policy that has been globally set by root to env reset. –  kubanczyk Feb 3 '12 at 23:50
I'd think that probably not, since the root user (i.e. administrator) is controlling the configuration and any user can access sudo (it may not get them anywhere). Thus you'd be overriding something that the administrator wants to be enforced. –  mdpc Feb 3 '12 at 23:51
Is there any way to allow overrides for certain users? Also, can't any user with sudo access just change the defaults? –  Codemonkey Feb 3 '12 at 23:57
Sudo users do NOT necessarily have root access, and thus this your statement is not true. –  mdpc Feb 4 '12 at 0:18
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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