Question: In this case, the hostname(geeklab) has already been updated in /etc/sysconfig/network and /etc/hosts file, but still needs to be updated in the shell prompt.
For example, to change the old shell prompt as below:
From : [root@geek ~]#
To : [root@geeklab ~]#
OS: CentOS/RHEL 6 and 7
Changing the Shell Prompt On the fly
The shell prompt is controlled using the PS environment variables. Here is the list of commonly used PS environment variables and their function.
- PS1 – The value of this parameter is expanded and used as the primary prompt string. The default value is \u@\h \W\\$ .
- PS2 – The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ].
- PS3 – The value of this parameter is used as the prompt for the select command.
- PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +.
PS1 is a primary prompt variable which holds \u@\h \W\\$ special bash characters. This is the default structure of the bash prompt and is displayed every time a user logs in using a terminal. These default values are set in the /etc/bashrc file.
This command will show the current value.
# echo $PS1
[root@geek ~]# echo $PS1 [\u@\h \W]\$
This can be modified by changing the PS1 variable:
# PS1="[\u@geeklab \w]# "
Now the modified prompt will look like below. For example:
[root@geek ~]# PS1="[\u@geeklab \w]# " [root@geeklab ~]#
Making Changes Permanent
In order to make these settings permanent, add it to the ~/.bash_profile file:
# echo 'export PS1="[\u@\h \w]# "' >> ~/.bash_profile
The special characters in the default prompt are as follows:
\u = username
\h = hostname
\W = current working directory