Setting Up Hostname and Name Resolution

To communicate with other hosts, hostnames are used. As an administrator, it is important that you know how to set the hostname. You also need to make sure that hosts can contact one another based on hostnames by setting up hostname resolution. In this section, you learn how to do that.

Hostnames

Because hostnames are used to access servers and the services they’re offering, it is important to know how to set the system hostname. A hostname typically consists of different parts. These are the name of the host and the DNS domain in which the host resides. These two parts together make up for the fully qualified domain name (FQDN), which looks like server1.example.com. It is good practice to always specify an FQDN, and not just the hostname. There are different ways to change the hostname:

 Use nmtui and select the option Change Hostname.

 Use hostnamectl set-hostname.

 Edit the contents of the configuration file /etc/hostname.

To configure the hostname with hostnamectl, you can use a command like hostnamectl set-hostname myhost.example.com. After setting the hostname, you can use hostnamectl status to show the current hostname. Listing 8.8 shows the output of this command.

Listing 8.8 Showing Current Hostname Configuration

Click here to view code image


[root@server2 ~]# hostnamectl status
   Static hostname: server2.example.com
         Icon name: computer
           Chassis: n/a
        Machine ID: 708ab34cdfca454d908224e0b37a8bf6
           Boot ID: 9fa3baf6fe46420aaa44c324a76f40b2
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.el7.x86_64
      Architecture: x86_64


The hostnamectl command is new in RHEL 7. When using hostnamectl status, you see not only information about the hostname but also information about the Linux kernel, virtualization type, and much more.

Alternatively, you can set the hostname using the nmtui interface. Figure 8.3 shows the screen from which this can be done.

Figure 8.3 Changing the hostname using nmtui.

To set host name resolution, DNS is typically used. Configuring a DNS server is not an RHCSA objective. Apart from DNS, you can configure host name resolution in the /etc/hosts file. Listing 8.9 shows the contents of an /etc/hosts file.

Listing 8.9 /etc/hosts Sample Contents

Click here to view code image


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


All hostname - IP address definitions as set in /etc/hosts will be applied before the hostname in DNS is used. This is configured as a default in the hosts line in /etc/nsswitch.conf, which by default looks like this:

hosts:   files dns

Setting up an /etc/hosts file is easy; just make sure that it contains at least two columns. The first column has the IP address of the specific host, and the second column specifies the hostname. The hostname can be provided as a short name (like server1), or an FQDN. In an FQDN, the hostname as well as the complete DNS name are included, as in server1.example.com.

If a host has more than one name, like a short name and a fully qualified DNS name, you can specify both of them in /etc/hosts. In that case, the second column must contain the FQDN, and the third column can contain the alias. Listing 8.10 shows a hostname configuration example.

Listing 8.10 /etc/hosts Configuration Example

Click here to view code image


[root@server2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.10     server1.example.com     server1
10.0.0.20     server2.example.com     server2

DNS Resolving

Just using an /etc/hosts file is not enough for name resolution if you want to be able to communicate with other hosts on the Internet. You should use DNS, too. To specify which DNS server should be used, set the DNS server via NetworkManager. The NetworkManager configuration stores the DNS information in the configuration file for the network connection, which is in /etc/sysconfig/network-scripts, and from there pushes the configuration to the /etc/resolv.conf file, which is used for DNS name server resolving.

It is recommended to always set up at least two DNS name servers to be contacted. If the first name server does not answer, the second name server is contacted. So, this is why you want to use a second name server. If the first name server times out or cannot be reached, the second server is used. To specify which DNS name servers you want to use, you have a few different options:

 Use nmtui to set the DNS name servers. Figure 8.4 shows the interface from which you can do this.

Figure 8.4 Setting DNS servers from the nmtui interface

 Set the DNS1 and DNS2 in the ifcfg network connection configuration file in /etc/sysconfig/network-scripts.

 Use a DHCP server that is configured to hand out the address of the DNS name server.

 Use nmcli con mod <connection-id> [+]ipv4.dns <ip-of-dns>.

Notice that if your computer is configured to get the network configuration from a DHCP server, the DNS server is also set via the DHCP server. If you do not want this to happen, you have two options:

 Edit the ifcfg configuration file to include the option PEERDNS=no.

 Use nmcli con mod <con-name> ipv4.ignore-auto-dns yes.

To verify host name resolution, you can use the getent hosts <servername> command. This command searches in both /etc/hosts and DNS to resolve the hostname that has been specified.


Tip

With NetworkManager active you can't manually edit /etc/resolv.conf. You changes will be overwritten.