AKZN Notes

Archives for My Lazy and Forgetful Mind

Setting Persistent IPv4 DNS for Ubuntu 20.xx

Prefer IPv4 for DNS & Connections on Ubuntu (Keep IPv6 Enabled)

On Ubuntu 20.04+, systemd-resolved and the network stack prefer IPv6 when it’s available.
Even if you set IPv4 DNS, connections may still use IPv6 (AAAA records).

This guide keeps IPv6 enabled, but makes Ubuntu prefer IPv4 for DNS and connections.


🎯 Goal

  • Keep IPv6 available
  • Use IPv4 DNS servers
  • Prefer IPv4 for outbound connections

1) Set IPv4 DNS with systemd-resolved

Edit config:

sudo nano /etc/systemd/resolved.conf

Set:

[Resolve]
DNS=8.8.8.8 1.1.1.1
FallbackDNS=8.8.4.4

Restart:

sudo systemctl restart systemd-resolved

Verify:

resolvectl status

You should see:

DNS Servers: 8.8.8.8
             1.1.1.1

2) Prefer IPv4 over IPv6 for connections

Ubuntu still prefers IPv6 for connections unless changed.

Edit:

sudo nano /etc/gai.conf

Find and uncomment:

precedence ::ffff:0:0/96  100

This tells the OS to prefer IPv4-mapped addresses over IPv6.


3) Test

ping google.com

Before:

PING ... (2404:xxxx::xxxx)

After:

PING ... (142.xxx.xxx.xxx)

Check resolution:

resolvectl query google.com

✅ Result

  • IPv6 still enabled
  • DNS uses IPv4 servers
  • Connections prefer IPv4
  • No need to disable IPv6

🧠 Notes

  • DNS config controls servers.
  • gai.conf controls IP selection order.
  • Without gai.conf, Ubuntu prefers IPv6 even with IPv4 DNS.

🔁 Revert

Comment again in /etc/gai.conf:

# precedence ::ffff:0:0/96  100