hey folks, I've been playing with and trying to wrap my head around the various ways a person can switch users, and the different types of environments that they get. Specifically the differences between sudo and su for both users and root, and how the different methods of user switching affect the $PATH
As it stands, and I'm sure I'm putting out common knowledge here, when I log in to the computer (graphical boot), /etc/profile is parsed to get me my regular $PATH that i know and love.
However, if I log in as a particular user and then use 'su someotheruser' my path gets changed to the default set by /etc/login.defs. Ostensibly, if I have something in my .bashrc it should be able to modify that $PATH when I use 'su' and I can, but the order of operations seems off to me. Here's an example as well as my .bashrc to demonstrate what I'm getting at.
Code:
Gauls! We have nothing to fear; except perhaps that the sky may fall
on our heads tomorrow. But as we all know, tomorrow never comes!!
-- Adventures of Asterix.
nelsonm@slacker:~$ cat .bashrc
#!/bin/bash
# .bashrc for Nelson M.
#
# .bashrc is executed for interactive non-login shells. When you've
# already logged in to the machine and are opening a new terminal window
# in KDE, MATE, et al.
# I'm taking a few things from /etc/profile for this type of shell.
# `source`-ing the profile.d scripts because I'm lazy, maybe it's wrong.
# If anyone thinks to correct me I'll happily change my foolish ways :-)
# I like having fortune run when I open a terminal, it's cute!
source /etc/profile.d/bsd-games-login-fortune.sh
# I like colours.
source /etc/profile.d/coreutils-dircolors.sh
# I don't like the full path in my PS1, but the current directory name
# is definitely nice! (capital vs lowercase "W")
export PS1="\u@\h:\W\$ "
echo $PATH | grep /usr/games 1> /dev/null 2> /dev/null
if [ ! $? = 0 ]; then
PATH=$PATH:/usr/games
fi
Code:
nelsonm@slacker:~$ su nelsonm
Password:
bash: fortune: command not found
nelsonm@slacker:~$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/games
nelsonm@slacker:~$ which fortune
/usr/games/fortune
nelsonm@slacker:~$
as you can see, while my $PATH was modified to include /usr/games the non-login shell that I called did not find the fortune app or run it as expected.
I should mention that my .bash_profile does
not source .bashrc because I like the idea of keeping things separate.
I'm not calling this a "problem" per se, I'm just curious since it didn't align with my expectation. I know that there are many ways to switch between users in a terminal shell and that using `su - nelsonm` would give the full path as expected for a login shell, but I'm wondering why this behaviour is the way it is, and if there's any way to change that behaviour to meet my expectation.
from what I have found online and compared with Slack, some versions of su support an /etc/profile.d/su config file that supports a flag to always initialize PATH, but that's not supported in the login.defs file, and I don't actually know if that's even what I'm looking for since I cant experiment.
Anyways, thanks for reading. My co-workers always complain that I'm too verbose when I'm writing emails and the like. :-)