Skip to end of metadata
Go to start of metadata

Colouring Prompts to Differentiate Users

Category: Getting Started &nbsp

Command prompts are something that most of the big distributions have standardised these days. With BASH as the default shell with its highly customisable command prompt, it’s a familiar and common sight, reminding both who you are and where you are with the format of username @ hostname : current working directory.

Terminated with the dollar sign, like the Bourne Shell before it, it’s a prompt that you will see in many Linux distributions. This isn’t all the prompt can do though. In fact it’s highly customisable and capable of displaying a lot more information.

The key to the BASH prompt’s customisation is in the default resource file that is loaded as you log in.  .bashrc is a file that is stored in each user’s home directory and contains information about any customisations or configurations relevant to the user’s BASH shell session regardless of the terminal used for that session. Many distributions come with a lot of settings pre-configured for BASH, for example Debian and related distributions such as Ubuntu, have settings to provide a colourful BASH prompt by amending the following line…

#force_color_prompt=yes

Removing the hash (#) at the start of the line will tell BASH to use a coloured prompt as defined further on in the file. This default is green text with the current working directory in blue. In contrast, CentOS provides no such simple activation of a coloured command prompt and a user’s .bashrc file simply tells the system to load defaults from /etc/bashrc.

Amending your user prompt and setting your own colour preferences can make it much easier to spot that you are using your own user account, and there are a variety of colours that can be used to amend the prompt if you don’t like the defaults.  Whilst this isn’t helpful for much if you only use the one account (unless you are the sort of person that loves tweaking your settings) this can be very helpful if you are administering a system and are regularly alternating between different user accounts for spotting whether you are using your account or a different one.

In one case it’s quite important to be able to identify that you are logged in as a particular user. This is when you are logged in as root. As the root user has the power to make global changes on a system, including some that can break the system, it’s helpful to be reminded when you are logged in with that user account.

It may sound silly, but I’ve encountered a number of stories of mistakes made when logged in as root leaving systems damaged because a user wasn’t careful as they’d forgotten which user they were logged in as. Making the use of the root account more prominent along with warnings is something I’ve seen used in a number of places, this is usually performed by making the initial part of the prompt bright red. This is easily achieved by adding the following line to the end of the root users .bashrc file, normally /root/.bashrc

PS1=’\[\033[0;31m\]\u@\h\[\033[00m\]:\w\$ ‘

What this is doing is setting the PS1 variable (used by BASH to denote how the command prompt should appear) to initially be red (\[\033[0;31m\]), show the username (\u), the @ symbol, the hostname (\h) then change colour back to default (\[\033[00m\]), a colon, the working directory (\w) and finally the dollar symbol (\$).

For more details on colours and available options for your prompt, the Arch Linux wiki has a handy table of options here https://wiki.archlinux.org/index.php/Color_Bash_Prompt

Save this article