LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Issue with different PATHs for root when on non-login vs login shells in LFS build (https://www.linuxquestions.org/questions/linux-newbie-8/issue-with-different-paths-for-root-when-on-non-login-vs-login-shells-in-lfs-build-4175665448/)

bionor 12-05-2019 06:53 AM

Issue with different PATHs for root when on non-login vs login shells in LFS build
 
Hey guys :)

First let me start off by thanking the people behind the LFS project. What an amazing project. I've learned so much doing this. I've completed the LFS book and is basically done with the BLFS as well. I've been following the LFS v9 System V and am running GNOME. I have installed Firefox, Libreoffice, VLC, Transmission and a whole lot of other stuff. My build is nearly worthy of being a daily driver as far as my laptop goes.

I have a few various small issues though. One of them is that the $PATH for my normal user and root are not the same with the path for my normal user being as it should, but root has a much smaller path variable, but only when using a non-login shell like Gterm. When I login to a TTY, like if I don't boot into the GUI, all the paths are correct.

I understand this is due to all the various bash scripts, and there are a number of them. To me this seems slightly more complicated than it needs to me.

To the best of my knowledge it seems like the scripts in /etc/profile.d are not executed for the root user when in GNOME and Gterm as it seems the missing paths are the ones found there.

My /etc/profile:
https://pastebin.com/7tJpsuDU

My /etc/bashrc:
https://pastebin.com/9FrX3hkn

An example from /etc/profile.d/extrapaths.sh:
https://pastebin.com/PMuFYVRv

The /usr/local/sbin path found here is missing when running the non-login shell from GNOME.

The ~/.bash_profile of root:
https://pastebin.com/vV85FNWg

The ~/.bashrc of root:
https://pastebin.com/FZAChB7x

The ~/.profile of root:
https://pastebin.com/VrBRx8hn

All the home directory scripts are identical for my normal user.

Output of paths for normal user:
Code:

bio@bioLinux [~]# echo $PATH
/opt/rustc/bin:/usr/local/bin:/bin:/usr/bin:/opt/ant/bin:/opt/jdk/bin:/opt/qt5/bin

Output of paths for root:
Code:

root@bioLinux[ /home/bio]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin


scasey 12-06-2019 01:50 AM

I don't usually expect the PATHs of normal users and root to be the same. Yes, often a non-privileged user has a longer path.

Is this causing a problem for you? What is the problem?

You can modify the root path to be what you want in root's .bash_profile or .bashrc.

pan64 12-06-2019 02:38 AM

Quote:

Originally Posted by bionor (Post 6064895)
One of them is that the $PATH for my normal user and root are not the same with the path for my normal user being as it should

what do you think, how should it be set?
Quote:

Originally Posted by bionor (Post 6064895)
but root has a much smaller path variable,

yes, this is normal, and not only in LFS, but in [almost] any other distro
Quote:

Originally Posted by bionor (Post 6064895)
but only when using a non-login shell like Gterm. When I login to a TTY, like if I don't boot into the GUI, all the paths are correct.

You need to understand the difference between the login shell and non-login shell, also the difference between interactive and non-interactive shell.
Speaking about bash, you can see the man page (see section invocation) and you will see how are the different files are used (during start of the shell).

Quote:

Originally Posted by bionor (Post 6064895)
I understand this is due to all the various bash scripts, and there are a number of them.

I do not really understand how is it related to PATH.

Quote:

Originally Posted by bionor (Post 6064895)
To the best of my knowledge it seems like the scripts in /etc/profile.d are not executed for the root user when in GNOME and Gterm as it seems the missing paths are the ones found there.

gnome and other terminals execute the shell as non-login shell, but probably that can be configured.

bionor 12-06-2019 04:26 AM

Quote:

Originally Posted by pan64 (Post 6065226)
what do you think, how should it be set?

Root lacks several sbin paths for instance, but only when logged into gnome and using gterm. The reason I found out about this is that I installed OpenVPN which installed into /usr/local/sbin which was not in root's path when logged in via a non login shell. Then I found out it's there if I log in directly to a console from boot. There might be supposed to be a difference between normal users and root, but not for the root user itself depending on whether you're on login shell or not, except perhaps in special cirumstances.

I've managed to resolve the issue for now though by copying this from /etc/profile into "~/.bashrc":
Code:

for script in /etc/profile.d/*.sh ; do
        if [ -r $script ] ; then
                . $script
        fi
done

As I mentioned, it seems that when using a non-login shell it doesn't read /etc/profile, which as I understand it, it should, or it could be that some of the later scripts overrides it. /etc/profile should be the first script to be read, independently of whether one's on a login or non-login shell, if I'm correct.

Quote:

Originally Posted by pan64 (Post 6065226)
You need to understand the difference between the login shell and non-login shell, also the difference between interactive and non-interactive shell.

Yes, I do. I tried to inform of that by specifying that the difference seems to be between login shells and non-login shells, but I could have made that clearer perhaps.

EDIT: I've changed the title of my OP to better reflect the actual issue.

MadeInGermany 12-08-2019 02:45 AM

It's a pity that graphical logins no longer behave like logins.
They do not run /etc/profile and .profile
These should run once - at login.
Some distros take care, e.g. by running /etc/profile from /etc/bash.bashrc
If that's not the case in your distro then I suggest to run /etc/profile in your .bashrc
The /etc/profile should run the /etc/profiles.d/ files.
Code:

if [ -z "$MY_PROFILE_RUN" ]
then
  export MY_PROFILE_RUN=1
  . /etc/profile
fi

The environment variable ensures that it is run once.
(Some distros have such a variable in /etc/profile ...)

bionor 12-08-2019 04:23 AM

Quote:

Originally Posted by MadeInGermany (Post 6065729)
It's a pity that graphical logins no longer behave like logins.
They do not run /etc/profile and .profile

Oh really? Well that explains it. I was told /etc/profile was the first one to be run independently of login vs non-login. Thanks! I'll mark it as solved now.

pan64 12-08-2019 04:49 AM

Quote:

Originally Posted by bionor (Post 6065739)
Oh really? Well that explains it. I was told /etc/profile was the first one to be run independently of login vs non-login. Thanks! I'll mark it as solved now.

That is false.
Quote:

(man bash)
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists.
During graphical login there will be no bash invoked as interactive login shell. But anyway, you can tell your terminal to run bash with --login.


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