3

I want to change the color of the root prompt after using sudo su. It is currently white. I am using Ubuntu in a VirtualBox VM.

Here is a picture of my terminal:

screenshot of Ubuntu terminal in VirtualBox

6

2 Answers 2

3

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.

1

This solution is relatively simple, type

nano ~/.bashrc

in the terminal as root. I like to use nano, you may use whatever text editor you wish. Find and uncomment:

#force_color_prompt=yes

in the file, save/write out the file, then pull up a fresh terminal and enter root. You should now have colored root terminal text.

1
  • Very likely, the OP will want a different color than normal users.
    – vanadium
    Oct 2, 2021 at 10:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .