Change color of root user prompt

I wanted to enable color in the shell as the root user on Ubuntu Server 22.04 after having typed sudo -s. I found this question which led me to the solution of uncommenting the force_color_prompt=yes in the file /root/.bashrc. This worked well, but I’m now wondering if there is also a way to change the color of the root prompt to give better indication that I’m operating as root.

Here is what I have:

enter image description here

Here is what I want:

enter image description here

Update:

For completeness here is my original PS1 line:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '

And here is the modified PS1 line which gave me the results I desired:

PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
Asked By: ubiquibacon

||

Yes; you can color codes in your prompt PS1 variable; for example, in your /root/.bashrc you could PS1="e[38;2;128;0;0m[u@h W]$ " or similar.

There’s also much richer frameworks for shell prompt customization, but they are a bit more prolific (and, in my humble experience, mature) for zsh than for bash. You can try oh my BASH!, but frankly, if you do that, I’d recommend trying zsh instead of bash as your default shell, paired with powerlevel10k for better prompts, and if you need your shell to do things like help you with a lot of tasks, even oh-my-zsh (which oh my BASH! is a clone of).

Answered By: Marcus Müller
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.