How to Set or Change a Hostname in CentOS 7 | PhoenixNAP KB

Introduction

A host name is simply the name used by a server on a network. Setting a unique hostname is a great way to quickly find and identify different servers on your network.

There are three 3 types of host names.

  1. The static host name is the most important and is stored in the /etc/hostname file. This hostname is used between machines to identify a particular server.
  2. The nice hostname got its name because it allows for more characters and punctuation. It is easier to use, but since it uses non-standard characters, it is not allowed for machine code. The nice hostname is stored in the /etc/machine-info directory.
  3. The transient hostname is one maintained in the Linux kernel. It is dynamic, which means it will be lost after a reboot. This approach can be useful if you have a minor job that requires a temporary host name, but you don’t want to risk making a permanent change that can be confusing.

Prerequisites

  • Server running CentOS 7
  • Accessing a user account with root privileges Terminal window
  • (Menu > Applications > Utilities > Terminal)
  • A text editor, such as Vim

How to change

the Centos hostname

Step 1: Verify

the existing hostname

Before you begin, it is recommended to check what your current hostname is. Type the following command in the console to find out

: hostnamectl The output should return the static host name

, as well as a list of other information about the network configuration and operating system

. Step 2:

Set a new static

hostname

Since CentOS 7 only allows fully qualified domain names (FQDNs), verify the hostname you plan to use.

Acceptable values include:

  • Lowercase letters from a to z
  • Numbers from 0 to 9
  • Periods and hyphens
  • Host names must

  • be between 2 and 63 characters long Host names
  • must begin and end with a number or letter

Type the following command in the terminal: hostnamectl set-hostname my.new-hostname.server

Step 3: Verify

the host name

Then, check the host name again by using

the

following command

: hostnamectl

The console should display the new host name

. Step 4: Edit the /etc/hosts file

Start by opening the hosts file by typing:

sudo vim /

etc/hosts

In the text editor, locate 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 old.hostname entry to my.new-hostname.server and type it as in step 2.

Save the file and exit

.

Step 5: Restart and verify the hostname of the CentOS 7 machine Restart the

system. Open a console window and run:

hostnamectl

It should display your new host name

.

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

Step 6 (Optional): Use

a nice hostname To use a “nice” hostname

, type the following command

: hostnamectl set-hostname “My boss’s hostname”

Make sure you have the quotation marks

.

Once complete, verify the hostname: hostnamectl

status

The console should return a list of information. Check the lines Static Hostname and Pretty Hostname: They should look like this: Static hostname: mybossshostname Pretty hostname : My boss’s hostname By putting the hostname in quotation marks, you are telling the system to use the complex characters inside the quotation

marks as a pretty hostname

.

This allows you to avoid character restrictions for static host names.

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

Step 7 (optional): Configure a

transient hostname

Fist, open the console and type the following:

sudo hostnamectl -transient set-hostname temporary.hostname

You can verify the hostname the same way you did previously, with the hostnamectl or hostnamectl status command

.

This change will last until you restart your computer.

You can use this command with any type of host name (static, pretty, or transient) as a double-hyphenated option.

Just use the double hyphen to indicate what you want:

sudo hostnamectl -prettyset-hostname “Pretty Hostname” or sudo hostnamectl -staticset-hostname temporary.hostname

Conclusion Setting

or changing a hostname on CentOS 7 is pretty straightforward

.

If you are a more advanced user, you can also try other methods. One way is to edit the /etc/hostname file with a text editor. Alternatively, you can run the nmtui tool at the command prompt, which will launch a small graphical tool to change the hostname.

A guide on how to change or set the hostname is also available for other Linux distributions such as Ubuntu 18.04.

Contact US