Setting up a Ubuntu-CentOS Server from scratch: Customize your shell - LightDestory - 11-06-2019
*This tutorial is part of a series, visit the main thread to learn more*
When we own a server we basically interact with it via a terminal using SSH or something older then that. The bash prompt became something that we always see and read, so why don't customize it?
On this tutorial, we will learn how customize our bash prompt: how to change colors and how to add information that can useful on Ubuntu Server & CentOS.
Let's start with some information!
- How works our bash prompt?
Most Linux distro displays by default the hostname and the current working directory. Prompt's configuration is stored inside a special shell variable called PS1, there are other variables called PS2, PS3 and PS4 that we will not be used on this tutorial.
So we can say that the PS1 variable is used as the primary prompt string and it is evaluated before each terminal update. To see our current bash's prompt configuration we can run the following command:
This configuration can be system-wide or user-specific, on this tutorial we will focus on user-specific customization.
- Where is placed the bash's prompt configuration file?
As we said above, the customization we will focus on is the user-specific one, so our bash prompt configuration file called ".bashrc", that contains the PS1 variable, is stored in the user's home:
Code: ~/.bashrc
OR
/home/{user}/.bashrc
- What kind of customization can I configure?
Well, let's start listing the customization that can add useful information to our bash:- A bell character:
- An escape character:
- A newline:
- A carriage return:
- A backslash:
- A character whose ASCII code is the octal value nnn:
- A character (like 'q' will print 'q'):
- If the user's uid is 0 it will print a '#', otherwise, if the user is 'root' it will print a '$':
- The date in 'Weekday Month Date' format (like “Tue May 26”):
- The time, in 24-hour HH:MM:SS format:
- The time, in 12-hour HH:MM:SS format:
- The time, in 12-hour am/pm format:
- The time, in 24-hour HH:MM format:
- The username of the current user:
- The version of Bash (like 5.00):
- The release of Bash, version and patchlevel (like 5.00.2):
- The hostname, up to the first ‘.’ (like 'POST4VPS.FREEVPS.COM' will be displayes as 'POST4VPS'):
- The hostname (like 'POST4VPS.FREEVPS.COM' will be displayes as 'POST4VPS.FREEVPS.COM'):
- The number of jobs currently managed by the shell:
- The basename of the shell’s terminal device name:
- The name of the shell, the basename of $0 (the portion following the final slash):
- The current working directory, with $HOME abbreviated with a tilde (like if you are on '/home/POST4VPS_tutorial' folder it will print '/home/POST4VPS_tutorial'):
- The basename of $PWD, with $HOME abbreviated with a tilde (like if you are on '/home/POST4VPS_tutorial' folder it will print 'POST4VPS_tutorial'):
- The history number of this command:
- The command number of this command:
- Begin a sequence of non-printing characters that can be used to embed a terminal control sequence into the prompt:
- End a sequence of non-printing characters:
The last two codes are really important because we will use them to set the colors and formatting configurations, the following codes need to be between the BEGIN and END snippets (like "\[ COLOR_FORMATTING_CODE \]"):- Reset the colors:
- Set text color as Black:
- Set text color as Red:
- Set text color as Green:
- Set text color as Yellow:
- Set text color as Blue:
- Set text color as Purple:
- Set text color as Cyan:
- Set text color as White:
- Set text as bold and text's color as Black:
- Set text as bold and text's color as Red:
- Set text as bold and text's color as Green:
- Set text as bold and text's color as Yellow:
- Set text as bold and text's color as Blue:
- Set text as bold and text's color as Purple:
- Set text as bold and text's color as Cyan:
- Set text as bold and text's color as White:
- Set text as underline and text's color as Black:
- Set text as underline and text's color as Red:
- Set text as underline and text's color as Green:
- Set text as underline and text's color as Yellow:
- Set text as underline and text's color as Blue:
- Set text as underline and text's color as Purple:
- Set text as underline and text's color as Cyan:
- Set text as underline and text's color as White:
- Set the text background color as Black:
- Set the text background color as Red:
- Set the text background color as Green:
- Set the text background color as Yellow:
- Set the text background color as Blue:
- Set the text background color as Purple:
- Set the text background color as Cyan:
- Set the text background color as White:
Now, we can start our tutorial:
Opening file configuration:- MAKE A BACKUP of your .bashrc before starting editing it!
- As we already said our bash prompt configuration file is located inside the current user folder, so we can open it using terminal editor, such as nano or vim, or graphical editors, such as gedit or sublime.
Locate the PS1 variable and start customizing:- Scroll down until you locate the PS1 variable.
- Add your own customization by mixing up all the above snippets of code.
Examples:- I want to display username in yellow and hostname in red (Desired output: user@host:
Code: \[\e[0;33m\]\u\[\e[0m\]@\[\e[0;31m\]\h[\e[0m\]
- I want to display the username is blue, hostname in purple and the current directory in bold and green:
Code: \[\e[0;34m\]\u\[\e[0m\]@\[\e[0;35m\]\h[\e[0m\]\[\e[4;32m\]\w
Useful online tools:
There are very useful online tools that can help you creating your own bash configuration:
To see your customization enable just re-log into the system!
Have fun!
RE: Setting up a Ubuntu-CentOS Server from scratch: Customize your shell - chanalku91 - 11-06-2019
Very useful, to change the style of your linux terminal shell!
I want to ask!
Can it be made like this?
https://ibb.co/2PSwyTk
RE: Setting up a Ubuntu-CentOS Server from scratch: Customize your shell - LightDestory - 11-06-2019
(11-06-2019, 09:27 AM)chanalku91 Wrote: Very useful, to change the style of your linux terminal shell!
I want to ask!
Can it be made like this?
https://ibb.co/2PSwyTk
Likely yes but there are two limitations:- You can't make a background color effect like that, the background color is applied in a "character area" basics.
- You can't display images, the branch/fork symbol is not an ASCII character so you should replace it with something like the CHARACTER 204 (╠)
RE: Setting up a Ubuntu-CentOS Server from scratch: Customize your shell - humanpuff69 - 11-06-2019
(11-06-2019, 09:38 AM)LightDestory Wrote: Likely yes but there are two limitations:- You can't make a background color effect like that, the background color is applied in a "character area" basics.
- You can't display images, the branch/fork symbol is not an ASCII character so you should replace it with something like the CHARACTER 204 (╠)
ssh doesnt have an ability to display an image , it just basically dumb terminal in current day that can only show text . with a lot of custom character that have pixel per pixel probably you can make an image but that is very hard . just use vnc over a gui for image support
RE: Setting up a Ubuntu-CentOS Server from scratch: Customize your shell - LightDestory - 11-06-2019
(11-06-2019, 02:49 PM)humanpuff69 Wrote: ssh doesnt have an ability to display an image , it just basically dumb terminal in current day that can only show text . with a lot of custom character that have pixel per pixel probably you can make an image but that is very hard . just use vnc over a gui for image support
Isn't this a useless/"just to increase my counter" reply?
Did you even read my tutorial? When did I said that we were going to display images?
I have clearly listed almost every type of customization and there isn't any 'Image reference", just speaking about characters.
On my reply regarding @chanalku91 question I have again clearly said that we can't displays images so he should think about replacing it with a symbol like '204' because it reassemble that particular image.
'Custom character that have pixel per pixel'... what? Again you didn't read the ASCII part of the tutorial.
Your suggestion about using a desktop remote technology is just no sense and out of contest.
|