How to Configure the hostname and name resolution in CentOS/RHEL 7 and 8

Changing the system hostname

The hostname command displays or temporarily modifies the system’s fully qualified host name.

[root@host ~]# hostname [email protected]

A static host name may be specified in the /etc/hostname file. The hostnamectl command is used to modify this file and may be used to view the status of the system’s fully qualified host name. If this file does not exist, the host name is set by a reverse DNS query once the interface has an IP address assigned.

[root@host ~]# hostnamectl set-hostname [email protected]
[root@host ~]# hostnamectl status
 Static hostname: host.example.com
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 73ab164e278e48be9bf80e80714a8cd5
         Boot ID: 6b1cbc4177164ef58c0e9ed4adb2904f
  Virtualization: kvm
Operating System: Red Hat Enterprise Linux 8.0 beta (Ootpa)
     CPE OS Name: cpe:/o:redhat:enterprise_linux:8.0:beta
          Kernel: Linux 4.18.0-60.el8.x86_64
    Architecture: x86-64
[root@host ~]# cat /etc/hostname
[email protected]

Configuring Name Resolution

The stub resolver is used to convert host names to IP addresses or the reverse. It determines where to look based on the configuration of the /etc/nsswitch.conf file. By default, the contents of the /etc/hosts file are checked first.

[root@host ~]# cat /etc/hosts
127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4
::1             localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.254.254 classroom.example.com
172.25.254.254 content.example.com

The getent hosts hostname command can be used to test hostname resolution using the /etc/hosts file. If an entry is not found in the /etc/hosts file, by default the stub resolver tries to look up the hostname by using a DNS nameserver. The /etc/resolv.conf file controls how this query is performed:

  • search: a list of domain names to try with a short hostname. Both this and domain should not be set in the same file; if they are, the last instance wins. See resolv.conf(5) for details.
  • nameserver: the IP address of a nameserver to query. Up to three nameserver directives may be given to provide backups if one is down.
[root@host ~]# cat /etc/resolv.conf
# Generated by NetworkManager
domain example.com
search example.com
nameserver 172.25.254.254

NetworkManager updates the /etc/resolv.conf file using DNS settings in the connection configuration files. Use the nmcli to modify the connections.

[root@host ~]# nmcli con mod ID ipv4.dns IP
[root@host ~]# nmcli con down ID
[root@host ~]# nmcli con up ID

[root@host ~]# cat /etc/sysconfig/network-scripts/ifcfg-ID
...output omitted...
DNS1=8.8.8.8
...output omitted...

The default behavior of nmcli con mod ID ipv4.dns IP is to replace any previous DNS settings with the new IP list provided. A + or - symbol in front of the ipv4.dns argument adds or removes an individual entry.

[root@host ~]# nmcli con mod ID +ipv4.dns IP

To add the DNS server with IPv6 IP address 2001:4860:4860::8888 to the list of nameservers to use with the connection static-ens3:

[root@host ~]# nmcli con mod static-ens3 +ipv6.dns 2001:4860:4860::8888

Testing DNS Name Resolution

The host HOSTNAME command can be used to test DNS server connectivity.

[root@host ~]# host classroom.example.com
classroom.example.com has address 172.25.254.254
[root@host ~]# host 172.25.254.254 254.
254.25.172.in-addr.arpa domain name pointer classroom.example.com.
[root@host ~]# nmcli con mod "static-ens3" ipv4.ignore-auto-dns yes