Comments, Code and Qt. Some words about the wonderful world of software engineering

13Feb/123

‘sudo ./script.sh: command not found’ on Ubuntu

I am pretty sure I could run my script last week, but I cannot anymore today.

kypeli@ubuntu:~$ sudo ./myscript.sh
[sudo] password for kypeli:
sudo: ./myscript: command not found

Obviously this was very strange to me because the script was in the current working directory, all permissions looked fine and I could run the script just fine without sudo, which indicated that the shebang was ok.

kypeli@ubuntu:~$ ls -la ./script.sh
-rwxr-xr-x 1 root root 568 2012-02-13 05:49 ./script.sh

kypeli@ubuntu:~$ ./script.sh
Usage: ./script.sh [foobar] Give 'foobar' to do Baz

So what has happened? Apparently Ubuntu changed something in some update (I am still to verify this) which prevents me from running scripts as sudoer from other than sudo compile time predefined paths. You can verify this by looking at the flags that were used to compile sudo and look for the --with-secure-path option. sudo will not run any script (or command for that matter) outside of these paths. Including .

kypeli@ubuntu:~$ cat /usr/share/doc/sudo/OPTIONS
...
--with-secure-path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin"

Give a reasonable default path for commands run as root via sudo.

Well - this was a bit annoying to me for two reasons. I am pretty sure this worked for me last week on my Ubuntu, but suddenly it doesn't work anymore. And secondly, I am not running any public server and I want to run my own scripts with root privileges (why I want to do that is a different topic :)) if I so choose. I must stress that I can see the reasons for doing this and security is always a trade off between security and usability. I would not do this on any public server, but for Ubuntu to not allow me to run my own commands as root is a bit annoying.

Luckily I found this tip from Björn Wijers' blog: http://www.burobjorn.nl/blog/2012/01/04/fix-sudo-and-path-environment-variable-on-ubuntu/

Update: Seem Björn's blog is down, so I'll post the solution here. Put the following line to your $HOME/.bashrc file:

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

And the run source $HOME/.bashrc (this will not be necessary on subsequent boots or starts of the terminal). You should again be bale to run your scripts with sudo.

This will enable the fix only for me, as I put the alias in my .bashrc file, so it's not even a terrible security risk. And that's fine.

I would also like to thank @anidel for pointing out initially this possibility.

Technorati Tags: , , , , , ,

  • Paul Childs

    The blog is not available anymore. Could you post the fix?

    • http://www.johanpaul.com/blog/ Johan Paul

      Thanks for letting me know, I didn’t notice that. The fix has been updated to the blog text.

  • attabi

    Hi, I am using Monkey-spider-0.2(ms-extract.py),trying to read heritrix-2.0.2 (arc file) on ubuntu-11.04 although I did /etc/environment /etc/bash.bashrc and chmod + x (heritrix and arcreader) I got this log

    ms-extract-arc sh: arcreader: not found

    please I need help seriously