loading...

Ubuntu Server 18.04 – Understanding Linux name resolution

How to Configure Network Static IP Address on Ubuntu 19.10

In Chapter 7, Setting Up Network Services, we’ll have a discussion on setting up a DNS server for local name resolution for your network. But before we get to that, it’s also important to understand how Linux resolves names in the first place. Most of you are probably aware of the concept of a Domain Name System (DNS), which matches human-understandable domain names to IP addresses. This makes browsing your network (as well as the internet) much easier. However, a DNS isn’t always the first thing that your Linux server will use when resolving names.

For more information on the order in which Ubuntu Server checks resources to resolve names, feel free to take a look at the /etc/nsswitch.conf file. There’s a line in this file that begins with the word hosts. Here is the output of the relevant line from the file on my server:

hosts:          files dns 

In this case, the server is configured to first check local files, and then the DNS if the request isn’t found. This is the default order, and I see little reason to make any changes here (but you certainly can). Specifically, the file the server will check is /etc/hosts. If it doesn’t find what it needs there, it will move on to the DNS (basically, it will check the DNS server we configured earlier or the default server provided by DHCP).

There are many other lines in the nsswitch.conf file, but I won’t discuss them here as they are out of scope of the topic of this section.

The /etc/hosts file, which we briefly discussed while working with our hostname, tells our server how to resolve itself (it has a hostname mapping to the localhost IP of 127.0.0.1), but you are also able to create additional names to IP mappings here as well. For example, if I had a server (minecraftserver.local.lan) at IP 10.10.96.124, I could add the following line to /etc/hosts to make my machine resolve the server to that IP each time, without it needing to consult a DNS server at all:

10.10.96.124 minecraftserver 

In practice though, this is usually not a very convenient method by which to configure name resolution. Don’t get me wrong, you can certainly list your servers in this file along with their IP addresses, and your server would be able to resolve those names just fine. The problem stems from the fact that this method doesn’t scale. The name mappings apply only to the server you’ve made the /etc/hosts changes on; other servers wouldn’t benefit since they would only check their own /etc/hosts file. You could add a list of servers to the hosts file on each server, but that would be a pain to manage. This is the main reason why having a central DNS server is a benefit to any network, especially for resolving the names of local resources. However, the /etc/hosts file is used every now and again in the enterprise as a quick one-off workaround, and you’ll probably eventually end up needing to use this method for one reason or another.

On legacy Ubuntu servers, there was a file, /etc/resolv.conf, that was used to determine which DNS servers to check. This is not used anymore in Ubuntu 18.04, since name resolution is now handled by systemd-resolved. For the sake of completeness though, here is a brief overview of this file in case you end up working on such a server. An example of this file is as follows:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) 
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN 
nameserver 10.10.96.1 
nameserver 10.10.96.2 

In this example, /etc/resolv.conf output is utilizing servers 10.10.96.1, and 10.10.96.2. Therefore, the server will first check /etc/hosts for a match of the resource you’re looking up, and if it doesn’t find it, it will then check /etc/resolv.conf in order to find out which server to check next. In this case, the server will check 10.10.96.1.

The network assignment on this server is actually being managed by NetworkManager, as you can see from the warning comment included in the file. You can certainly alter this file so that the server will check different DNS servers, but the next time its IP address renews, or the connection is refreshed, this file will be overwritten and will be reset to use whatever DNS servers your DHCP server tells it to use. Therefore, if you really want to control how your server resolves names you can create a static assignment, as we did earlier in this chapter when configuring Netplan. This isn’t a problem if you’re using a static IP assignment.

Now that newer Ubuntu servers utilize systemd-resolved to handle name resolution, how do you see what name servers your server is currently using? You could consult the Netplan configuration if you’re utilizing a static IP address, but if your server received an IP address via DHCP, that file won’t help you. The following command will let you know what DNS nameservers your server is currently pointing to:

systemd-resolve --status |grep DNS\ Servers
Viewing a server’s current DNS assignment

In a typical enterprise Linux network, you’ll set up a local DNS server to resolve your internal resources, which will then forward requests to a public DNS server in case you’re attempting to reach something that’s not internal. We’ll get to that in Chapter 7, Setting Up Network Services, but you should now understand how the name resolution process works on your Ubuntu Server.

Comments are closed.

loading...