How to change DNS on Linux

#dns #linux #cli

Temporarily change DNS on Linux

  1. Edit the file /etc/resolv.conf with your favourite text editor:
vim /etc/resolv.conf
  1. Add the following lines to the file:
# Generated by NetworkManager

# Google Primary DNS
+ nameserver 8.8.8.8 

# Cloudflare Primary DNS
+ nameserver 1.1.1.1 

Permanently change DNS on linux

  1. Become root:
sudo -i
  1. Edit /etc/NetworkManager/system-connections/<connection_name> with your favourite text editor:
vim /etc/NetworkManager/system-connections/<connection_name>
  1. Add (change) the following line under the [ipv4] section:
[connection]
id=Wired connection 1
uuid=a9d84673-638a-3f89-b487-3fd64d0c7673
type=ethernet
autoconnect-priority=-999
interface-name=enp34s0
timestamp=1714051582

[ethernet]

[ipv4]
+ dns=8.8.8.8;1.1.1.2;
ignore-auto-dns=true
method=auto

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
  1. Save and exit

  2. Exit root:

exit
  1. Restart the network manager:
systemctl restart NetworkManager