How to Change Hostname in RHEL / CentOS 7.0

You have probably needed to change the hostname of your CentOS 7 install for one reason or the other. Changing the hostname in CentOS 7 is now much simpler than ever.

If you are ready open up your terminal and follow through this tutorial, and in less than 10 minutes you will have changed your hostname!

Check Hostname

To check the hostname open your terminal and type the following command:

$ hostname

By default the hostname is localhost.localdomain. To change this you can follow the following steps

Edit /etc/hostname File

The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favourite 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 server.

Using Hostnamectl

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

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

$ hostnamectl status


hostnamectl status

To change the hostname type the command:

$ hostnamectl set-hostname grace


hostnamectl set-hostname

It may be 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.

Using Nmtui

To change the hostname you can also use the NetworkManager text interface tool (nmtui). This is also another very easy method and fairly straight forward.

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 default

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

default 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. authorize nmtui

Using Nmcli

Nmcli is a command line tool for controlling the NetworkManager and can also 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 it should be localhost.localdomain.

To change this 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 then check the hostname with the command hostname or nmcli general hostname.

nmcli usage


See how easy it was? CentOS 7 is now much easier to administer compared to the previous releases. Its time to upgrade/install systemd version if you haven't done so.

Allan Mbugua 9:12 am


Your email address will not be published. Required fields are marked *


  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'

    1. 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

      1. 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?


    1. 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 !

      1. 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.