• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Update the hostname in Shell Prompt Once it is Changed in the Linux OS

by admin

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

For example:

[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

Filed Under: CentOS/RHEL, CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. electrum: Ergonomic Bitcoin wallet and private key management
  2. gdebi: command not found
  3. pluma: command not found
  4. telinit Command Examples in Linux
  5. CentOS / RHEL : How to find free space in a Volume Group in LVM
  6. How to uninstall local-apt-repository software package in Ubuntu
  7. scrot Command Examples in Linux
  8. insmod: command not found
  9. aurman Command Examples
  10. How to Create and Manage Storage Domains in RedHat Virtualization (RHV)

You May Also Like

Primary Sidebar

Recent Posts

  • glab Command Examples
  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright