In this short tutorial I am going to show you how to change your bash prompt when you login through SSH permanently.

Every user with their own directory should have their own /$HOME/.bashrc file and that is where the prompt configuration is stored. To change the bash prompt you need to edit the ~/.bashrc file for each user manually. E.g The root bash prompt is stored in /root/.bashrc

If you only customized the bash prompt and want the same format you can always copy the bash prompt from a user with the same profile to another user.

You can check out my youtube video on that.

  1. Let me show you how you can customized the Bash Prompt for the user demo

Edit the ~/.bashrc (/home/demo/.bashrc), you can use any editor to do it. In this example I am using nano. Locate the prompt section. Make the necessary changes and save the changes.

Here is an example of changing the color and adding new line to the prompt.

PS1=’${debian_chroot:+($debian_chroot)}\n[\033[01;31m]\u[\033[00m]@[\033[01;32m]\h[\033[00m]:[\033[01;34m]\w[\033[00m]\n\n\$ ‘

Once you completed the change you can issue the command source </user folder/.bashrc> file.

Some of the common options that are available for customizing bash prompt.

OptionsDesciptions
\uDisplay the current user. When you issue the whoami command the user is what will be displayed if you use this option. echo $USER will show you the current user as well.
\hDisplay the hostname. echo $HOSTNAME will show you the current server hostname up to the first . DOT if you have specified a FQDN for your hostname
\HThe whole hostname e.g dracocyberlinux.com including .DOT if you have setup a FQDN as your hostname
\wThe current working directory
\WThe basename of $PWD
\$If you are not root user you will get the $ inserted to the end of the prompt. If you are root user you will get #
\nAdd A new line
\dThe date, in “Week Month Day”
\sThe name of shell e.g bash
\jNumber of Jobs currently managed by the shell
\tThe time, in 24 hour format
\TThe time, in 12 hour format
\@The time, in am/pm format
\vversion of Bash
\VThe release of Bash, version, patch level
\[Begin a sequence of non-printing characters. This allows bash to calculate word wrapping correctly. Commonly use in color escape sequences. E.g \[033\[01;32m\] = Bold Green.
\]End a sequence of non-printing characters.

Now let’s look at the basic Foreground color options. You can replace \e with \033 if you are using the escape sequence.
The \[\033[00m\] options ends the color for the range.

OptionsDescriptions
\e[0;30m Dark Gray
\e[1;30mBold Dark Gray
\e[0;31mRed
\e[1;31mBold Red
\e[0;32mGreen
\e[1;32m Bold Green
\e[0;33mYellow
\e[1;33mBold Yellow
\e[0;34mBlue
\e[1;34mBold Blue
\e[0;35mPurple
\e[1;35m Bold Purple
\e[0;36mTurquoise
\e[1;36m Bold Turquoise
\e[0;37m Light Gray
\e[1;37mBold Light Gray

Now let’s look at the basic Background color options. You can replace \e[ with \033 if you are using the escape sequence

OptionsDescriptions
\e[40mDark Gray
\e[41mRed
\e[42mGreen
\e[43m Yellow
\e[44m Blue
\e[45mPurple
\e[46mTurquoise
\e[47mLight Gray

Some of the additional prompt variables that are not set in the .bashrc that you might want to customized.

PS1 is the default prompt and that is what is mostly set in the .bashrc
PS2 – use this if you want to change the continuation interactive prompt (When a long command is broken by \ at the end of the line) default is “>”
PS3 – use this for the “select” loop inside a script.
PS4 – use this when a shell script is executed in debug mode (“set -x” will turn this on) default =”++”

Therefore it is command to use the PS1 variables as the rest of the variable are rarely use by normal users.
To test out the prompt before making the change permanent you can issue the command e.g PS1=’Test Prompt \[\033[01;31m\]\u\[\033[00m\]\$’ to change the prompt to display the following.

The \[\033[00m\] options ends the color for the range.