LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   why is /usr/sbin not in root's path? (https://www.linuxquestions.org/questions/debian-26/why-is-usr-sbin-not-in-roots-path-4175736161/)

cmk77 04-18-2024 04:29 AM

why is /usr/sbin not in root's path?
 
I've been using debian bullseye for a while now and noticed that /usr/sbin is not by default in root's path, contrary to other distros. This leads some (third-party) scripts to fail, for instance because they cannot find ldconfig. What, if any, would be the reason debian ommits /usr/sbin from root's path?

Code:

$ whoami
root
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$


murugesandins 04-18-2024 04:59 AM

I used to use the following at windows (CYGWIN bash.exe)
~/.bashrc file:
Code:

$ /usr/bin/gawk.exe '{ if( ( 8 <= NR ) && ( 11 >= NR ) )  { print $0 } }'  ~/.bash_profile
if [[ -f ~/.bashrc ]]]
then
        . ~/.bashrc
fi

Few contents inside ~/.bashrc file:
Code:

if [[ -d /usr/sbin ]]
then
        echo "$PATH" | /usr/bin/grep -E "^/usr/sbin:|:/usr/sbin:|:/usr/sbin$" 2>&1 |\
        /usr/bin/grep -E -v "^$" >/dev/null 2>&1
        Ret=$?
        if [[ 0 -ne $Ret ]]
        then
                export PATH="/usr/sbin:$PATH"
        fi
fi
if [[ -d /usr/bin ]]
then
        echo "$PATH" | /usr/bin/grep -E "^/usr/bin:|:/usr/bin:|:/usr/bin$" 2>&1 |\
        /usr/bin/grep -E -v "^$" >/dev/null 2>&1
        Ret=$?
        if [[ 0 -ne $Ret ]]
        then
                export PATH="/usr/bin:$PATH"
        fi
fi
if [[ -d /bin ]]
then
        echo "$PATH" | /usr/bin/grep -E "^/bin:|:/bin:|:/bin$" 2>&1 |\
        /usr/bin/grep -E -v "^$" >/dev/null 2>&1
        Ret=$?
        if [[ 0 -ne $Ret ]]
        then
                export PATH="/bin:$PATH"
        fi
fi
if [[ -d /sbin ]]
then
        echo "$PATH" | /usr/bin/grep -E "^/sbin:|:/sbin:|:/sbin$" 2>&1 |\
        /usr/bin/grep -E -v "^$" >/dev/null 2>&1
        Ret=$?
        if [[ 0 -ne $Ret ]]
        then
                export PATH="/sbin:$PATH"
        fi
fi

I agree that we can similar kind of changes inside /etc/bash.bashrc using root(Windows Admin) user.

michaelk 04-18-2024 05:14 AM

I am guessing you are using su to login as root. su still uses the user's path environment but su - switches to root's environment. debian does not add /usr/sbin to the user's path while others do.

Turbocapitalist 04-18-2024 06:47 AM

Yes, if you are switching to root using su then you will need the -l option to start with an environment similar to a real root login.

cmk77 04-18-2024 09:16 AM

Indeed, I was using su without flags, thanks for clarifying.
- debian does have /usr/sbin in root's path (but not in a user's path)
- su without -l preserves that user's environment

_blackhole_ 04-18-2024 11:10 AM

Quote:

Originally Posted by cmk77 (Post 6496800)
What, if any, would be the reason debian ommits /usr/sbin from root's path?

Absolutely no idea, but it's just annoying. There are certain commands in /sbin you may want to run as a normal user (such as ifconfig).

In Debian, my method is to set the $PATH variable early in ~/.profile as follows:

Code:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin
[...]

Log out and back in.

Even when the path is set as above, you should still su to root as follows in order to use root's environment:
Code:

$ su -l

friendlysalmon8827 04-29-2024 07:30 PM

The reason that certain utilities and system programs aren't directly in the root or super users' path is because the /sbin folder is a symbolic link back to the user sbin folder.

murugesandins 04-29-2024 07:50 PM

~/.bashrc or ~/.mkshrc or ~/.kshrc for all users including root
 
Quote:

Originally Posted by cmk77 (Post 6496858)
Indeed, I was using su without flags, thanks for clarifying.
- debian does have /usr/sbin in root's path (but not in a user's path)
- su without -l preserves that user's environment

if I am facing this issue:
I should have added following code inside ~/.bashrc or ~/.kshrc or ~/.mkshrc based on different OS
for all users including root user
Code:

OS=$(/usr/bin/uname -s 2>&1 | /usr/bin/sed "s/-[0-9]*\.[0-9]*\-[0-9]*//;")
case $OS in
        "Linux")
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
        "SunOS")
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
        "AIX")
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
        "HP-UX")
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
        "CYGWIN_NT")
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
        *)
                #echo use related path to grep
                if [[ -f /usr/bin/grep ]]
                then
                        EGREP="/usr/bin/grep -E"
                elif [[ -f /usr/bin/egrep ]]
                then
                        EGREP="/usr/bin/egrep"
                fi
                ;;
esac
if [[ -d /usr/sbin ]]
then
        echo $PATH 2>&1 |\
        $EGREP "^/usr/sbin:|:/usr/sbin:|:/usr/sbin$" >/dev/null 2>&1
        Ret=$?
        if [[ 0 -ne $Ret ]]
        then
                export PATH="/usr/sbin:$PATH"
        else #DEBUG01
                echo "\$PATH having /usr/sbin" #DEBUG02
        fi
else #DEBUG03
        /usr/bin/ls -ltrd /usr/sbin #DEBUG04
fi


jens 05-01-2024 03:18 PM

The user's path isn't what changed, "/usr/sbin" has always been in the root path only.
What did change is the behavior of su.

Quote:

The su command in buster is provided by the util-linux source package, instead of the shadow source package, and no longer alters the PATH variable by default. This means that after doing su, your PATH may not contain directories like /sbin, and many system administration commands will fail. There are several workarounds:

Use su - instead; this launches a login shell, which forces PATH to be changed, but also changes everything else including the working directory.

Use sudo instead. sudo still runs commands with an altered PATH variable.

To get a regular root shell with the correct PATH, you may use sudo -s.

To get a login shell as root (equivalent to su -), you may use sudo -i.

Put ALWAYS_SET_PATH yes in /etc/default/su (create it) to get an approximation of the old behavior. This is documented in su(1).

Put the system administration directories (/sbin, /usr/sbin, /usr/local/sbin) in your regular account's PATH (see EnvironmentVariables for help with this).
https://wiki.debian.org/NewInBuster

cmk77 05-02-2024 12:06 PM

Thanks jens, that really explains it


All times are GMT -5. The time now is 08:31 AM.