Display dotted hostname at prompt
table of contents
This is Nakagawa from the System Solutions Department.
When I built a server the other day, I re-examined the prompt display format, so
I would like to introduce it to you.
The prompt is the next part that appears when you log in to the server.
[root@test ~]#
By default, user name@host name and current directory are displayed from the left.
We will show you how to change the host name displayed here.
Please note that this method is only applicable up to CentOS6, so please note that there may be differences in the way it works on CentOS7.
1. Temporarily change host name
First, here's how to change it temporarily:
hostname {hostname}
After running the command, run the hostname command without any options to display the hostname you entered.
In this case, rebooting will disable the settings and return to the previous host name.
2.Enable the configured host name even after rebooting
Even though you were able to change the host name in step 1, the host name displayed in the prompt remains the same.
To change the prompt display and keep the changes across reboots,
you need to modify two configuration files.
・/etc/hosts
127.0.0.1 localhost localhost.localdomain {host name}・/etc/sysconfig/network
HOSTNAME={host name}
After setting, restart /etc/init.d/network to take effect.
If it is not reflected, you will need to restart the server itself.
3. Set a special hostname
the host name you want to set contains
. such as ○○.jp or ○○.co.jp , it may not be reflected properly using the method explained above.
In that case, specify an environment variable to change the host name.
The configuration file to edit is:
-Edit or add the environment variable "PS1"
The display format of the prompt is the environment variable PS1, so
edit it or add a new one if it is not set.
You can check the list of currently set environment variables using the following command.
# env
# printenv
What we want to display on the prompt this time is the user name, host name, and current directory, so
set them as the following environment variables.
export PS1="[\u@\H \W]\$ "
Run the export command and see if there are any errors.
If you run the env command again, the settings for the environment variable "PS1" will be displayed.
・~./bashrc
This is a file that exists under the target user's home directory.
The environment variable "PS1" set earlier will become invalid after rebooting, so
add it to the end of the .bashrc file so that it will be automatically read when you log in.
export PS1="[\u@\H \W]\$ "
After configuring the above settings, close the session or switch to another user and return to the original user,
then log in again and the host name you set will be displayed in the prompt.
4.Other settings changes
Here are some examples of other settings changes.
If you want to display the date, add "\d" to the environment variable.
export PS1="[\u@\H \d \W]\\$ "
If you want to display the command history number, add "\!".
export PS1="[\u@\H \! \W]\\$ "
At the end
I thought I'd share some tips on how to customize prompts that are easier for you.
Thank you for reading to the end.