How do I change the color of the prompt in Ubuntu terminal after using sudo su?

Solution 1:

The prompt is defined by the PS1 variable. You can see how it is defined with

~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

In standard Ubuntu, it is actually being defined with the command:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  • Change 01;32 in the part \[\033[01;32m\] to a different number to change the color of the username@hostname part.
  • Change 01;34 in the part \[\033[01;34m\] to change the color of the folder name.

A list of color codes (adapted from source):

Color       Code
Black       0;30
Red         0;31
Green       0;32
Brown       0;33
Blue        0;34
Magenta     0;35
Cyan        0;36
White       0;37

Replace 0 with 1 to get a light colored version.

This variable is set in your .bashrc configuration file, which is executed each time an interactive shell is opened. Thus, modify the existing command accordingly.

You can change the color of the prompt of the root user in the same way by editing the .bashrc file of the root user, /root/.bashrc. However, to enable a colored prompt for that account, you should also uncomment the line force_color_prompt=yes.