Home Beginner's Guide How to change the Hostname on CentOS

How to change the Hostname on CentOS

by Habib Ahmed
change hostname centos

The hostname is a critical piece of information that helps you maintain your server machines. As a system administrator, it is imperative to have a short but recognizable hostname to separate the server machines from each other. An ambiguous or complex hostname may cause difficulty in recognizing the exact server that you may want to access remotely and can increase chances of error while applying software patches or other updates.

In this article, I’ll focus on changing the CentOS server’s Hostname, but before that, let’s discuss the different types of Hostname.

Hostname has three types:

Static Hostname: It is the primary and essential Hostname. They are kept in the /etc/hostname file. It is used by machines to recognize the specific server.

Pretty Hostname: This hostname type is user friendly. It allows you to use additional character and punctuation. But it is not permitted for machine code due to its non-standard characters.

Transient Hostname: It is a temporary hostname and is reset after the reboot. Linux Kernel maintains this Hostname. You can use it for a minor troubleshooting job in a session.

In this article, we will describe a method to change it permanently without restarting your PC.

How to change the Hostname on CentOS

To change the Hostname in the CentOS permanently follow the beneath steps:

Step 1: Check Current Hostname

Firstly, you should know the current Hostname. To check the current Hostname, type hostnamectl command in the console.

$ hostnamectl

This command will list the operating system and network configuration information along with the static Hostname.

Output:

Static hostname: workmachine21
Icon name: computer-desktop
Chassis: desktop
Machine ID: 5f7e36c18a974f06ae94ddaaf11d71e8
Boot ID: 337e48b00fed4abe9ab929fed5aa6018
Operating System: CentOS Linux 8 (Core)
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-193.x86_64
Architecture: x86-64

Step 2: Change the Static Hostname

Before changing the static Hostname of CentOS 7, you should know the values of it because it allows only Fully Qualified Domain Names (FQDNs).

It includes:

  • Lower-case letters A-Z
  • Numbers 0-9
  • Hyphens and Periods
  • Character limit of the Hostname is 2 to 63
  • Hostname start and end must be a letter or a number.

Now type the command as follows:

hostnamectl set-hostname fosslinux

We just changed the static Hostname from “workmachine” to “fosslinux”.

Step 3: Verify the Hostname

After changing the Hostname, verify it by typing the hostnamectl command. The result shows the system information along with a new static hostname.

Output:

Static hostname: fosslinux
Icon name: computer-desktop
Chassis: desktop
Machine ID: 5f7e36c18a974f06ae94ddaaf11d71e8
Boot ID: 337e48b00fed4abe9ab929fed5aa6018
Operating System: CentOS Linux 8 (Core)
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-193.x86_64
Architecture: x86-64

Step 4: Use Pretty and Transient Hostname (Optional)

For setting a pretty Hostname, the use of quotation marks is mandatory if the Hostname contains spaces or a single quotation mark. For example:

hostnamectl set-hostname "FOSS Linux" --pretty

For Transient Hostname, where we set a temporary hostname, use the command below:

sudo hostnamectl –transient set-hostname temporary.fosslinux

After completing the renaming, check the status of Hostname by using the command:

hostnamectl status

The console will return a list of information, including the static, pretty, and transient Hostname, to confirm the change.

Note: Machines can only identify the FQDN hostname. So, CentOS automatically changes or removes the unacceptable characters.

Clearing a Particular Host Name

To clear a hostname and revert to the default, enter the following command as root with the appropriate option:

hostnamectl set-hostname "" [option...]

Where “” is a quoted empty string followed by option is one or more of: –pretty, –static, and –transient.

Changing Host Names Remotely

For the situations where you want to execute a hostnamectl command on a remote system, use the -H, –host option as follows:

hostnamectl set-hostname -H [username]@name

The tool will use SSH to connect to the remote system. Where name is the remote Hostname, you want to configure. The username is optional.

Conclusion

That’s all about setting, clearing the CentOS hostname on a local machine or over the network, remotely. Run the desired command from the commands as mentioned earlier and permanently or temporarily change the Hostname.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.