[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

Display dotted hostname at prompt

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.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
3,767
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Sakina Nakagawa

I joined the company in 2016 as a new graduate. Lately, I've been having fun learning the basics of servers.