• 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. “userdel: user xxx is currently used by process yyy” – Unable to delete an User
  2. “-bash: route: command not found” on CentOS/RHEL 7
  3. How to Connect Remote Host Using the ssh Command
  4. CentOS / RHEL 5 : dm-multipath file /etc/sysconfig/mkinitrd/multipath explained
  5. “Warning: Missing charsets in String to FontSet conversion” – how to resolve the xclock warning message
  6. Linux / UNIX : How to send mails with attachments using mailx command
  7. dir Command Examples in Linux
  8. CentOS / RHEL 7 : How to set udev rules for ASM on multipath disks
  9. Understanding The /etc/sysconfig Directory
  10. How to Transfer a File In Passive Mode by FTP

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright