How to Change or Set hostname on OEL/RHEL/CentOS 7

Unlike Oracle Enterprise Linux (OEL) 6, Redhat Enterprise Linux (RHEL) 6 or CentOS 6, manually changing “HOSTNAME=xxxxx” into file /etc/sysconfig/network and restarting system simply will not work on OEL/RHEL/CentOS 7. The procedure to totally different from the previous version.

For those looking for OEL/RHEL/CentOS version 6 instructions, see my other post (How to change hostname on OEL/RHEL/CentOS version 6).

There are 4 different methods to change/set the hostname in OEL/RHEL/CentOS 7:

  1. The recommended method is by using the hostnamectl command to control the system hostname.
  2. Using the NetworkManager text based user interface (nmtui)
  3. Using the NetworkManager command-line tool (nmcli)
  4. Editing the /etc/hostname file directly

Prior to changing the hostname, let's check the current hostname:

# hostname

Method 1 : Using hostnamectl (recommended)

hostnamectl is used to change the system hostname. With this tool we can change all three classes of hostname (static, transient and pretty)

  1. We can also use hostnamectl to check the current hostname:
    # hostnamectl status
       Static hostname: schlumpf.localdomain
       Pretty hostname: RHEL 7 Lab System
             Icon name: computer-vm
               Chassis: vm
            Machine ID: b7cd503ff56bf007dd3e3612482bb2aa
               Boot ID: 5eab5023f20640c8855a15e10cad767f
        Virtualization: kvm
      Operating System: Red Hat Enterprise Linux Server 7
           CPE OS Name: cpe:/o:redhat:enterprise_linux:7:GA:server
                Kernel: Linux 3.10.0-693.1.1.el7.x86_64
          Architecture: x86-64
  2. To set the new hostname, we issue the command:
    hostnamectl set-hostname hostname
    For example:
    # hostnamectl set-hostname pitufo

    Note: If you use hostnamectl, you do not need to notify the change of the hostname to the system.

Method 2 : Using nmtui

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

  1. Start the NetworkManager text based tool by issuing the nmtui command on your terminal window.
    # nmtui
  2. Select Set system hostname from within nmtui: nmtui system hostname
  3. Set the hostname to match your new FQDN hostname, and select OK: nmtui change hostname
  4. Restart the hostnamed to force the hostnamectl to notice the change of the static hostname:
    # systemctl restart systemd-hostnamed
  5. Verify the change on the hostname:
    # hostname
    # cat /etc/hostname
    # cat /etc/sysconfig/network
    # Created by anaconda

Method 3 : Using nmcli

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

  1. To check the hostname:
    # nmcli general hostname
  2. Change the hostname:
    # nmcli general hostname pitufo
  3. Restart the hostnamed to force the hostnamectl to notify the change of the static hostname:
    # systemctl restart systemd-hostnamed

Method 4 : Editing /etc/hostname

Note: This is the simplest method but does require a reboot of the server.

  1. View the current content:
    # cat /etc/hostname
  2. To change the hostname to pitufo replace the content of the file /etc/hostname:
    # echo "pitufo" >/etc/hostname
    # cat /etc/hostname
  3. Restart the system:
    # shutdown -r now
  4. Verify the change:
    # hostname