LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   How set PATH for root user in Debian. (https://www.linuxquestions.org/questions/debian-26/how-set-path-for-root-user-in-debian-4175689666/)

gardenair 02-01-2021 11:00 AM

How set PATH for root user in Debian.
 
hi,
In my Debian 10 laptop, I am facing a problem using fdisk command which is in /sbin. My path is

Code:

test@debian:~$ su
Password:
root@debian:/home/test# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
root@debian:/home/test#

I can't execute the command
Code:

root@debian:/home/test# fdisk -l
bash: fdisk: command not found

If I set my PATH to:
Code:

root@debian:/home/test# PATH="/sbin:$PATH"
then I get
Code:

root@debian:/home/test# echo $PATH
/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
root@debian:/home/test#

After that the fdisk -l command executes perfectly. The problem is that every time I reboot my laptop, I have to set the path again. I want to add it permanently.

There is a file /etc/login.defs, This setting is for root and for normal users(if I am correct). If I use the command, in the middle of the file there is a path. Can I edit it? if it the right file? if yes then where may I add my path?

root@debian:/home/test# vi /etc/login.defs

Code:

#
# *REQUIRED*  The default PATH settings, for superuser and normal users.
#
# (they are minimal, add the rest in the shell startup files)
ENV_SUPATH      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_PATH        PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Another plase is
Code:

.profile
which is hidden
root@debian:/home/test# ls -al

Please guide me on which file may I choose to edit for permanent changes and what should I add inside it. I don't know shell scripting I just want to add the path in my root user (by switching su from normal user) so I may use fdisk or any other utility in /sbin.

Thanks in advance.

Emerson 02-01-2021 11:09 AM

Read 'man su' and then use it properly. Spoiler: plain su will not switch the environment. This is what 'su -' does.

gardenair 02-01-2021 11:17 AM

Yes if I log in through the root user then there is no issue. The thing is by using su every time I set the path root@debian:/home/test# PATH="/sbin:$PATH" I want to fix it? Is there any way?

shruggy 02-01-2021 11:26 AM

Have you read what Emerson wrote above? Then read it once again. su - is your friend.

gardenair 02-01-2021 11:33 AM

Thanks for guiding :)

jmgibson1981 02-01-2021 11:42 AM

I cheat. I just put the full $PATH in /etc/environment. It's unset there. Saves me time and I can do a regular su.

teckk 02-01-2021 11:55 AM

Files in /etc/profile.d are sourced by /etc/profile for system wide profile.

HappyTux 02-01-2021 12:57 PM

My solution to the problem, slightly edited...

Code:

root@haswell:~# cat .profile
# ~/.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

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

# set PATH so it includes sbin if it exists fscking buster change to apt/dpkg
if [ -d "/sbin" ] ; then
    PATH="/sbin:$PATH"
fi


gardenair 02-02-2021 09:27 AM

Thanks for the replay.I try but no success.Following is my .profile after adding ending three lines.
Code:

root@debian:/home/test# vi .profile
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

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

# set PATH for /sbin
if [ -d "/sbin" ] ; then
    PATH="/sbin:$PATH"
fi

Then reboot my system

Code:

test@debian:~$ su
Password:
root@debian:/home/test# fdisk -l
bash: fdisk: command not found
root@debian:/home/test#


michaelk 02-02-2021 09:58 AM

I don't think you still understand the difference between su and su -

With su you are still using the users path environment. Look no /sbin
Quote:

~# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
With su -

Quote:

~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
You need to change the path environment for your user not roots or as suggested use su - instead. You can always use the absolute path but that would require you knowing where a particular command is located.
You can logout/login instead of rebooting...

cynwulf 02-02-2021 11:17 AM

I believe /usr/local/sbin, /usr/sbin and /sbin were removed from users' $PATH at some point, in some previous release...

Just add a line similar to this to ~/.profile:
Code:

export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin
There are some possible pitfalls with using "su" instead of "su -". i.e. it can cause dotfiles within your environment to become owned by root, which is almost always a bad thing - but users tend to learn that for themselves after breaking things a few times.

Most of us have managed for years without destroying everything, so I don't personally see the issue with sbin directories being in users' paths. Because at times, one wants to see output from tools like ifconfig or whatever, without being root.

michaelk 02-02-2021 11:29 AM

As long as I've been regularly using debian /sbin has never been in a users path environment. As far as I know there is no path "standard" and while debian does not include it for a user, Red Hat does.

When root you can change everything so there is always the chance you can destroy something.

shruggy 02-02-2021 11:57 AM

Quote:

Originally Posted by cynwulf (Post 6215200)
I believe /usr/local/sbin, /usr/sbin and /sbin were removed from users' $PATH at some point, in some previous release

You probably confuse Debian with Ubuntu. Since Ubuntu by default locks root account and forces use of sudo, it overrides regular users' PATH set (by Debian) in /etc/login.def via /etc/environment.

HappyTux 02-02-2021 07:37 PM

Quote:

Originally Posted by gardenair (Post 6215159)
Thanks for the replay.I try but no success.Following is my .profile after adding ending three lines.
Code:

root@debian:/home/test# vi .profile
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

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

# set PATH for /sbin
if [ -d "/sbin" ] ; then
    PATH="/sbin:$PATH"
fi

Then reboot my system

Code:

test@debian:~$ su
Password:
root@debian:/home/test# fdisk -l
bash: fdisk: command not found
root@debian:/home/test#



You have to make certain it is being sourced from something and as has been mentioned the differences in how you invoke the shell switch. Put it in your normal user .profile too that way it will always be available.

Code:

if [ -f ~/.profile ]; then
    . ~/.profile
fi

In both of the .bashrc files and then a source ~/.bashrc to have the file re-read and the changes applied no need of a reboot. Make certain you have another Terminal window open as if you mess it up and the shell is useless you will have no way to change it as it will not load a usable shell again. You would need to boot with rescue usb/dvd and mount the partition and change it that way.

cynwulf 02-03-2021 02:08 AM

Quote:

Originally Posted by michaelk (Post 6215205)
As long as I've been regularly using debian /sbin has never been in a users path environment. As far as I know there is no path "standard" and while debian does not include it for a user, Red Hat does.

I expect you're quite correct.
Quote:

Originally Posted by shruggy (Post 6215218)
You probably confuse Debian with Ubuntu. Since Ubuntu by default locks root account and forces use of sudo, it overrides regular users' PATH set (by Debian) in /etc/login.def via /etc/environment.

No, I'm most likely thinking of Slackware, or even one of the BSD's. I haven't used 'buntu in around 13 years.


All times are GMT -5. The time now is 07:48 PM.