Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
a BASH promt to show when $USER is using su
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Duplicate Threads
View previous topic :: View next topic  
Author Message
slais-sysweb
Apprentice
Apprentice


Joined: 14 Jun 2002
Posts: 221
Location: London

PostPosted: Mon Dec 15, 2003 10:41 pm    Post subject: a BASH promt to show when $USER is using su Reply with quote

There are similar solutions on these forums and elsewhere but I thought I would give you the fruits of several days exploration of this topic!

The BASH prompt can be formated by seting the PS1 and PS2 variables in your ~/.bashrc (for an example see Gentoo's /etc/profile).
Colours can be set for different users so that, for example, the user name can be changed to red if the user is root. However if I su to root the $USER remains as the original logon, whereas the user displayed at the prompt will change to root if
Code:
PS1='\u'

The ~/.bashrc script will run whenever a shell is started, and the colours for the prompt set by either testing the value of $USER or the output of
Code:
/usr/bin/whoami

But my ~/.bashrc will not be reread when I use
Code:
su root

so my prompt, though it will show the user as root will not change to a colour coded warning that I am root. Having a /root/.bashrc will solve the first problem. but not the more general one of indicating that I have used su to change user. I could indicate the su state by displaying both users:
Code:
PS1='$USER \u'

but that is ugly.
The PROMPT_COMMAND string defines a command that is run immediately before the PS1 string is used to generate the prompt. So if that command were to change the value of the PS1 string, then the prompt could be modified before being displayed. Alternatively, the command might simply echo a string that would preceed the prompt. However, the PS1 string can be encoded to ignore the length of format strings by wrapping formatting codes in \[ \]. This ensures that the end of line wrap occurs at the appropriate place. So PROMPT_COMMAND should: assign a new value to PS1: or, write a complete line ending with a carriage return; or, hould not output anything that would displace the start of the prompt string generated by the PS1 format string.

The following extract from my ~/.bashrc accomplishes the following:
colour codes the user to distinguish root, and various trusted and untrusted users;
changes colour/highlight when the user uses su;
changes the prompt according to terminal type;

The code needs to be include in the ~/.bashrc of all the users including root.
Comments?
Code:

#!/bin/bash

 # SET PROMPT
# Define colours for PS1 including \[ no-space \] codes
# 1; = bold/bright 22 -off, 30-37 = foreground/text, 40-47 = background,
# 7 = reverse 27 -off, 4 = underline 24 -off, 8 = hidden, \007 = bell
C_REVID='\[\e[7m\]'
#C_ULINE='\[\e[4m\]'
#C_HIDE='\[\e[8m\]'
C_BLACK='\[\e[30m\]'
C_DGREY='\[\e[1;30m\]'
C_RED='\[\e[1;31m\]'
C_GREEN='\[\e[1;32m\]'
C_YELLOW='\[\e[1;33m\]'
C_BLUE='\[\e[1;34m\]'
C_PURPLE='\[\e[1;35m\]'
C_LCYAN='\[\e[1;36m\]'
C_WHITE='\[\e[1;37m\]'
C_LGREY='\[\e[0;37m\]'
C_BG_W='\[\e[47m\]'
C_BG_B='\[\e[40m\]'
C_NONE='\[\e[m\]'

# For xterms including Putty:
#       \e]2; title, \e]1; icon, \e]0; both.
#        \e = \033 (escape) \a = \007 (^G Bell)
X_TITLE='\[\e]2;\u@\h:${PWD} \a\]'
X_ICON='\[\e]1;\h \a\]'
# title for screen copied from Gentoo config
# if /etc/screenrc or ~/.screenrc includes (for xterm, rxvt etc)
#    termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
# then the $X_TITLE version appears to work ok
S_TITLE='\[\e_\u@\h:${PWD} \e\\\]'

# if using su /u in PS1 will change but not $USER
# so use PROMPT_COMMAND to set the USER colour codes
# \[ no-space \] only works in PS1 do not use with echo
case $USER in
   uczcdjc)
      # Purple
      C_USER='\e[1;35m'
      ;;
   uczcwww)
      # Purple reverse video
      C_USER='\e[7;1;35m'
      ;;
   sysweb)
      # Light Green
      C_USER='\e[1;32m'
      ;;
   root)
      # Red
      C_USER='\e[1;31m'
      ;;
   *)
      # default
      C_USER='\e[m'
      ;;
esac

function suPrompt() {
   if [ $UID = '0' ] && [ $USER != 'root' ]; then
      echo -ne "\e[7;1;31m"
   elif [ `/usr/bin/whoami` != "$USER" ]; then
      echo -ne "\e[7m${C_USER}"
   else
      echo -ne "${C_USER}"
   fi
}

# PROMPT_COMMAND
#   If set, the value is executed as a command prior formatting PS1.
PROMPT_COMMAND="suPrompt"

PROMPT_X="\u${C_DGREY}@${C_GREEN}\h:${C_BLUE}\W${C_NONE} \$ "
PROMPT_S="${C_BG_W}\u${C_DGREY}@${C_DGRAY}\h:${C_BLUE}\W${C_NONE} \$ "
PROMPT_T="${C_BG_B}\u${C_LGREY}@${C_YELLOW}\h:${C_BLUE}\W${C_GREEN} \$ "

case $TERM in
   xterm*|rxvt|eterm)
      PS1="${X_TITLE}${X_ICON}${PROMPT_X}"
      ;;
   screen)
      PS1="${S_TITLE}${X_ICON}${PROMPT_S}"
      ;;
