I have a strange issue where sudo
is not preserving the PATH by default. I tried the answers from this question and it works when I use the sudo env "PATH=$PATH"
version but I want to fix this permanently.
Here's my PATH as regular user and under sudo:
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/opt/jdk/bin:/opt/python3/bin:/opt/qt5/bin
$ sudo printenv | grep 'PATH'
PATH=/bin:/usr/ucb:/usr/bin:/usr/sbin:/sbin:/usr/etc:/etc
I tried sudo -E
and sudo "PATH=$PATH
but they don't work either. sudo env "PATH=$PATH"
works but I have to type it every time run sudo
. I want to avoid doing that every time I run sudo
.
I'm not sure where sudo
is picking up this path from. I don't have secure_path
hard-coded or env_reset
in /etc/sudoers
. Hence this is not a duplicate of this question.
Any ideas what is going on and how to fix this? I would prefer not create aliases or hard-code the secure_path
.
Edit:
As requested, the output of sudo -l
:
$ sudo -l
User munir may run the following commands on munir-lfs:
(ALL) NOPASSWD: ALL
sudo -l
report? – muru Nov 04 '16 at 04:06sudo env "PATH=$PATH
. Basically I want the PATH under sudo to be the same as the users PATH, always. – Munir Nov 04 '16 at 14:06secure_path
unless as a last resort. – Munir Nov 04 '16 at 14:12PATH
. You can overwrite it (withsecure_path
) but you shouldn't really be looking to preserve it. – Chris Davies Nov 04 '16 at 14:22/usr/local/bin
and/opt
doesn't work becausesudo
can't find it. My current PATH withoutsudo
is the system PATH from/etc/profile
. So, it is really surprising thatsudo
is not using that but creating it's own PATH. – Munir Nov 04 '16 at 14:27secure_path
definition and then checksudo -V
. On my system here this preservesPATH
(and can be verified as doing so). – Chris Davies Nov 04 '16 at 14:42secure_path
defined, which is why I am confused that the PATH is different undersudo
. My current/etc/sudoers
has only two lines which are not commented out:root ALL=(ALL) ALL %sudo ALL=(ALL) NOPASSWD: ALL
– Munir Nov 04 '16 at 15:02sudo -V
havePATH
listed in? – Chris Davies Nov 04 '16 at 15:11sudo sudo -V
. You might try adding a sudoers entry to override any builtin options:Defaults:munir !secure_path, env_keep+="PATH"
. Make sure you have a root shell somewhere to reover with first. – meuh Nov 04 '16 at 15:53secure_path
since this question has been (incorrectly) closed. – Munir Nov 04 '16 at 22:52PATH
to carry through. I achieved it on my test system here so it's clearly possible. – Chris Davies Nov 04 '16 at 23:56secure_path
and it works as expected. But if you have an alternate would be interested in knowing it. Let's wait for this to reopen. – Munir Nov 05 '16 at 00:20