How to Set or Change Hostname in CentOS 7 / RHEL 7

After installing the CentOS 7 on my server, I tried to change hostname by modifying the /etc/sysconfig/network, but the change did not take an effect of the modification. Even after multiple reboots of the server, the hostname remained localhost.localdomain.

I found that the procedure to change the hostname in CentOS 7 is now totally different from the previous version. This guide will help you to setup the hostname on CentOS 7 / RHEL 7.

Hostname Class

Static – The static hostname is traditional host which can be chosen by the user and is stored in /etc/hostname file.

Transient – The transient hostname is maintained by the kernel and can be changed by DHCP and mDNS.

Pretty – It is a free-form UTF -8 hostnames for the presentation to the user.

Hostname can be,

  • 64 character in a length
  • Recommend to have FQDN
  • Consists of a-z, A-Z, 0-9, “-”, “_” and “.” only

Change Hostname in CentOS 7

Before changing the hostname, let’s check the current hostname.

[root@localhost ~]# hostname
localhost.localdomain

1. hostnamectl

hostnamectl is used to change the hostname, with this tool we can change all the three class of hostname.

Here we look only static hostname. Check the current hostname.

[root@localhost ~]# hostnamectl status

Output:

Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

Set the hostname.

[root@localhost ~]# hostnamectl set-hostname client.itzgeek.com

Check the hostname again (Close the session and open new session using putty or console)

[root@client ~]# hostnamectl status

Output:

Static hostname: client.itzgeek.com
Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

If you use this command, you do not require to notify the change of the host name. Close the current session and relaunch the terminal.

2. nmtui tool

NetworkManager tool is used to set the static hostname in /etc/hostname file.

nmtui -Select Set HostName

Set the hostname.

nmtui – Change HostName 2

Restart the hostnamed to force the hostnamectl to notice the change of the static hostname.

[root@client ~]# systemctl restart systemd-hostnamed

You can verify the change in the hostname.

[root@server ~]# hostname
server.itzgeek.com
[root@server ~]# cat /etc/hostname
server.itzgeek.com
[root@server ~]# cat /etc/sysconfig/network
# Created by anaconda
HOSTNAME=server.itzgeek.com

3. nmcli tool

It can be used to query and set up the static hostname in /etc/hostname file.

Check the hostname.

[root@server ~]# nmcli general hostname
server.itzgeek.com

Change the hostname.

[root@server ~]# nmcli general hostname client.itzgeek.com

Restart the hostnamed to force the hostnamectl to notice the change of the static hostname.

[root@server ~]# systemctl restart systemd-hostnamed

4. Edit /etc/hostname

This is simple but requires a reboot of the server to take effect.

Note: Use the hostnamectl to change the hostname, which is far better than other commands and does not require to update the kernel about the change in the hostname.

centos 7networkingrhel 7
Comments (12)
Add Comment
  • tester

    For me all I needed was:

    [root@server ~]# hostnamectl set-hostname client.itzgeek.com

    and this has taken care of setting the hostname statically and on the fly.

    Thank you.

  • Zar Tejano

    Thanks for the info

  • ajftek

    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?

    • Aleksandar Kostadinov

      Possibly issue is cloud-init.
      Edit out hostname related modules from /etc/cloud/cloud.cfg

  • Pablo

    Hello,
    great information, thank you so much for sharing!

    Another possible option:

    Modify /etc/hostname
    systemctl restart systemd-hostnamed

    Close and open a session.

    Then, a server reboot isn’t needed 🙂

    Best regards,
    Pablo

  • sagar Dalvi

    Thank you Sir

  • Xaotay

    Can you show me how to change ip address in centos 7?
    Thanks!

    • Brandon Johnson

      ifconfig ‘interface name’ ‘ip address’
      like such:
      ifconfig vnet0 192.168.1.101

    • jose

      nmtui edit “interface name”
      like such:
      nmtui edit vnet0

  • Janus Rokkjær

    Thanks great article missed the hostname set-hostname hostname.tld from every other article i read about installing centos 🙂

  • mynicepc

    so what difference would it make if I use this command directly from the kernel?
    sysctl kernel.hostname=NEW_HOSTNAME

  • Zoran Destroyer

    after all this years (centuries) you still don’t have a simple standard way to change computer name in gui? What a bunch of idiots you really are.