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.

[[email protected] ~]# 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.

[[email protected] ~]# hostnamectl set-hostname [email protected]
[[email protected] ~]# 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
[[email protected]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.

[[email protected] ~]# 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.
[[email protected] ~]# 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.

[[email protected] ~]# nmcli con mod ID ipv4.dns IP
[[email protected] ~]# nmcli con down ID
[[email protected] ~]# nmcli con up ID

[[email protected] ~]# 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.

[[email protected] ~]# 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:

[[email protected] ~]# 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.

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