Walker News

Customize Linux Command Prompt To Differentiate Between Live And Test Servers

It’s not uncommon a Linux administrator initiate remote connection to test server and live servers concurrently, especially when migrating settings or data from test server to production machine.

To avoid any mistakes that could bring disaster, sysadmin has to be very alert on which remote session he/she is working on. Otherwise, settings that should be applied in test server may accidentally deploy to live server before UAT passed, or vice-versa.

If you’re such a sysadmin who deal with Linux command shell at most of time, isn’t it better to customize the command shell prompt of test server. For example, change the colour of the Linux command shell prompt to appear with outstanding visual effect, so that you could easily identify a test server and live server when switching Putty windows.

How to change colour of Linux command shell prompt?

How to change the colour of Linux command shell prompt and highlight it?Linux command shell prompt is defined by $PS1 environment variable, which the variable content can be viewed by executing echo $PS1 or env | grep PS1 or set | grep PS1.

By default, Red Hat Linux define $PS1 as [\u@\h \W]\$, which means the Linux command shell prompt appears with pattern [user@hostname directory].

So, we have to insert the terminal colour code into the $PS1 environment variable. For example, execute this command (or append it to .bash_profile):
export PS1="\\e[1;33;44m[\u@\h \W]\$\\e[m "

not only change colour of text but also highlight the text of Linux command shell prompt, as shown in the screenshot (above).

As you could compare the original and tweaked $PS1 variable value, the different is the prefix
\\e[1;33;44m

and the suffix
\\e[m

Where the \\e[ indicate the start of “colour code” and m character is to indicate the end of “colour code”.

The suffix \\e[m simply tells Linux command shell to stop using the earlier “colour code” and revert it back to original terminal colour.

The “colour code” can be in one of these patterns – x;y (to change the colour of text only) or x;y;z (to change the colour of text and highlight it).

Some of the valid x;y colour code are 0;30 (black), 0;31 (red), 0;32 (green), 0;33 (brown), 0;34 (blue), 0;35 (purple), 0;36 (cyan), 0;37 (light gray), 1;31 (light red), 1;32 (light green), 1;33 (yellow), 1;34 (light blue), 1;35 (pink), 1;36 (light cyan), 1;37 (white).

And the z value can be ranging from 40 up to 47, inclusively. For example, 40 is black, 41 is red, 42 is green, etc.

Custom Search

2014  •  Privacy Policy