7

I'd like to make happen in RHEL/CentOS 7.6

anytime you do su and become root I want the terminal prompt color in that terminal to become red for the duration of that su session. Type exit to go back to being whoever you were and I want the prompt color to go back to whatever the previous color was (black).

Same for an SSH window using putty logged in over the network: initially ssh in as user and have a default white shell prompt; do an su to root and I want the prompt to become red; type exit I want the prompt to go back to white.

so far i did this but it's not working 100%, the color stays red after you type exit and leave the su session and go back to being user.

/etc/profile.d/red_root_prompt.sh

if [ $UID -eq 0 ]; then
   PS1="\e[31m[\u@\h \W]# "
else
   PS1="[\u@\h \W]# "

Is there a way to make things happen the way I want? I only want it for bash shells.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
ron
  • 5,749
  • 7
  • 48
  • 84
  • Are you typing `su` or `su -`? I’d have thought the prompt would return to what ever it was before you elevated if you used the latter. – bxm Nov 07 '19 at 23:09
  • i only ever do just `su`, and the color thing has always worked in SUSE. – ron Nov 08 '19 at 12:53
  • You probably want to use `tput` rather than embedding terminal-specific escape codes that will make a mess when you use some other terminal type. And don't forget `\[…\]` around non-spacing output, or Bash will miscalculate line lengths. – Toby Speight Jul 22 '23 at 17:38

2 Answers2

11

You can either add this to /etc/bash.bashrc or edit /etc/profile/

force_color_prompt=yes

    if [ "$LOGNAME" = root ] || [ "`id -u`" -eq 0 ] ; then
        PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;34m\]#\033[00m\] '
    else
        PS1='\u@\h:\w\$ '
    fi

and you will get a prompt that looks like this. If you use a white background, change the last part after # to \033[01;30m\] so you can see the command text. I included that as a second example for reference .

Visual appearance

also if you add the following to \etc\bash.bashrc or ~/.bashrc:

export col_white='\033[00m'
export col_black='\033[01;30m'

export col_red='\033[01;31m'
export col_green='\033[01;32m'
export col_yel='\033[01;33m'
export col_blue='\033[01;34m'

You will be able to do :

$ echo -e $col_red red $col_blue blue $col_yel yellow $col_green green
 red blue yellow green

with the output looking like this:

enter image description here


EDIT: For some reason using variable expansion for the prompt breaks the carriage return (it locks it to the length of the variable, i.e. pushes it forward by n-many spaces corresponding to echo $col_blue,echo $col_white, and I have not found a good solution for this as of this moment, but using proper bracketing without variable substitution as above solves this issue.

if [ "$LOGNAME" = root ] || [ "`id -u`" -eq 0 ] ; then
    PS1="$col_red\u@\h:$col_purple\w$col_green# $col_white"
else
    PS1="\u@\h:$col_blue\w$col_yel\$ $col_white "
fi
NetIceCat
  • 2,244
  • 1
  • 14
  • 25
  • I could achieve my favorite setting (similiar to yours but without the carriage return misrenderering or whatever) with this: `if [ "$LOGNAME" = root ] || [ "`id -u`" -eq 0 ] ; then PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;29m\]# ' else PS1='\u@\h \w : ' fi` Plenty of trial and error on my behalf though; so please don't rely for certain on it. – steffres Jul 27 '20 at 11:47
3

for RHEL/CentOS 7.x I thought customized stuff like this was supposed to be under /etc/profile.d or in something like /etc/bashrc.local. That way any customization is preserved after updates when something like /etc/bashrc is might be changed or replaced.

for RHEL/Centos 7, I thought one should only need to put a file under /etc/profile.d/ having the corresponding suffixes for the shells they are for,

so I did /etc/profile.d/redrootprompt.sh having only

if [ "$LOGNAME" = root ] || [ "`id -u`" -eq 0 ] ; then

    PS1='[\[\033[01;31m\]\u@\h\[\033[00m\]:\w] : '

else
    PS1='[\u@\h \w] : '
fi

and this works great giving the look i wanted

red root prompt

it also works in a putty window which has black background and white text, but goes red after su to root and back to white on exit.

ron
  • 5,749
  • 7
  • 48
  • 84
  • As far as config, if you look at `man bash` there are different sets of configuration files that bash reads in depending on the mode of invocation. Both `/etc/bash.bashrc` and `~/.bashrc` correspond to `bash` invoked in interactive mode, which is the default mode as long as your default shell is invoked as `sh`. The `/etc/profile` and `~/.profile` files get read in when `bash` is invoked as a login shell and if you run `echo $BASHOPTS` , it will have `login_shell` as a listed parameter. – NetIceCat Nov 08 '19 at 17:29