Using /root Preferences with sudo -s

Sometimes, you just have to drop into a root shell in order to Get Things Done.

When you are root, your capacity for destroying your system is virtually limitless. So it would be wise to try and avoid doing anything that might result in losing a few days of work while you patch your system back together.

What I’ve done to avoid this particular unpleasantness is to force my shell to read preferences from my /root folder when I am in fact the root user. In this way, I can keep my preferences separate, for instance, when I alias rm to rm -i, just to avoid stupid mistakes.

I’ve also modified the color of my Bash prompt in /root/.bashrc to show up as bright red, as a visual reminder that I am in god mode on my system. It has helped me avoid at least one brush with calamity.

Let’s take a look at how we can accomplish this

There are two files you need to modify. Well, one you need to, and one you want to in order to be cool.

Modifying sudoers

The first is the sudoers file. The only way to properly do this is to use this command, with the sudo command or as root:

# visudo -f /etc/sudoers

This opens up the sudoers file, which is the file that sets permissions for users that have sudo power. The reason we use visudo is because it it safer in case something horrible like a network failure happens. In this file, at the top, you should see a few lines that start with Defaults. Add this line at the end:

Defaults    env_keep -= "HOME"

Which says, “When you are in the root shell, do not keep (-=) the $HOME environment variable of the actual user you are.” This will force Bash to load your preference files (.profile, .bashrc, etc.) from the /root folder. This way, you can keep separate preferences when you sudo -s into a root shell.

Cue The Colored Prompts!

If you’re like me, regularly popping in and out of a root shell to take care of administrative tasks, it would be helpful if there were a reminder that you were root. Aside from the # sign at the end of the prompt.

The way I do this is by coloring my prompt a bright, unmistakable cherry red I borrowed from Kali Linux (and I’m sure other distros) that screams, “You Are Root! Do Nothing Stupid!”

Alternating between periwinkle user and red root

The code for colorizing the Bash prompts should be located in your .bashrc files located in your home directory ~/ and /root directory, respectively.

Since I use a modern Linux distro, xterm, konsole, and yakuake (a dropdown konsole like guake) can all display a myriad of colors (download ponysay if you have little girls). By using one of a plethora of Linux terminal color charts online (like this one), I can use different colors for different computers in my network. In order to force my terminals to show colors, there were two things I needed to do.

Opening up my .bashrc file, I uncommented:

force_color_prompt=yes

I hardly think that requires explanation. I found it on or about line 39. The second was to adjust the value of $PS1, a dozen or so lines further down. In my root .bashrc, the line is:

PS1='${debian_chroot:+($debian_chroot)}\[\033[1m\]\[\033[38;5;196m\]\u@\h:\w\$\[\033[00m\] '

Which reads, “Show the chrooted value (if chrooted), then use the escape sequence [\033, then turn on bolding [1m], then use the escape sequence [\033 again, use the color [38;5;196m] (256-color #196, bright red), show the user \u, the @ sign, the host \h, a : the working directory \w, the prompt $(which will change to a # for root) then shut off all color formatting [\033[00m], then end with a space. In my regular user prompt, the periwinkle color code is 38;5;104m.

I don’t have explanations why the colors are referred to this way, although I do understand the color numbers. Some of it I have to take on faith that it Just Works.

Leave a Reply

Your email address will not be published. Required fields are marked *