LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 02-01-2021, 11:00 AM   #1
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Rep: Reputation: 45
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.

Last edited by gardenair; 02-01-2021 at 11:21 AM.
 
Old 02-01-2021, 11:09 AM   #2
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
Read 'man su' and then use it properly. Spoiler: plain su will not switch the environment. This is what 'su -' does.
 
Old 02-01-2021, 11:17 AM   #3
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
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?
 
Old 02-01-2021, 11:26 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Have you read what Emerson wrote above? Then read it once again. su - is your friend.
 
Old 02-01-2021, 11:33 AM   #5
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
Thanks for guiding
 
Old 02-01-2021, 11:42 AM   #6
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
Old 02-01-2021, 11:55 AM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Files in /etc/profile.d are sourced by /etc/profile for system wide profile.
 
Old 02-01-2021, 12:57 PM   #8
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 02-02-2021, 09:27 AM   #9
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
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#
 
Old 02-02-2021, 09:58 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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...

Last edited by michaelk; 02-02-2021 at 10:14 AM.
 
1 members found this post helpful.
Old 02-02-2021, 11:17 AM   #11
cynwulf
Senior Member
 
Registered: Apr 2005
Posts: 2,727

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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.
 
Old 02-02-2021, 11:29 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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.
 
Old 02-02-2021, 11:57 AM   #13
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by cynwulf View Post
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.
 
Old 02-02-2021, 07:37 PM   #14
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gardenair View Post
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.
 
Old 02-03-2021, 02:08 AM   #15
cynwulf
Senior Member
 
Registered: Apr 2005
Posts: 2,727

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Quote:
Originally Posted by michaelk View Post
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 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grub 2 - when / why I need both in grub.cfg ; set root= and search ... --set=root ... masuch Linux - Newbie 5 07-19-2012 03:41 PM
set up java_home path in /home/user/.bashrc but use path in /usr/lib/java vitalstrike82 Slackware 4 01-13-2009 11:25 PM
Set the path systemwide/Set the path for a user with Slackware jayhel Slackware 1 06-12-2005 12:24 AM
How to set PATH How do I set PATH environment variable? Tranquil Linux - Newbie 3 11-02-2003 02:52 AM
$PATH!? how to set these PATH(s)!? sirpelidor Red Hat 5 10-25-2003 04:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration