Malcom Ridgers is a tech expert at BairesDev specializing in the software outsourcing industry.
The Linux bash shell is quite the wonder. With it, you can interact with the operating system in very powerful and flexible ways. To do that, you make use of one of many terminal emulators, such as GNOME Terminal, Guake, Terminator, or xterm.
With the bash shell, you run commands like ls, mkdir, chmod, rm, ln, iptables, and make. This is all done from the prompt, which tends to default a minimal bit of information, such as:
Where USER is your username and HOST is the hostname of the machine you're working on. It's not much information but at least you know who you are and where you are.
You should know, though, that the bash terminal is quite customizable. And for many a seasoned Linux administrator, such customizations are often considered a must-have.
Let's first answer the question "Why?"
There are a very few good reasons you'd want to customize your bash prompt. One of the more important reasons is to ensure the admin knows exactly the machine they are working on. Or maybe you want to make it very clear that you're working with the root user.
Why would you want to do that? When an admin gets busy, really busy, mistakes can be made. You might be able to clearly avoid making even the slightest mistake if you have a red terminal glaring at you to say, "You're using the root user now!" or that "You're working on your database server at this moment!"
And not only is it crucial to always know which user's prompt you're on, being aware of which machine you're working with could prevent you from taking action on the wrong machine, actions that could have devastating effects (such as uninstalling Java on a web server used for Java development).
These might seem like very small changes, but anything you can use to prevent errors is an important step in your daily admin grind.
With that said, let's take a look at how to make that bash prompt serve our needs.
There are a few ways to customize the bash prompt, some are permanent and some are temporary. I want to focus only on the permanent method, because the temporary method doesn't really help us, especially when we're trying to ensure we know exactly what we're doing and with what server.
There are two ways to permanently change the bash prompt. The first is on a per-user basis (which is what we'll use). The second method changes the bash prompt globally, but the problem with that method is that it's not nearly as flexible. And given this is Linux, we want to illustrate how flexible the operating system is.
With that said, there's a file in your user's home directory (which is often indicated as ~/) called .bashrc. This file is responsible for housing user-specific aliases and functions. This file is also where you can place bash prompt customizations.
To edit this file, you'll use a text-based editor like nano. So open the file with the command:
How bash handles the default prompt will differ depending on what Linux distribution you use. However, one of the universals is that the bash configuration will always begin with:
Let's start with the CentOS 8 .bashrc file (because it's fairly simple). When you open that file with nano, you'll see the line:
Okay, so it's not exactly simple. But we can break it down.
First, let's talk color. When you see the string \e[ it indicates the beginning of a color change. Colors are indicated with numbers:
And every color has a typeface:
For example, you could set a bold red color with:
Or a normal black with:
The next important strings are for various elements you can add to the prompt, which are:
Now, let's start to put those together. Let's say we want to create a prompt that indicates (in bright red letters) that we're working on a machine that's used for a Java web development company. We'll indicate that with the word "Java" in bright red letters.
Open the .bashrc file (for the user that will be working on the machine) with the command:
At the bottom of that file, we're going to add a line that will print out a bash prompt to include username@hostname :JAVA: $: and will indicate the base of the current working directory. For that we'll add the following:
Save and close the file and open a new terminal window. Your prompt should now look like that in Figure 1.
Our new custom bash prompt, indicating we're on the JAVA development machine.
Now, let's customize the CentOS 8 prompt so we know (without a doubt) we're the root user on that machine. For that we'd need to change the root user .bashrc file by first su'ing to the root user with the command:
Edit the root user's .bashrc file with the command:
At the bottom of that file, we'll create a prompt customization that prints out the username in red and adds a $ *: at the prompt (for extra warning). That line would look like:
Save and close the file. Open a new prompt, su to the root user, and you very clearly see you are using root (Figure 2).
Yes, you are at the root user's prompt, so you should be very careful.
Now that you know, without a doubt, you are at the root user's prompt, you will make sure to use caution. Of course, this just scratches the surface as to how you can change your bash prompt on Linux. But this should give you a start on your journey to some serious bash customizations.
Create your free account to unlock your custom reading experience.