Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

A Beginner's Guide to Using DNS in Linux

See which DNS servers a Linux system uses, change them the way that survives a restart, clear the cache, and find out why a name does not resolve.

2 min readBy Soumya

Reviewed by the AmazingRDP technical team

Using DNS in Linux

A server of your own is the best place to learn networking. A Linux VPS gives you root access. See Linux VPS from $6.99

DNS turns names such as example.com into the IP addresses computers connect to. When it fails, nothing seems to work, although the network itself is fine. On Linux the settings live in different places depending on the system, which is where most confusion starts.

See what your system uses

resolvectl status
cat /etc/resolv.conf

On Ubuntu, Fedora and most current systems a service called systemd-resolved handles lookups, and the first command shows the real DNS servers for each network interface. The file /etc/resolv.conf then usually shows only nameserver 127.0.0.53, the local service itself. On systems without that service, the file lists the real servers.

Look a name up

dig example.com
dig +short example.com
dig @1.1.1.1 example.com
dig -x 93.184.216.34

The first shows the full answer and which server gave it; the second only the address; the third asks a specific server, which tells you whether a problem lies with your own DNS server; the fourth looks up the name for an address. If dig is missing, install dnsutils (Debian, Ubuntu) or bind-utils (Fedora, AlmaLinux, Rocky). nslookup and host do similar jobs.

Change the DNS servers

Do not edit /etc/resolv.conf by hand on a current system: it is rewritten at the next restart or network change. Use the tool that owns the setting.

SystemWhere to set it
Ubuntu Server (Netplan)The file in /etc/netplan/
Desktops and RHEL family (NetworkManager)nmcli or the network settings window
Any system with systemd-resolved, for all interfaces/etc/systemd/resolved.conf

Netplan: add under your interface, then apply:

      nameservers:
        addresses: [1.1.1.1, 9.9.9.9]
sudo netplan apply

The full file is explained in DHCP or static IP on Ubuntu Server.

NetworkManager:

nmcli connection show
sudo nmcli connection modify "Wired connection 1" ipv4.dns "1.1.1.1 9.9.9.9" ipv4.ignore-auto-dns yes
sudo nmcli connection up "Wired connection 1"

systemd-resolved, globally: in /etc/systemd/resolved.conf set DNS=1.1.1.1 9.9.9.9 under [Resolve], then run sudo systemctl restart systemd-resolved.

Well-known public servers: Cloudflare 1.1.1.1, Google 8.8.8.8, Quad9 9.9.9.9. More in DNS servers compared.

Clear the cache

sudo resolvectl flush-caches
resolvectl statistics

Useful after a DNS record has changed and your system still returns the old address.

The hosts file

/etc/hosts is checked before DNS. A line such as 203.0.113.10 test.example.com makes that name point where you say, on this machine only. It is handy for testing a site before its DNS is switched, and a classic cause of "it works everywhere but here" when an old line is forgotten.

When a name does not resolve

  1. Is it DNS at all? ping 1.1.1.1 works and ping example.com does not: yes.
  2. Does another server answer? dig @1.1.1.1 example.com. If it does, your configured DNS server is the problem.
  3. Is the service running? systemctl status systemd-resolved.
  4. Is port 53 blocked? A firewall must allow outgoing UDP and TCP 53.
  5. Is the name overridden? Look in /etc/hosts.
  6. Only one name fails? The fault is with that domain's own DNS, not with you.

For a server to practise on, see our Linux VPS plans. Related: restart the network in Ubuntu.

Questions

How do I check which DNS server Linux is using?

Run resolvectl status. On systems without systemd-resolved, read /etc/resolv.conf.

How do I change the DNS server in Ubuntu?

On Ubuntu Server, set nameservers in the Netplan file and run sudo netplan apply. On a desktop, use the network settings or nmcli.

Why does /etc/resolv.conf keep changing back?

The file is generated by the system. Set DNS servers in Netplan, NetworkManager or systemd-resolved, which write it.

How do I flush the DNS cache in Linux?

Run sudo resolvectl flush-caches.