Chapter 12: Networking

Exercise: Names, hosts and DNS

Look at how servera knows its own name and how it finds serverb, then plant a wrong entry in /etc/hosts and see how it overrides DNS. This is a common cause of "it works from there, but not from here".

Names, hosts and DNS

  • Show and change the host name with hostnamectl.
  • Compare getent with dig.
  • Use /etc/hosts and see that it wins over DNS.

Start the exercise

On workstation, start the exercise. It removes test entries from /etc/hosts of servera and checks its host name.

[student@workstation ~]$ lab start sa-names

Later tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.

Read the name configuration

On servera as root (sudo -i), show the short and fully qualified names, the file that stores the name, the DNS servers and the search domain, and the order of name sources.

[student@workstation ~]$ ssh student@servera
[student@servera ~]$ sudo -i
[root@servera ~]# hostname -s; hostname -f
servera
servera.lab.example.com
[root@servera ~]# cat /etc/hostname
servera.lab.example.com
[root@servera ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search lab.example.com
nameserver 172.25.250.254
[root@servera ~]# grep ^hosts /etc/nsswitch.conf
hosts:      files dns myhostname

Resolve serverb two ways

Resolve serverb with getent hosts, with dig +short, and with host. Do they agree? Find the name for the address of serverb with dig -x.

[root@servera ~]# getent hosts serverb
172.25.250.11   serverb.lab.example.com
[root@servera ~]# dig +short serverb.lab.example.com
172.25.250.11
[root@servera ~]# host serverb
serverb.lab.example.com has address 172.25.250.11
[root@servera ~]# dig +short -x 172.25.250.11
serverb.lab.example.com.

Plant a wrong entry

Append 10.1.1.1 serverb to /etc/hosts. Resolve serverb with getent and with dig, and ping it once. Which tool disagrees, and where does the ping go?

[root@servera ~]# echo "10.1.1.1 serverb" >> /etc/hosts
[root@servera ~]# getent hosts serverb
10.1.1.1        serverb
[root@servera ~]# dig +short serverb.lab.example.com
172.25.250.11
[root@servera ~]# ping -c1 -W1 serverb | head -1
PING serverb (10.1.1.1) 56(84) bytes of data.

getent and ping follow nsswitch.conf and find the local line first. dig asks DNS only, so it still gives the real address. The fully qualified name serverb.lab.example.com is not in the file, so it still resolves correctly.

Fix it

Remove the wrong line (keep the rest of the file) and check again.

[root@servera ~]# sed -i '/^10.1.1.1 serverb/d' /etc/hosts
[root@servera ~]# getent hosts serverb
172.25.250.11   serverb.lab.example.com

Use /etc/hosts on purpose

Give a name app.lab.example.com (short alias app) to the address 172.25.250.11, using /etc/hosts. Test it with getent and by pinging the name.

[root@servera ~]# echo "172.25.250.11 app.lab.example.com app" >> /etc/hosts
[root@servera ~]# getent hosts app
172.25.250.11   app.lab.example.com app
[root@servera ~]# ping -c1 app | head -1
PING app.lab.example.com (172.25.250.11) 56(84) bytes of data.
[root@servera ~]# dig +short app.lab.example.com

dig prints nothing: DNS does not know the name. Only this machine does. For a name that every machine should know, the DNS server must be changed.

Change the host name and restore it

Set the host name to alpha.lab.example.com, show it, then put back servera.lab.example.com and delete your app line.

[root@servera ~]# hostnamectl set-hostname alpha.lab.example.com
[root@servera ~]# hostname; cat /etc/hostname
alpha.lab.example.com
alpha.lab.example.com
[root@servera ~]# hostnamectl set-hostname servera.lab.example.com
[root@servera ~]# sed -i '/ app.lab.example.com app/d' /etc/hosts
[root@servera ~]# hostname
servera.lab.example.com
[root@servera ~]# exit
[student@servera ~]$ exit

Grade and finish

Check your work with lab grade sa-names, then put the exercise away with lab finish sa-names.