4 Methods to Change Hostname in RHEL/CentOS 7/8

To check the hostname open your terminal and type hostname or hostnamectl command in CentOS 7/8. By default, the hostname is localhost.localdomain. To change hostname you can follow the following methods.

1) By Editing /etc/hostname File

The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favorite text editor delete localhost.localdomain and change to whatever name you like.

$ sudo vi /etc/hostname

Save changes and then confirm your hostname by using hostname command. If it has not changed then you need to restart the CentOS 7/8 server.

2) Using Hostnamectl Command

Hostnamectl is a tool that is used control the Linux system hostname. You can also use this tool to change the hostname.

First and foremost confirm the current hostname by using hostnamectl as follows:

$ hostnamectl status
hostnamectl status
hostnamectl status

To change the hostname type the following command:

$ hostnamectl set-hostname hostname
hostnamectl set-hostname
Example - hostnamectl set-hostname grace

It's necessary to restart the systemd-hostnamed daemon so that as to reflect the change in Static hostname.

$ sudo systemctl restart systemd-hostnamed
restart systemd-hostnamed

Note: Hostnamectl recognizes three types of hostnames: pretty, static and transient. Pretty is stored in /etc/machine-info and is a human-readable format while static hostname is stored in /etc/hostname. Transient hostname is more of a temporary hostname which may take up the static hostname when network connectivity is lost.

3) Using Nmtui Utility

To change the hostname you can use NetworkManager text interface tool (nmtui).

From the command line invoke nmtui:

$ sudo nmtui

This will present to you a text user interface like this. Using the arrow keys select Set system hostname and use tab to select OK.

nmtui menu
nmtui - select system hostname

Edit the hostname which by default is localhost.localdomain and change it to whatever you prefer.

set hostname
nmtui - set hostname
edited hostname

After changing the hostname, choose okay, then the following confirmation message will appear. If the hostname is as you desire you can choose OK. You can confirm changes using the hostname command.

Click Ok

4) Using Nmcli Utility

Nmcli is a command line tool for controlling the NetworkManager and can be used to change the hostname.

To check the hostname with nmcli, type in your terminal:

$ nmcli general hostname

This will print out the hostname to your terminal. By default in CentOS 7/8 it should be localhost.localdomain.

To change hostname use the command:

$ nmcli general hostname grace

This will request for your password via a GUI interface. If you are running this command remotely remember to use root or sudo:

$ sudo nmcli general hostname grace

You can confirm the hostname by running nmcli general hostname.

nmcli general hostname
nmcli general hostname


In this article, we learn methods to change hostname in CentOS 7 and same command you can apply on CentOS 8. Please let us know your feedback in the below comment section.

9 Comments... add one

  1. I set the hostname but upon reboot the systemd-hostnamed changes it back to the original hostname.

    I have used hostname, /etc/hostname , /etc/hosts, AND hostnamectl, but none of them are persistent upon reboot.

    Here is /var/log/messages , where it changes the hostname

    Oct 17 16:55:26 sim systemd[1]: No hostname configured.
    Oct 17 16:55:26 sim systemd[1]: Set hostname to .
    Oct 17 16:55:29 sim systemd: Set hostname to .
    Oct 17 16:55:30 sim NetworkManager[372]: hostname 'ip-172-31-18-83'
    Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
    Oct 17 16:55:32 sim dbus[386]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
    Oct 17 16:55:32 sim systemd: Starting Hostname Service...
    Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Successfully activated service 'org.freedesktop.hostname1'
    Oct 17 16:55:32 sim dbus[386]: [system] Successfully activated service 'org.freedesktop.hostname1'
    Oct 17 16:55:32 sim systemd: Started Hostname Service.
    Oct 17 16:55:32 sim systemd-hostnamed: Changed static host name to 'ip-172-31-18-83.ec2.internal'
    Oct 17 16:55:32 sim systemd-hostnamed: Changed host name to 'ip-172-31-18-83.ec2.internal'
    [root@ip-172-31-18-83 ec2-user]# date
    Fri Oct 17 16:57:09 EDT 2014
    [root@ip-172-31-18-83 ec2-user]#

    Any suggestions?

  2. Same issue here, centos 7 static hostname gets set, then changed at boot time -

    1583:Jan 21 17:53:13 p-ftp01 systemd-hostnamed: Changed static host name to 'p-ftp01.predsci.local'
    2253:Jan 21 17:54:34 p-ftp01 systemd-hostnamed: Changed static host name to 'ip-172-31-28-106.ec2.internal'
    2254:Jan 21 17:54:34 p-ftp01 systemd-hostnamed: Changed host name to 'ip-172-31-28-106.ec2.internal'

    • As I can see that you have Amazaon EC2, you'll need to do the following to change your hostname.
      1. Set the hostname in /etc/hostname.
      2. Edit /etc/cloud/cloud.cfg and set preserve_hostname to True. (You can also pass this option in with your user data.)
      Thank You

      • Thank you so much Arun. I've been scouring the web for this having tried all the other methods I could find. Each time on reboot I see the name changed in the log. I found mention of the cloud.cfg but nothing regarding the specifics.

        For other searchers, this is on the CentOS AWS image kernel 3.10.0-123.8.1.el7.x86_64. I'm going to post a comment and credit you Arun.

  3. Arun,

    Thank you man!

    I was about to go crazy since the hostname changes I make was not being applied properly after a reboot!

    Adding "preserve_hostname:true" to /etc/cloud/cloud.cfg made it permanent!

    Any idea if that file is a core component of the OS or is used by something else in essence?


    • Hi Mustafa,
      Welcome to LinOxide,
      I am glad to hear that you fixed your problem. That file is of cloud-init package that handles early initialization of a cloud instance which is used in EC2.
      Thank You !

      • Thanks Arun :-)
        Most OS's cloud image release is packaged with "cloud-init" and it support many data source.
        EC2 datasource is the oldest and most widely used datasource that cloud-init supports.

  4. Working on Amazon AWS EC2, Followed the instructions and did what is written here:
    1. /etc/cloud/cloud.conf has the line
    preserve_hostname: true
    2. Did the command
    sudo hostnamectl set-hostname "foobar"
    Then reboots works like a charm the first time.
    Next reboot (without running the hostnamectl command) but the cloud.conf verified to still have the value to true, and the machine is back to it's Amazon default name of ip_172 something.

    Not sure how to solve this besides hacks of manually setting it.

    On a bit sarcastic mood, Makes me wonder if in 2015/6 we still need to re-design setting up hostname to a machine or move on to more interesting things.


Leave a Comment