Introduction

This tutorial is for users who already have a server running the CentOS 7 operating system on a network.

A hostname is simply the name a server goes by on the network. Setting a unique hostname is an excellent way to make sure that different servers on your network can be found and identified quickly.

There are three 3 types of hostnames.

The first – called Static hostname is the most important one, and it’s stored in the /etc/hostname file. This hostname is used between machines to identify a particular server.

Another kind of hostname is called Pretty because it allows for more characters and punctuation. This is more user-friendly, but since it uses non-standard characters, it is not permitted for machine code. The pretty hostname is stored in /etc/machine-info.

The third and final type of hostname is Transient. A transient hostname is changed in the Linux kernel but will be lost after a reboot. This approach might be useful if you have a minor job that requires a temporary hostname, but don’t want to risk making a permanent change that might be confusing.

change hostname on centos 7 tutorial

Prerequisites

  • Server running CentOS 7
  • Access to a user account with root privileges
  • A command prompt (Menu > Applications > Utilities > Terminal)
  • A text editor, like Vim

How to Change Centos Hostname

Step 1: Check Existing Hostname

Before you start, it is advised to check what your current hostname is. Type the following in the console:

hostnamectl

The console should return the static hostname, as well as a list of other information about your network configuration and operating system.

Step 2: Set a New Static Hostname

Double check the hostname you plan to use. CentOS 7 only allows Fully Qualified Domain Names (FQDN’s). Acceptable values include lower-case letters a to z, numbers 0 to 9, the period, and the hyphen, and between 2 and 63 characters.

At the console, type:

hostnamectl set-hostname my.new-hostname.server

NOTE: Replace my.new-hostname.server with your chosen hostname.

Step 3: Check the Hostname

At the console, type:

hostnamectl

The console should display the new hostname.

Step 4: Edit the /etc/hosts File

At the console, type:

sudo vim /etc/hosts

In the text editor, look for the line that begins with 127.0.0.1 (the IP address that refers to the system you are working on). It should read:

127.0.0.1  localhost localhost.localdomain localhost 4 localhost4.localdomain4 old.hostname

Change the entry old.hostname to my.new-hostname.server – and spell it the same as in Step 2. Save the file and exit.

Step 5: Reboot and Check CentOS 7 machine hostname

Restart your system. Open a console window, and run:

hostnamectl

It should display your new hostname.

You can also use your text editor to open and verify your /etc/hostsfile. It should still have your new hostname listed.

Step 6 (Optional): Using a Pretty Hostname

At a console, type:

hostnamectl set-hostname “My Boss’s Hostname”

Make sure you have the quote marks.

Once that completes, check the hostname by typing:

hostnamectl status

The console should return a list of information.  Check the Static hostname and Pretty hostname lines – they should look like this:

Static hostname:  mybossshostname

Pretty hostname:  My Boss’s Hostname

By putting the hostname in quotes, we’re telling the system to use the complex characters inside the quotes as a pretty hostname. This enables you to get out of the character restrictions for static hostnames!

But you still need a FQDN hostname for the machine to understand. Fortunately, CentOS is smart enough to remove or change any unacceptable characters and render the static hostname automatically.

Step 7 (Optional): Setting a Transient Hostname

Open your console and type the following:

sudo hostnamectl –transient set-hostname temporary.hostname

You can check the hostname in the same way you did earlier, with the hostnamectl or hostnamectl status command. This change will last until you reboot the machine.

You can use this command with any type of hostname (Static, Pretty, or Transient) as an option with the double-hyphen. Just use the double-hyphen to indicate what you want:

sudo hostnamectl --prettyset-hostname “Pretty Hostname”

or

sudo hostnamectl --staticset-hostname temporary.hostname

Conclusion

To Set or change a hostname in CentOS 7 is fairly straightforward.

There are other methods you can use if you are a more advanced user. One way might be editing the /etc/hostname file with a text editor. Another might be to run the nmtui tool at the command prompt, which will launch a little graphical tool to change the hostname.