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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. Understanding rsyslog Templates
  2. Linux OS Service ‘network’
  3. How to Change the Default Timeout Settings for Telnet Session in CentOS/RHEL
  4. How to Clone Linux disk partition over network using dd
  5. How to query and modify kernel parameters using sysctl (Immediately and persistently) in CentOS / RHEL
  6. How to copy directories recursively using rsync while excluding specific files
  7. What is the purpose of utmp, wtmp and btmp files in Linux
  8. 18 Practical tcpdump Command Examples – A Network Sniffer Tool Primer
  9. LVM Configuration : Physical Volume (PV) Operations/Utilities
  10. CentOS / RHEL : How to exclude kernel or other packages from getting updated using YUM Versionlock Plugin

You May Also Like

Primary Sidebar

Recent Posts

  • SQL script to find tables that are fragmented
  • TRUNCATE TABLE not releasing space from tablespace
  • How to reclaim entire space of an oracle database table with “Truncate Table” statement
  • Oracle SQL Script to Report Tablespace Free and Fragmentation
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary