Permanent DNS Settings for All Network Interfaces

DNS settings in Linux are usually stored in /etc/resolv.conf file, you could basically just edit this file to change the DNS settings in any Linux systems. However, the change is not permanent, it will be overwritten by Network Manager when u reconnect or reboot. So, to make the change permanent, there are two methods:

  • Network Manager’s dispatcher script method
  • Immutable attribute method

Method 1: Network Manager’s Dispatcher Script Method

This method will just replace copy /etc/resolv.conf with /etc/resolv.conf.googledns everytime NetworkManager connect to the network.

  1. Run sudo /etc/resolv.conf.googledns

  2. Add the following lines, I'm using Google DNS here, you can change to any DNS you want:

  3. Create and edit the file /etc/NetworkManager/dispatcher.d/12-dns_server and add the following line:

     sudo cp -f /etc/resolv.conf.googledns /etc/resolv.conf
  4. Make /etc/NetworkManager/dispatcher.d/12-dns_server executable:

     sudo chmod +x /etc/NetworkManager/dispatcher.d/12-dns_server
  5. Restart NetworkManager service: sudo systemctl restart NetworkManager

Method 2: Immutable Attribute Method

The immutable attribute is part of extended attribute added since ext2. Since resolv.conf file is automatically regenerated every time Network Manager connects, you can set the file as immutable so that it will never be replaced. You can set resolv.conf as immutable using the chattr utility as follows (Remember to edit the file and add all the DNS before setting the attribute):

sudo chattr +i /etc/resolv.conf

Whenever you wanted to change the DNS, the attribute have to be removed from the file using the following command (similar to previous command, except the +i and -i option which indicates add or remove the attribute):

sudo chattr -i /etc/resolv.conf
Consent Preferences