11

I've modified my .profile to include a folder if a flash drive is plugged in. When running the command as the user it works fine but tells me the scrip must be run by sudo (this is what i want). However, when i try to run it with sudo i get "command not found"

I have a symlink (flash) in my /var/www folder pointing to my /media/flash drive. (nevermind this setup since is just for dev)

this is my user's .profile :

# set PATH so it includes flash scripts
if [ -d "/var/www/flash/scripts" ] ; then
    PATH="/var/www/flash/scripts:$PATH"
fi

when trying to run as sudo i get:

sudo: script: command not found    

any ideas?

4 Answers 4

4

When you run the script as sudo you are trying to access root. So your $HOME/.profile wont be accessed instead /root/.profile will be accessed.

So make the changes what ever you did in $HOME/.profile to /root/.profile. To create /root/.profile. Use the following commands.

sudo su
cd /root
touch .profile

after making the changes try running the script.

or edit $HOME/.bashrc and add the following line

alias sudo='sudo env PATH=$PATH $@'

Hope this helps.

4
  • I tried "alias sudo='sudo env PATH=$PATH $@'" but i get "env: wp: Permission denied"
    – user564448
    Nov 9, 2012 at 17:35
  • sorry, "wp" is the script
    – user564448
    Nov 9, 2012 at 17:47
  • Try the other step I mentioned.
    – devav2
    Nov 9, 2012 at 17:49
  • 1
    Read it here you should use sudo su -p
    – devav2
    Nov 9, 2012 at 17:53
3

that is the behavior of sudo in ubuntu..

for example run following command

echo 'echo $PATH' | sh

and than this one

echo 'echo $PATH' | sudo sh 

you see the output is different! to avoid different paths put the following in your ~/.bashrc

alias sudo='sudo env PATH=$PATH'
1
  • After putting alias sudo='sudo env PATH=$PATH' into my .bashrc i get "env: wp: Permission denied"
    – user564448
    Nov 9, 2012 at 17:33
3

You can preserve the value of $PATH when using sudo. Use visudo to add $PATH to the list of variables to keep. A line like the following in /etc/sudoers should do the trick:

Defaults env_keep += "PATH"
-1

You can also move your file in a sudoers used directory :

    sudo mv $HOME/bash/script.sh /usr/sbin/ 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .