Use A Different Colour For The Root Shell Prompt
Linux only: Reader Chris writes in with an excellent tip that changes the prompt to red when using the root account from the terminal—as a reminder to be more careful.
Using the tip is relatively simple—just edit the /root/.bashrc file and add in the following, preferably commenting out the existing lines that set the colour, though you can simply add this line to the end of the file.
PS1=’${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;34m]w[33[00m]$ ‘
Once you’ve added this line, anytime you switch to using the root shell you will see the prompt in red with white text for the command line. Chris takes it further, with a line that turns the prompt green for regular users, which you can enable by adding the following to your ~/.bashrc file:
PS1=’${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ ‘
This tip can really come in handy if you have a bunch of terminal windows open at once, so you can tell at a glance which ones are using root mode and which aren’t. Thanks, Chris!
For more tips on powering up your terminal, check out how to display your public IP address, show a list of only subdirectories, or make any directory into an ISO file.
- Next Post: Purple Moods And Glowing Cubes »
- « Previous Post: Add Custom Printed Pages To Your Moleskine
Comments (AU Comments | US Comments)
I thought most distros do this already. On gentoo it already turns it red.
hadees
I always use sudo!
shashank_hi
@shashank_hi: sudo doesn't always work. (modprobe for example)
shinchan
@hadees: There are so many distros, you may be correct. It didn't do it by default for my Ubuntu or Red Hat boxes that I use daily.
Either way, it's a useful tip if you want to customize the colors to something different, and I'm enjoying reading through tips from the readers. =)
@shashank_hi: Do you have ssh-access for your account? If so, do you realize you're effectively giving root ssh-access, at least for anything authorized in /etc/sudoers.
jokono
Linux Mint does this as well.
MadelineAntlion
I commented on another thread about prompts a while back, but I think it bears repeating, considering this is exactly on topic with what i'd said before. I use colored prompts with tcsh, where the each of the various hosts I have access to are different colors, and the prompt changes to yellow on a red background if I'm root (which I do with 'sudo tcsh' instead of actually becoming root). The prompt is also screen-aware, and will show the current window, like this:
0-kestrel [Thu 02Apr2009] 13:57 ~ 3590>
And here's the code from my .tcshrc:
if ($?prompt) then
set host=`/bin/hostname -s`
switch ($host)
case eagle:
set promptcolor="^[[1;31m"
breaksw
case kestrel:
set promptcolor="^[[1;36m"
breaksw
case sparrow:
set promptcolor="^[[1;33m"
breaksw
default:
set promptcolor="^[[1m"
breaksw
endsw
set prompt="%{$promptcolor%}%m%{^[[0;39m%} [%d %D%w%Y] %T %c %h%# %L"
if ($USER == "root") then
set prompt="%{$promptcolor%}%m %{^[[33;41m%}[%d %D%w%Y] %T %c %h%#%{^[[0m%} %L"
endif
if ($?WINDOW) then
set prompt="$WINDOW-$prompt"
endif
endif
Enjoy!
David Goodwin
debian_chroot is meaningless on my slackware box...
Alden Pyle
@David Goodwin: That's a very interesting idea. I like it!
@hadees: It is also the default behavior on openSUSE... But, I think I'll try to change the color of ssh-ed prompt (not sure how should I do though).
For Fedora users, add this to the bottom of /root/.bashrc
# Make root's prompt red
PS1="\[$(tput setaf 1)\][\u@\h \W]$\[$(tput sgr0)\] "
jokono
A life saver for me is changing the background of production or critical servers. I use PuTTY for terminal sessions, and the standard background is black, but on the production/critical servers, I've changed it to blue. Another simple reminder to be more careful, even if you're not root.
Ronald Bruintjes
@David Goodwin: Hey look! A real geek!
Alden Pyle
Wasn't there another tip Lifehacker published a while ago about changing the window theme for root windows in GNOME? I've never been able to find it since...
Great tip although I wish when things like this were suggested, the syntax were explained a bit more. This is lifehacker, after all.
I fail to see how debian_chroot would work across all linux distros and I myself use OS X mainly and have modified my shell colors before and have had to tweak my .bashrc files across boxes that use different variants of *nix.
spittingangels
@Alden Pyle: Thanks! :)
David Goodwin
I was gonna set up this tweak, but then I saw this line in my .bashrc
# uncomment for a colored prompt, if the terminal has the capability; turned off by default to not distract the user: the focus in a terminal window should be on the output of commands, not on the prompt
And I thought "my, that's true!
@Alden Pyle:
same here (Arch)
BrianB
@Yab: In a way, though, you could argue that coloring the prompt helps to separate the prompt from the commands and output, thus helping readability :D
Colors in sh seem like a bad idea for some reason. The # or $ should be enough for anyone to know which root/other uid they are using.
I do have a custom PROMPT on my 2 main boxes that map exactly to what scp wants.
username@hostname:/full/path/to/cur/dir$
There doesn't seem to be anything special to get this under bash provided you use xterms. The default .bashrc includes it. Getting the same prompt under tcsh, the one true shell, is a little more complex.
TheFu
Windows CLI users can easily do this by creating a shortcut to "C:\WINDOWS\system32\cmd.exe", placing the shortcut where it would be useful, clicking it, right-clicking on the header bar of the CLI window, choosing Properties, customizing the colors, etc.
When done, click OK and you'll be prompted to apply "to this instance or to the shortcut that started this instance" - chjoose the latter to make that shortcut a custom font / color / etc.
I made a number of icons and shortcuts that give me various frequently-used commandline functions - adding runas, psexec, net use, etc. functions inline w/ the shortcut.
@ David Goodwin:
I have a situation at work where I ssh into plenty of servers and desktops. (tcsh)
I normally work at one of 4 workstations.
I bow down do your geekitude and humbly ask that you help me make it to where my prompt will change to purple when I am on any host that is not the 4 workstations.
*bow*
BTW,... I can not reply to any posts, only post new.
Anyone else having that problem?
CoHPhasor
@eternicode:
That's my view too, and precisely why I use a coloured prompt. I can see exactly where my commands are and where the results are at a quick glance.
Paul Graydon
In my bash_profile that was passed out to every *nix machine I logged into was the following PS1:
PS1="\[\033[1;32m\][\t \u@\h \w]\\$\[\033[0m\] "
Gives me the time, user name @ server name and path. I'd be logged into several machines at once, and switching between boxes regularly. This way I could quickly and easily see exactly where I was and what access level I was on, and most importantly exactly where I was.
e.g.
[15:04:13 pgraydon@Monkey ~/cpp]$
It was also great when I used to log into managed hosted servers and work on something for customers. They could see the timescales of the task from screenshots or copy & pasted text which always helps prove their problem was actively being worked on, not just when they chased :)
Paul Graydon
I prefer doing it like this:
DEFAULT='0;39m'
PROMPT_COLOR=`echo $DEFAULT`
if [ ${UID} -eq 0 ]; then
PROMPT_COLOR='37;41m'
fi
## Prompt w/ date, color if root, deb chroot
PS1=' \[\e[${PROMPT_COLOR}\]\u@\h\[\e[${DEFAULT}\]${debian_chroot:+.$debian_chroot}:\w\$ '
And I've aliased su to 'su -m' which maintains my environment (I have a *lot* of aliases and functions that I don't like losing when I su). Now I don't have to change root's settings to get my funky colored prompt. Not a big deal on a home box, but I share some servers with other admins at work who wouldn't appreciate me changing root's rc files. Oh and if you ever don't want to use su -m, just ignore the alias by calling \su.
valadil
@BnWRainbow: You could check for the environment variable SSH_CONNECTION. Maybe something like:
-----
if [ -n "$SSH_CONNECTION" ]; then
PS1="something"
else
PS1="something else"
fi
-----
-Jason
jasonp
reminds me of my BASIC days... man, I was a geek since 8...
ridgecity
@Alden Pyle: lol, here's to the L33T!
ridgecity
Why would you say "Linux only" instead of "bash only with a ANSI supporting terminal".
Aron Rubin
this is new and/or news?
been doing this for at least 10 years
cotdagoo
Making the root prompt fancy colors doesn't save you from yourself. Learning to install and configure Linux, Solaris, AIX, and other *NIX systems is way more about having experience, knowledge and wisdom about what you're doing.
zaka
The best bash prompt ever: PS1="\[\e[36;1m\]\u\[\e[31;1m\]@\[\e[32;1m\]\H \[\e[31;1m\][\w] \n \[\033[1;33m\]?\[\033[0m\] "
AbelJebbies
I noticed that the above trick put the entire path of the working directory in the shell prompt. Which part of the code controls this? How to change this to default and only get the color part working.
bobby953
See this http://michsan.wordpress.com/2007/03/12/fancy-colorful-shell/
WinstonHans