This guide explains how to change the DNS servers on a Debian 12 system. DNS (Domain Name System) is used to resolve domain names (like example.com) into IP addresses.
You can temporarily change DNS by editing the /etc/resolv.conf file directly. This change will be lost after reboot or network restart.
-
Open the file with a text editor:
sudo nano /etc/resolv.conf
-
Add or edit nameserver entries. For example:
nameserver 1.1.1.1 nameserver 8.8.8.8 -
Save and exit (press
CTRL+O, thenEnter, thenCTRL+X). -
Test DNS resolution:
ping google.com
systemd-resolved or the dhclient. So changes here might get overwritten.
sudo nano /etc/systemd/resolved.confAdd or update the [Resolve] section like this:
[Resolve]
DNS=1.1.1.1 8.8.8.8
FallbackDNS=9.9.9.9You can change the IPs to any DNS servers you prefer.
sudo systemctl restart systemd-resolvedIf /etc/resolv.conf is not linked properly, link it to systemd-resolved:
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.confCheck the link:
ls -l /etc/resolv.confIf your server uses NetworkManager, you can change DNS per connection.
-
List connections:
nmcli connection show
-
Set custom DNS:
nmcli connection modify <connection-name> ipv4.dns "1.1.1.1 8.8.8.8" nmcli connection modify <connection-name> ipv4.ignore-auto-dns yes
-
Restart the connection:
nmcli connection down <connection-name> && nmcli connection up <connection-name>
| Provider | DNS IPs |
|---|---|
| Cloudflare | 1.1.1.1, 1.0.0.1 |
8.8.8.8, 8.8.4.4 |
|
| Quad9 | 9.9.9.9, 149.112.112.112 |
Run the following command to check which DNS is being used:
systemd-resolve --statusOr check /etc/resolv.conf:
cat /etc/resolv.conf-
If your DNS doesn't seem to update, make sure no other service (like
dhclientorNetworkManager) is overwriting it. -
Restart your networking or reboot the system if needed:
sudo systemctl restart systemd-networkd