LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 10-29-2015, 12:22 AM   #1
nelsonm
LQ Newbie
 
Registered: Oct 2015
Location: Winnipeg
Distribution: Slackware, GalliumOS,
Posts: 21
Blog Entries: 2

Rep: Reputation: 7
su vs .bashrc


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. :-)

Last edited by nelsonm; 10-29-2015 at 12:27 AM. Reason: I thought I caught all the typos.
 
Old 10-29-2015, 02:50 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
The shell executes the commands found in a script in sequence, from top to bottom.

So, when it finds "source /etc/profile.d/coreutils-dircolors.sh" $PATH has not yet been modified, that's why it doesn't find "fortune."

Try sourcing /etc/profile.d/bsd-games-login-fortune.sh after having added /usr/games to $PATH in ~/.bashrc, it works.

Last edited by Didier Spaier; 10-29-2015 at 09:29 AM. Reason: s/founded/found/
 
1 members found this post helpful.
Old 10-29-2015, 08:06 AM   #3
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
You have to su like this to get the correct environment:

Code:
$ su - <newuser>
Note the "-".
 
1 members found this post helpful.
Old 10-29-2015, 08:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
see the man page of pash, look for invocation
 
Old 10-29-2015, 10:14 AM   #5
nelsonm
LQ Newbie
 
Registered: Oct 2015
Location: Winnipeg
Distribution: Slackware, GalliumOS,
Posts: 21

Original Poster
Blog Entries: 2

Rep: Reputation: 7
Of course! I feel silly for not thinking of something so simple as order of operations. Thanks Didier!
kikinovak, that is absolutely valid advice for normal use, however it would not have exposed my inherently broken .bashrc
and pan64, I'm reading that man page right now, I spent so much time with man su that I didin't even think to man bash!

Last edited by nelsonm; 10-29-2015 at 10:18 AM.
 
Old 10-29-2015, 10:53 AM   #6
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Don't worry, I have spent hours if not days missing things even simpler.

"man bash" is hard to read in my opinion, partly because it implicitly assumes that the reader already knows the POSIX specification for Shell & Utilities.

As the latter is, still in my opinion more accessible I advise you to first read in it the chapter Shell Command Language that also provides examples that completely lack in the bash manual and help understanding the syntax.

This will also help you understand which among bash features are extensions to the POSIX specification that you won't find in some other shells.
 
Old 10-29-2015, 12:49 PM   #7
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
Quote:
Originally Posted by nelsonm View Post
kikinovak, that is absolutely valid advice for normal use, however it would not have exposed my inherently broken .bashrc and pan64, I'm reading that man page right now, I spent so much time with man su that I didin't even think to man bash!
Here's mine. Keep it simple.

Code:
# ~/.bashrc

# PATH
PATH=$PATH:/sbin

# Alias
alias ls='ls --color=auto'
alias ll='ls -al'
alias ..='cd ..'
alias ...='cd ../..'
alias cp='cp -i'
alias rm='rm -i'
alias mv='mv -i'
alias vi='vim'
alias uman='GROFF_ENCODING=utf8 man'

# PS1
GREEN='\[\033[0;32m\]'
WHITE='\[\033[1;37m\]'
NC='\[\033[0;m\]'
PS1="$GREEN[$WHITE\u$NC@$WHITE\h$NC:$WHITE\W$GREEN] \$ $NC"

# Vim
EDITOR=vim
VISUAL=$EDITOR
export EDITOR VISUAL
 
1 members found this post helpful.
  


Reply

Tags
environment variables, interactive shell, non-interactive shell, su



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
[SOLVED] /etc/bashrc ,dircolors and /root/.bash_profile ,/.bashrc not exist in LFS-7.5(sec9.3) jaassi Linux From Scratch 2 09-20-2014 05:04 PM
~/.bashrc, /etc/bash.bashrc files not read? Tachtory Slackware 3 01-04-2014 12:25 AM
[SOLVED] How to use my /home/kangjoo/.bashrc rather than root/.bashrc kangjoo.lee Linux - Newbie 2 11-05-2012 03:38 PM
.bashrc / .bash_profile /etc/profile /etc/bashrc deadeyes Red Hat 2 02-13-2010 11:22 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM

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

All times are GMT -5. The time now is 08:37 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