Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I made a bin directory in my home folder where I place all my scripts. Then in my .bashrc I added the following:

export PATH=$PATH:/home/myusername/bin

So I could access files I placed in there from anywhere. But some of the scripts need to be executed as root. So I thought, I could symlink my .bashrc as root, (as in /root/.bashrc points to /home/myusername/.bashrc, don't know if this is smart) so when I need to run a script as root I can just do:

sudo program_that_requires_root

But then I get a:

sudo: program_that_requires_root: command not found

If I login as root and execute the program, it works fine though. So what is the correct way to accomplish what I want?

share|improve this question
2  
My guess: sudo probably is using secure_path instead of your $PATH. Run sudo -V as root (e.g. sudo sudo -V) and look for lines that contain PATH. –  jw013 Oct 18 '12 at 17:14
2  
Yes, at least in Debian, open the file /etc/sudoers and find this line: Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin (paths may vary) - if you add your user bin path, it'll work. (Perhaps that solution is not recommended for other reasons; I don't know.) Oh, in the file mentioned, it says you shouldn't change it, but I did, and it works great, at least in terms of what I tried to achieve (having the same problem as you). –  Emanuel Berg Oct 18 '12 at 19:27

3 Answers 3

up vote 3 down vote accepted

You have to make sure these two lines are present in the sudoers file.

Defaults        env_reset
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$

See these URLs for more details:

share|improve this answer
    
I guess $ at the end of the second (code) line followed when you copied - it should be a closing quote, of course. Also, the new path to the user binaries (and other executables, as @Carlito mentioned scripts) must be added. (You probably did this; just got chopped off.) –  Emanuel Berg Oct 19 '12 at 21:01
    
Thanks, I looked in the sudoers file and added my path to the second line, works great! –  Carlito Oct 22 '12 at 9:41

sudo resets the PATH environment variable to a built-in or administrator-supplied default, in most configurations. So you won't be able to just type sudo myscript.

You could write a wrapper function:

sudo () {
  local cmd
  case $1 in
    -*) :;; # there are options, change nothing
    */*) :;; # explicit path, change nothing
    *) # a command name, look it up in our $PATH
      cmd=$(type -p -- "$1")
      if [[ -n $cmd ]]; then shift; set -- "$c" "$@"; fi;;
  esac
  command sudo "$@"
}

(This is for bash; change type -P to type -p in ksh or zsh.)

share|improve this answer
    
Hm, though I don't doubt this does it, it seems a bit complicated - is there if fact some hidden gain, compared to editing /etc/sudoers, which seems a much simpler solution? –  Emanuel Berg Oct 19 '12 at 21:08
1  
@EmanuelBerg If this is your computer, you can edit the sudoers file and change secure_path. If this is a shared machine, the admin is unlikely to be willing to accommodate you: a user-chosen secure_path setting, or recompiling sudo without that option, would be a maintenance headache. –  Gilles Oct 19 '12 at 21:21

By default sudo resets the environment. Have you tried adding the path to root's .bashrc? Or invoke sudo with -E (which only works if setenv in sudoers is set, or the respective command has the SETENV tag set).

share|improve this answer
2  
Changing root's .bashrc won't help: sudo doesn't start an interactive instance of bash here. –  Gilles Oct 18 '12 at 23:33

Your Answer

 
discard

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.