Submitted by jean-paul on Sat, 07/28/2012 - 20:15

As an Ubuntu user, I don't usually go to a root Bash prompt. It's actively discouraged by disabling root's login. That doesn't mean you can't get a root shell, but you have to be logged in as a regular user first. The way I do it is by issuing a "sudo su" at my normal prompt. This changes my $HOME environment variable to root's home (what I want), while not actually changing to root's home directory (also what I want). The latter point is where "sudo su" differs from "sudo -i".

When I switch to root, I usually have more than one terminal open, and I am sometimes a bit nervous that I will give a command that was intended for a non-root terminal, so I decided to add some colour to my root prompt. It was a fairly simple change, although if you aren't using Ubuntu and Bash, your files might be a bit different than mine.

First, I switched to root and navigated to /root which is root's home directory. I edited .bashrc (note the "." before the b) with vim, and I looked for the following line:

#force_color_prompt=yes

It was on line 39 for me. Remove the octothorpe (#) to enable the option.

Then look a bit farther down for the following lines:

 

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 
It's less complicated than it looks! The colours in Bash are set with numbered pairs, separated by a semi-colon. In the default config, the two colours are set to 01;32 (light green) for the first part of the prompt, and 01;34 (light blue) for the last bit. The first number in the pair can either be 00 (normal colour) or 01 (light colour). I wanted the first part of the prompt to be red, which I thought would stand out, so I changed 01;32 to 01;31. Below is the changed section:
 
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 
If you want to make yours a different colour, just use the following colour chart:
 
ColourCode
Black0;30
Red0;31
Green0;32
Brown0;33
Blue0;34
Purple0;35
Cyan0;36

 

 

 

 

 

 

Remember to change the 0 to a 1 if you want a lighter or brighter colour!