How do I add sbin to PATH while taking over $PATH at sudo?

Asked 2 years ago, Updated 2 years ago, 80 views

When sudo,
is configured to take over the user-specific $PATH as the secure_path is configured,
to /etc/sudoers

Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/bin:/sbin:/bin"
Defaults extract_groups = developers

It seems that it is common to write as shown in .
However, this will result in the user-specific $PATH being exactly the same as the new $PATH.
I would like to set the sum of the user-specific $PATH and secure_path to $PATH at sudo, is that possible?

linux sudo

2022-09-30 16:08

1 Answers

-i [command] You may want to load .profile or .login with the option

In the following example, hoge user .bash_profile adds ~/bin.

userhoge:

$cat to /.bash_profile
#.bash_profile
export PATH=$PATH:$HOME/bin

$ cat<<eof>/home/hoge/test.sh
#!/bin/bash
echo PATH = $PATH
eof

$ chmod+x/home/hoge/test.sh

userfoo:

$sudo-uhoge/home/hoge/test.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

$ sudo-i-uhoge/home/hoge/test.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/hoge/bin

However, please note that environment variables such as HOME SHELL USER LOGNAME are also rewritten.
For more information, see man sudo


2022-09-30 16:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.