Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to run a command under userx using sudo that is normally not available in the PATH. Command is available in bash under userx because of HOME/.bashrc.

Simply if I run in bash under userx

echo $PATH
/opt/sw-python/extra/pythonbrew/bin:/opt/sw-python/extra/pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

I get the right PATH and command is available.

But if I run with sudo:

sudo -u userx echo $PATH
/home/curuser/.pythonbrew/bin:/home/curuser/.pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo -H -u userx echo $PATH
/home/curuser/.pythonbrew/bin:/home/curuser/.pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo su python -c 'echo $PATH'
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo su python -c 'bash -c "echo $PATH"'
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

PATH variable is not set using userx's .bashrc.

How to run with sudo so that PATH will be normal PATH under userx?

share|improve this question

migrated from stackoverflow.com Jul 19 '12 at 2:08

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 2 down vote accepted

Run sudo with -i to have it start a new interactive shell (and load all the user configurations).

  -i [command]
               The -i (simulate initial login) option runs the shell specified by the password database 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 via the shell's -c option.  If no command is
               specified, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the
               shell.  The security policy shall initialize the environment to a minimal set of variables, similar to what is present
               when a user logs in.  The Command Environment section in the sudoers(5) manual documents how the -i option affects the
               environment in which a command is run when the sudoers policy is in use.
share|improve this answer
    
Oops, I read this in man, but did not realize this is what I need. Also it's possible to use su usrex -l -c "command" –  user674618 Jul 18 '12 at 3:32
    
This needs the user's password instead of using sudo. –  Benjamin Bannier Jul 18 '12 at 5:36

Your Answer

 
discard

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