DNS Basics: Hostname Resolution

When you access a website, you type a URL that contains a hostname, for example www.linux‑magazine.com. The browser will then contact the web server that hosts the website, which normally means it will try to establish an HTTPS connection to the HTTPS port (443) on that machine. Before it can contact www.linux‑magazine.com, the operating system first needs to resolve that name to an IP address: the Internet Protocol (IP) uses numeric addresses, either IPv4 or IPv6.

Address resolution (from a hostname to an IP address) can be handled in several ways: The operating system may find the name and address in a local hosts file (/etc/hosts), it may query your Internet router (which in turn will likely query your ISP), or it may ask an alternative Domain Name System (DNS) server that you’ve configured. DNS handles address translation in a hierarchical fashion.

To see resolution in action, you can use the dig command:

$ dig +short www.linux‑magazine.com
linux‑magazine.com.
104.237.128.147

Here dig has found the address 104.237.128.147. If dig is not installed, use ping or ping4; depending on your system’s setup ping can return an IPv6 address instead of an IPv4 address, for example 2600:3c00::f03c:91ff:fe89:28c1.

Block Ad Hosts via /etc/​hosts

You can use the /etc/hosts file to overwrite the addresses of hostnames. This is often used to block notorious ad servers: Just add a line for each hostname and assign it your localhost IP address 127.0.0.1, like this:

127.0.0.1 notorious.adserver.com

When a web page contains links to ads on that server, the browser will no longer download them but try to connect to your local machine instead (which won’t return anything unless you run a web server on your computer). The figure shows how dig gives a different answer if you list www.linux‑magazine.com that way. This overwriting can be disabled in /etc/host.conf where you will typically find a line order hosts, bind that makes Linux look in the hosts file first.

Figure 1

If you want your system to resolve a hostname differently, add a line to /​etc/​hosts.

Enable Local DNS Server

Most modern distributions use systemd, which can provide a lot of services: it is much more than a replacement for the old SysVinit-style initialization. One of the services it offers is DNS. If installed and enabled, it will act as a DNS cache – that means it will ask another DNS server when it does not know an IP address, but it will memorize previous lookups, so that it won’t ask for the same address again and again. This should speed up loading web pages. (The information will time out eventually.) Check if it’s in use with:

resolvectl status

If it’s working, it will display information about the DNS server to which it forwards queries: You should see a line like

Current DNS Server: 192.168.178.1

in the output. If you get an error message instead, you need to enable the service, and you may also have to install it first. For example, on a current openSUSE installation, I had to install the systemd‑network package with Zypper first. Then edit /etc/systemd/resolved.conf (as root) and add a line

DNS=a.b.c.d

where a.b.c.d is the IP address of the DNS server that you’ve used before – typically, that will be your router. Run

sudo systemctl enable systemd‑resolved
sudo systemctl start systemd‑resolved

to enable the service. On the openSUSE test system, resolvectl timed out after these steps; the problem went away after a reboot. Also, even though the DNS server was active, the system did not use it. To fix that problem, open /etc/resolv.conf (again, as root), comment out any nameserver line by adding a # prefix and add

nameserver 127.0.0.53

This address, like 127.0.0.1, always points to your computer.

This article originally appeared in Cool Linux Hacks and is reprinted here with permission.

Want to read more? Check out the latest edition of Cool Linux Hacks.

 

 

FOSSlife Newsetter

Comments