sudo command won’t run from script

Posted on

Problem :

Linux Mint.

Ok, this is going to be seriously basic but for some reason, I am mis-understanding how this works.

If I type the following commands, everything works fine:

cd home/user/folder1/folder1/

sudo openvpn --auth-nocache --config /home/user/folder2/folder2/config-file.ovpn 

I input the sudo password and all is good.

If I put these commands into a script file, I get nothing but ‘command not found’.

I have placed the script file in a folder, added both the script AND the opvn folders to the path.

I created a new script (without the paths) in the same folder as the opvn files, ran it from there and fail.

sudo opvn-script.sh 
[sudo] password for <user>: 
sudo: opvn-script.sh: command not found

What I don’t understand is the script file itself can’t be the thing ‘not found’ because it auto-completes after I type a the first few characters and hit tab. openvpn can’t be the ‘not found’ because I can run the commands straight up.

The script file IS marked as executable.

echo $PATH shows both folders in the path.

What the heck is left??

Solution :

Consider this error message:

sudo: opvn-script.sh: command not found

This occurred after you entered the password and indicates that, under the PATH of the new user (root), the script could not be found.

One solution is:

sudo /path/to/opvn-script.sh 

If that works, then move opvn-script.sh to a convenient location somewhere on root’s PATH.

For security, make sure that permissions are set so that only root can modify (write) opvn-script.sh.

Ah, I think I see it. (Hard to tell remotely of course, and I don’t have a linux box at hand.)

When you run a command via sudo, ie

$ sudo blah

there’s a potential security hole — consider, eg, if someone edited your script and added vi /etc/passwd to it. I beleive Linux changes the path to prevent this.

To check, write a script that just echoes $PATH and run it from sudo. If the path is not what you expect, you’ve found your problem.

Path to script was not in SUDO user profile, moved script to a SUDO path

Leave a Reply

Your email address will not be published.