Bookmark this page

Configure Hostnames and Name Resolution

Objectives

Configure a server's static hostname and its name resolution and test the results.

Update the System Hostname

The hostname command displays or temporarily modifies the system's fully qualified hostname.

[root@host ~]# hostname
host.example.com

Specify a static hostname in the /etc/hostname file. Use the hostnamectl command to modify this file, and view the system's fully qualified hostname. If this file does not exist, then the hostname is set by a reverse DNS query when an IP address is assigned to the interface.

[root@host ~]# hostnamectl hostname host.example.com
[root@host ~]# hostnamectl status
   Static hostname: host.example.com
         Icon name: computer-vm
           Chassis: vm 🖴
        Machine ID: ace63d6701c2489ab9c0960c0f1afe1d
           Boot ID: 0edf5ba1830c48adbd6babfa08f0b867
    Virtualization: kvm
  Operating System: Red Hat Enterprise Linux 9.0 (Plow)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:9::baseos
            Kernel: Linux 5.14.0-70.13.1.el9_0.x86_64
      Architecture: x86-64
   Hardware Vendor: Red Hat
    Hardware Model: OpenStack Compute
[root@host ~]# cat /etc/hostname
host.example.com

Important

In Red Hat Enterprise Linux 7 and later versions, the static hostname is stored in the /etc/hostname file. Red Hat Enterprise Linux 6 and earlier versions store the hostname as a variable in the /etc/sysconfig/network file.

Configure Name Resolution

The stub resolver converts hostnames to IP addresses or the reverse. It determines where to look based on the configuration of the /etc/nsswitch.conf file. By default, it attempts to resolve the query by first using the /etc/hosts file.

[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 tests hostname resolution with the /etc/hosts file. If an entry is not found in the /etc/hosts file, then the stub resolver uses a DNS name server to look up the hostname. The /etc/resolv.conf file controls how this query is performed:

  • search : A list of domain names to try with a short hostname. Either search or domain should be set in the same file; if they are both set, then only the last entry takes effect. See resolv.conf(5) for details.

  • nameserver : The IP address of a name server to query. Up to three name server directives can be given to provide backups if one name server is down.

[root@host ~]# cat /etc/resolv.conf
# Generated by NetworkManager
domain example.com
search example.com
nameserver 172.25.254.254

NetworkManager uses DNS settings in the connection configuration files to update the /etc/resolv.conf file. Use the nmcli command 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/NetworkManager/system-connections/ID
...output omitted...
[ipv4]
...output omitted...
dns=8.8.8.8;
...output omitted...

The default behavior of the nmcli con mod ID ipv4.dns IP command is to replace any previous DNS settings with the new IP list that is provided. A plus (+) or minus (-) character in front of the nmcli command ipv4.dns option adds or removes an individual entry, respectively.

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

In the following example, add the DNS server with an IPv6 IP address of 2001:4860:4860::8888 to the list of name servers on the static-ens3 connection.

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

Note

Static IPv4 and IPv6 DNS settings become nameserver directives in /etc/resolv.conf. On a dual-stack system, keep listed at least one IPv4-reachable and an IPv6 name server (assuming a dual-stack system), in the event of networking issues with either stack.

Test DNS Name Resolution

The host HOSTNAME command can 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.

Important

DHCP automatically rewrites the /etc/resolv.conf file when interfaces are started, unless you specify ignore-auto-dns = yes in the relevant interface configuration file in the /etc/NetworkManager/system-connections/ directory.

Set this entry by using the nmcli command.

[root@host ~]# nmcli con mod "static-ens3" ipv4.ignore-auto-dns yes

Use the dig HOSTNAME command to test DNS server connectivity.

[root@host ~]# dig classroom.example.com

; <<>> DiG 9.16.23-RH <<>> classroom.example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3451
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 13, ADDITIONAL: 27

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 947ea2a936353423c3bc0d5f627cc1ae7147460e10d2777c (good)
;; QUESTION SECTION:
;classroom.example.com.		IN	A

;; ANSWER SECTION:
classroom.example.com.	85326	IN	A	172.25.254.254
...output omitted...

Neither the host nor the dig commands view the configuration in the /etc/hosts file. To test the /etc/hosts file, use the getent hosts HOSTNAME command.

[root@host ~]# getent hosts classroom.example.com
172.25.254.254  classroom.example.com

References

nmcli(1), hostnamectl(1), hosts(5), getent(1), host(1), dig(1), getent(1), and resolv.conf(5) man pages

For more information, refer to the Configuring and Managing Networking Guide at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_and_managing_networking/index

Revision: rh124-9.0-398f302