Tuesday, November 16, 2010

Change Prompt Color when logged in as Root

All security books will recommend you not to allow root SSH logins to your Linux machines. So most of us (administrators) commonly SSH with our regular user credentials and then use "su" to escalate our privileges in order to perform root tasks. If you do this frequently, sometimes it gets a little confusing to make out if you are currently root or regular user. To determine your current status you probably you end up looking at your prompt or type in "whoami".

In this article, I will show you how to change the color of your prompt when you escalate your privileges to a super-user. This technique is a good way to remind yourself that you are holding high privileges (so don’t do anything stupid). This tip is for administrator’s convenience and helps out more if you are managing large number of Linux servers.
Step 1: Login and escalate your privileges to a Super-User

I have a regular user account (username=param) on a Linux server. So in this step, I simply login to the server and then used “su” command to escalate my privileges to a Super-User.

Step 2: Modify /etc/bashrc file

Now using your favorite editor (I will be using vim), open the file /etc/bashrc and add the lines below at the end of /etc/bashrc file. Any shell commands that you want to be executed every time a user starts up a new shell is placed in the bashrc file. We are making the change to /etc/bashrc which runs for every user and not to user’s ~/.bashrc as it wont run when user will “su” to root.

function setprompt
{
local RED="\[$(tput setaf 1)\]"
local RESET="\[$(tput sgr0)\]"
if [ `id -u` = 0 ] # check if user is root
then
PS1="$RED[\u@\h:\W]$RESET "
else
PS1="[\u@\h:\W]$RESET "
fi
}
setprompt

In the code above:

* \u means current user name
* \h means hostname
* \W means trailing component of your current directory
* tput setaf 1 means, set foreground color to RED(1)
* PS1 is the prompt string setting

STEP 3: Test it

Now while you are logged in as regular user, execute “su” and provide the password. Once logged in as root you will see the prompt color is changed to red.

This is was a very simple trick, but it comes very handy. Hope you find it useful.

Colors
Submitted by Anonymous on Wed, 2008-05-07 17:37.

Thanks for this post.

To change the forecolor to another, look this table:

setaf n
0 = Black
1 = Red
2 = Green
3 = Yellow
4 = Blue
5 = Magenta
6 = Cyan
7 = White

Greetings!
»

* reply

Normal User
Submitted by Anonymous on Thu, 2009-06-25 18:54.

So if you are a user other than root would you just use this code to make them all blue?

function setprompt
{
local RED="\[$(tput setaf 4)\]"
local RESET="\[$(tput sgr0)\]"
if [ `id -u` != 0 ] # check if user is not root
then
PS1="$RED[\u@\h:\W]$RESET "
else
PS1="[\u@\h:\W]$RESET "
fi
}
setprompt
»

* reply

Normal users and root colors
Submitted by Anonymous on Tue, 2009-09-01 13:17.

To use blue for regular users and red for root you can do
the following. This also disables the coloring when no terminal
is used e.g. when you do a scp (secure copy).


function setprompt
{
if [ $TERM != "" ]
then
local RED="\[$(tput setaf 4)\]"
local BLUE="\[$(tput setaf 2)\]"
local RESET="\[$(tput sgr0)\]"
if [ `id -u` = 0 ] # check if user is not root
then
PS1="$RED[\u@\h:\W]$RESET "
else
PS1="$BLUE[\u@\h:\W]$RESET "
fi
fi
}
setprompt

No comments: