There is no technical reason to change the color of the terminal console, but anyone can accidentally issue commands as another user or while logged in to the wrong machine when all prompts look the same.

That is what we are going to address:

Find out your current prompt style code:

echo $PS1

To change your prompt style, run the following command replacing the bold text with the desired style:

export PS1="put_the_new_style_here"

Try the following two styles to see how it works:

export PS1="\[$(tput setaf 3)\]\t \u@\h:\w $ \[$(tput sgr0)\]"
export PS1="\[$(tput setaf 1)\]\t \u@\h:\W $ \[$(tput sgr0)\]"

Notice the difference between the lines. Only one number changed: 3 to 1. That is because 3 means Yellow and 1 means Red.

Color codes for the tput command:

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

This configuration is lost as soon as you close the terminal. To make it permanent, edit the file ~/.bashrc (on Ubuntu 20.04; other systems may use .bash_profile):

sudo nano ~/.bashrc

Append the color definition line at the end of the file.

There are other ways to create more complex color schemes, such as changing the color of specific parts of the prompt or applying Bold or Italic:

export PS1='\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

The syntax follows this structure: \[\033[X;Ym\]

Where X is the attribute and Y is the color.

Color Value
Green 32
Red 31
Black 30
Blue 34
Cyan 36
Purple 35
Yellow 33
White 37

Most common attributes:

Attribute Value Purpose
0 Normal text (default, even if no attribute is set)
1 Bold text (in the Ubuntu Terminal)
2 Dim text
4 Underlined text
5 Blinking text
7 Reversed text and background colors
8 Hidden text

BUG FIXING

A common issue with prompt customization is that keyboard history navigation using the up and down arrow keys can break.

A workaround is to wrap non-printing characters in ‘[]‘ within your PS1 variable, like this:

export PS1="\[\e[m\]\u@\e[1;31m\h\e[m \W ⇨ \[\e[m\] "
export PS1="\[\e[m\]\u@\[\e[1;31m\]\h\[\e[m\] \W ⇨ \[\e[m\] "

Have fun!


BONUS

For privacy when sharing or recording your screen, consider this prompt:

export PS1="\[$(tput setaf 4)\]$ \[$(tput sgr0)\]"

On Kali:

export PS1="%F{%(#.blue.green)}┌──${debian_chroot:+()─}${VIRTUAL_ENV:+($(basename $VIRTUAL_ENV))─}[%B%F{reset}%(6~.%-1~/…/%4~.%5~)%b%F{%(#.blue.green)}] 
└─%B%(#.%F{red}#.%F{blue}$)%b%F{reset} "

TrueNAS-based style:

PS1="\[\033[44m\]\u@\h\[\033[0m\] \[\033[1m\]\[\033[36m\]\W\[\033[37m\] \$\[\033[0m\] "
PS1="\[\033[41m\]\u@\h\[\033[0m\] \[\033[1m\]\[\033[38m\]\w\[\033[37m\] \$\[\033[0m\] "
PS1="\[\033[42m\]\u@\h\[\033[0m\] \[\033[1m\]\[\033[33m\]\W\[\033[37m\] \$\[\033[0m\] "
PS1="\[\033[45m\]\u@\h\[\033[0m\] \[\033[1m\]\[\033[94m\]\W\[\033[37m\] \$\[\033[0m\] "
PS1="\[\033[40m\]\u@\h\[\033[0m\] \[\033[1m\]\[\033[92m\]\W\[\033[37m\] \$\[\033[0m\] "