How to Set Hostname Permanently In RHEL 7

You can use any of the following ways to setup a new host name for RHEL 7 servers

Using hostnamectl command

You can set the hostname of RHEL using the hostnamectl command.

Step1: View the current hostname

hostnamectl status

step2: Set the hostname using the following commands. One for static hostname and other for pretty hostname.

hostnamectl set-hostname [your-hostname-here]
hostnamectl set-hostname "[your hostname with spces]"

For example,

hostnamectl set-hostname devopscube #static hostname
hostnamectl set-hostname "DevOpsCube Labs" #pretty hostname

Step 3: Now you can check the changed hostnames using the following command.

hostnamectl

A sample output is shown below.

   [devopscube@demo ~]$ hostnamectl
   Static hostname: devopscube
   Pretty hostname: DevOpsCube Labs
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b7cd503ff56bf007dd3e3612482bb2aa
           Boot ID: 5eab5023f20640c8855a15e10cad767f
    Virtualization: kvm
  Operating System: Red Hat Enterprise Linux Server 7.4 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.4:GA:server
            Kernel: Linux 3.10.0-693.1.1.el7.x86_64
      Architecture: x86-64

Using nmcli command

nmcli is a networkmanager command. You can manage the hostname of the system using nmcli.

Step1: You can view the current hostname using the following command.

nmcli general hostname

Step 2: Use the following nmcli command for setting up the new hostname.

nmcli general hostname [your-hostname-here]

For example,

nmcli general hostname devopscube

Step 3: Restart the hostnamed service

service systemd-hostnamed restart

Using nmtui command

nmtui command will let you set the hostname using the GUI.

Just execute the following command and it will guide you through the process of setting up a new hostname.

sudo nmtui 

An example GUI window,