resolvectl and DNS on Ubuntu: how your machine looks up domain names

  1. Introduction
  2. systemd-resolved sits in the middle
  3. Viewing the state with resolvectl status
  4. Trying a domain lookup
  5. Comparing with dig
  6. Temporarily changing DNS to test
  7. Clearing the cache
  8. The service's global configuration
  9. Quick diagnosis in order
  10. In summary
  11. References

Introduction

When you type example.com into a browser, the computer has to turn that name into an IP address before it can connect.

Turning a name into an address is called name resolution and is handled by the DNS system. When "the network is up but I can't reach websites", it is very often a DNS problem.

This article explains how an Ubuntu 24.04 LTS machine looks up DNS, and how to use resolvectl to inspect, test and diagnose it.

If your machine does not yet have the right address and DNS servers, review how to declare addresses and DNS in netplan before diagnosing further.

systemd-resolved sits in the middle

On Ubuntu, the systemd-resolved service acts as the name resolver for the whole machine.

Programs do not ask a remote DNS server themselves; they ask a "middleman" running right on the machine at the address 127.0.0.53. This middleman:

That is why, when you open /etc/resolv.conf, you usually see only the line nameserver 127.0.0.53.

That is not an error: this file is a link to /run/systemd/resolve/stub-resolv.conf, and the list of real DNS servers lives elsewhere. To see them, use resolvectl.

Viewing the state with resolvectl status

resolvectl status

The output is split into a global part (Global) and each network interface (Link). For each interface you will see information such as:

To view just one interface, add the interface name: resolvectl status enp1s0. This is the first step when you suspect DNS: is the DNS server what you think it is?

Trying a domain lookup

resolvectl query example.com

The command prints the addresses it found, and tells you which protocol was used and which network interface answered.

If an address came from the cache, the result also states the data source as the cache. Run it twice in a row and you will see the difference between a fresh query and one served from the cache.

You can ask for a specific record type, for example a mail record (MX) or IPv6 (AAAA):

resolvectl query --type=MX example.com
resolvectl query --type=AAAA example.com

You don't need to remember all the options; see resolvectl --help or man resolvectl when needed.

Comparing with dig

If you want to ask one specific DNS server directly for comparison, install the bind9-dnsutils package and then use dig:

sudo apt install bind9-dnsutils
dig @192.0.2.53 example.com

The @ sign specifies the server to ask. If dig answers correctly but resolvectl query is wrong, the problem most likely lies in the configuration on your machine, not in the DNS server.

Temporarily changing DNS to test

You can assign a DNS server to a network interface without editing a configuration file:

sudo resolvectl dns enp1s0 192.0.2.53
sudo resolvectl domain enp1s0 example.com

This change only lasts until the network is reconfigured or the machine restarts. To drop it right away, run:

sudo resolvectl revert enp1s0

To change it permanently, edit the nameservers entry in the netplan file in netplan and then apply it.

Clearing the cache

When you have just edited a DNS record but the machine still returns the old address, clear the cache:

sudo resolvectl flush-caches

The resolvectl statistics command shows the number of cache hits, the number of queries sent, and the overall state of the service.

The service's global configuration

The /etc/systemd/resolved.conf file holds global settings, for example the list of fallback DNS servers (FallbackDNS) or DNSSEC. The lines in the default file are usually commented out with #.

After editing, restart the service:

sudo systemctl restart systemd-resolved

Warning: a wrong DNS configuration can leave the whole machine unable to resolve any name. Back up the file before editing and keep a spare login session open.

Quick diagnosis in order

  1. resolvectl status: is it the right DNS server yet?

  2. resolvectl query example.com: can it look the name up?

  3. If not, try calling the IP address directly with ping. If pinging the IP works but pinging the name does not, the fault is in DNS, not in the connection.

  1. dig @<server> example.com: does the DNS server answer?

  2. sudo resolvectl flush-caches and then try again.

The connection-checking steps in item 3 are explained in Checking network connectivity on Ubuntu with ip, ss, ping and tracepath.

In summary

On Ubuntu 24.04, systemd-resolved is the name resolver for the whole machine, listening at 127.0.0.53.

Use resolvectl status to see the real DNS servers, resolvectl query to test a name lookup, resolvectl flush-caches to clear the cache, and make lasting DNS changes in the netplan configuration.

References

resolvectl and DNS on Ubuntu: how your machine looks up domain names