When using sudo
your $PATH
is getting overridden for security reasons by a line like this in your /etc/sudoers
file:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Example
Even though an entry such as this is present in /etc/environment
:
$ cat /etc/environment
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp
And it appears to be picked up:
$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp
And we have a script in /tmp
:
$ ls -l /tmp/blah.bash
-rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash
And this script will print whoami
when run:
$ cat /tmp/blah.bash
#!/bin/bash
whoami
Sudo won't let us do it:
$ sudo blah.bash
sudo: blah.bash: command not found
Changing sudo's secure path
But if I edit my /etc/sudoers
file:
$ sudo visudo
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp
Now it works:
$ sudo blah.bash
root
Security
Think long and hard about why you're adding additional directories to sudo
's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo
commands!
Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.