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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. Beginners Guide to Tuning Profiles in CentOS/RHEL
  2. How to Clone Linux disk partition over network using dd
  3. enum4linux: command not found
  4. lvremove Command Fails With Error “LVM – Can’t remove open logical volume”
  5. How to Move Swap From Disk Partition to LVM Volume in Linux
  6. CentOS / RHEL 7 : Unable to start/enable iptables
  7. a2dismod: command not found
  8. kpartx Command Examples in Linux
  9. Beginners Guide to Managing Package Module Streams in CentOS/RHEL 8
  10. How to configure apache virtual host on ubuntu

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright