0

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)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

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

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
ubiquibacon
  • 103
  • 4
  • I'm assuming you're using bash, because you're using .bashrc. If that's wrong, please fix my tag :) – Marcus Müller Mar 11 '24 at 23:08
  • @MarcusMüller that is correct – ubiquibacon Mar 11 '24 at 23:13
  • Before moving around with a loaded root on your hands take a look at this – Pablo A Mar 12 '24 at 03:38
  • @PabloA Thanks, that is a nice thesis. Kind of left me with more questions though. I would love to see a "good, better, best" breakdown of how to handle sudo and root permissions to protect against the most common attack vectors. "Best" would be the "one-time keys" that were mentioned in the accepted answer, but realistically only large orgs will be doing that. We limit the sudo group to a minimal amount of trusted users and have no actual users in the root group. Sensitive files have root:root ownership usually with X00 permissions. – ubiquibacon Mar 12 '24 at 17:10

1 Answers1

3

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).

Pablo A
  • 2,712
  • Thanks! I knew about oh my zsh, but didn't realize there was a bash port. The syntax of what you proposed was slightly different than what was in my .bashrc file, so I updated my question for completeness. – ubiquibacon Mar 12 '24 at 02:00