# OSX shows up as vt100
   vt100)
      PS1="$PROMPT_X"
      ;;
   linux)
      PS1="$PROMPT_T"
      ;;
   *)
      PS1="$PROMPT_X"
      ;;
esac

PS2="\[\a\]${C_DGREY} \# continue ${C_NONE}\$ "

_________________
--
djc
sysweb SLAIS UCL


Last edited by slais-sysweb on Tue Dec 16, 2003 7:08 am; edited 1 time in total
Back to top
View user's profile Send private message
ozonator
Guru
Guru


Joined: 11 Jun 2003
Posts: 591
Location: Ontario, Canada

PostPosted: Tue Dec 16, 2003 12:44 am    Post subject: Reply with quote

Seems very reasonable, and a nice use of color -- definitely easy to keep track of who's who. It's certainly useful to have some way of being able to see at a glance which user's priviledges your shell has.

If you're curious about such things, here's an alternate approach: instead of using 'su', use 'su -' (or 'su - user'). That way, the environment will be set to what it would be if the user had logged in directly, i.e., without inheriting the environment from the user who runs su. In other words, 'su -' and you get whatever prompt is set in the root user's bash configuration (which can be customized as desired, of course). To make this the default behavior system-wide, put "alias su='su -'" in /etc/profile.

Another alternate approach of keeping track of 'su': I tend to use gnu screen in my terminals, with a hardstatusline that's always visible. The statusline shows my virtual terminals, and with a particular escape sequence in $PS1 (see the 'Titles' section in the screen man page for details), the window will be re-named automatically to whatever command is active -- if that's 'su', the window will be called 'su'. This isn't quite as flexible as your method or 'su -', since the window title will always be 'su', with no indication of which user is active inside the 'su' (typically it's root, but not necessarily).

Thanks for sharing the script! I forsee some tweaking of my bash prompt soon.... :)
Back to top
View user's profile Send private message
srlinuxx
l33t
l33t


Joined: 22 Nov 2003
Posts: 627

PostPosted: Tue Dec 16, 2003 1:20 am    Post subject: Reply with quote

I just use these lines in my .bashrc and it works whether I su or su root. These change color for root and lists the current directory, which I find quite useful.

in root's:
PS1="\[\033[1;33m\][\u@\h]:\[\033[1;34m\]\W\[\033[0m\]# "

and in user's:
PS1="\[\033[1;32m\][\u@\h]:\[\033[1;34m\]\W\[\033[0m\]$ "
Back to top
View user's profile Send private message
mlsfit138
Guru
Guru


Joined: 20 Sep 2003
Posts: 406
Location: Washington

PostPosted: Sat Dec 20, 2003 9:26 am    Post subject: Reply with quote

srlinuxx wrote:
I just use these lines in my .bashrc and it works whether I su or su root. These change color for root and lists the current directory, which I find quite useful.

in root's:
PS1="\[\033[1;33m\][\u@\h]:\[\033[1;34m\]\W\[\033[0m\]# "

and in user's:
PS1="\[\033[1;32m\][\u@\h]:\[\033[1;34m\]\W\[\033[0m\]$ "


I could just copy these lines into my .bashrc's and not worry about it, but what part of these lines defines the colors? the only difference between the two lines is after the second opening brace, but how did you know what values would equal what colors? Where in the world did you find this information? :?
Back to top
View user's profile Send private message
mlsfit138
Guru
Guru


Joined: 20 Sep 2003
Posts: 406
Location: Washington

PostPosted: Sat Dec 20, 2003 9:38 am    Post subject: Reply with quote

Thanks! It worked, but I'd still like to know why! man bash is pretty friggin long, and not the greatest read, so I hope It's not just in there.

I have another related question. At some point, either during installation, or when I created my user, a .bashrc was created for my one and only user, which aliased ls, ld, and ll (another very nice use of colors). There was no .bashrc file for root, so i copied the user's file and used that. is that a bad idea? why wasn't it in there by default? I'm just full of questions tonight. :wink:

P.S. Is there a central config file that would put this line any any new user that I made?
Back to top
View user's profile Send private message
slais-sysweb
Apprentice
Apprentice


Joined: 14 Jun 2002
Posts: 221
Location: London

PostPosted: Sat Dec 20, 2003 6:21 pm    Post subject: Reply with quote

The list of colours etc is in the code of my original post. To be more explicit the PS1 and PS2 format string codes
\[ the characters that follow are non-printing and will not count when the length of the prompt line is calculated
\] end of non-printing character string
\e[ start of colour codes
; separates colour codes
m end of colour codes
example:
Code:
\[\e[7;1;35m\]text\[\e[m\]

7 reverse video, 1 bold/bright 35 purple
\e
mlsfit138 wrote:



[quote="mlsfit138"]I have another related question. At some point, either during installation, or when I created my user, a .bashrc was created for my one and only user, which aliased ls, ld, and ll (another very nice use of colors). There was no .bashrc file for root, so i copied the user's file and used that. is that a bad idea? why wasn't it in there by default? I'm just full of questions tonight. :wink:

P.S. Is there a central config file that would put this line any any new user that I made?

put .bash_profile .bashrc .bash_logout in /etc/skel
and use
Code:
useradd -m -k /etc/skel username


as for root, you may want to use a different .bashrc with, for instance, umask 077
_________________
--
djc
sysweb SLAIS UCL
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Duplicate Threads All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum