When a rule in sudoers
authorizes a user to run a specific command wibble
, it is often necessary to set PATH
to a safe value: one that only contains directories where the user cannot plant his own programs. If wibble
called the external command foo
, and PATH
was not reset, then the user could put ~/bin
at the front of his PATH
, link /bin/sh
to ~/bin/foo
and then run sudo wibble
to invoke ~/bin/foo
which lets him type arbitrary shell commands.
Resetting PATH is therefore a safe default. While it is not necessary in all cases, it is a lot easier and safer to make that the default setting.
When a rule in sudoers
authorizes a user to run arbitrary commands, resetting PATH has no direct security advantage. There is an indirect advantage, which is that the user may accidentally have set a PATH containing potentially harmful programs, and resetting PATH avoids risking that these programs are called accidentally. There is also a functional advantage: it is common to need to have /usr/local/sbin
, /usr/sbin
and /sbin
when running commands as root but not to have them as an ordinary user.
If you don't want the path to be reset when you run sudo
, add yourself to the exempt group in sudoers
:
Defaults exempt_group+=stevebennet
If you run a login shell (sudo -I
or sudo bash -l
or other method), that shell typically reads .profile
in the target user's home directory, and that file may change PATH.