How to make name server address permanent in /etc/resolv.conf?

Solution 1:

It depends. Please read to the end.

If you do not have the package resolvconf installed, then it is easy: after changing it the way you like, issue the command

 sudo chattr +i /etc/resolv.conf

The command changes the file attributes, making it immutable (the +i option).

However, if you have the package resolvconf installed, the file /etc/resolv.conf may be transformed into a symbolic link,

 # file /etc/resolv.conf
 /etc/resolv.conf: symbolic link to `../run/resolvconf/resolv.conf'

which is a file on a virtual file system, the epitomy of a non-permanent version. The funny thing is that you may not know you have resolvconf installed, because recent versions of Debian-like OSes come with it pre-installed. You can establish whether your distro has installed it for you by issuing the command:

 #whereis resolvconf
 resolvconf: /sbin/resolvconf /etc/resolvconf /lib/resolvconf /usr/share/man/man8/resolvconf.8.gz

Even most importantly, you can establish whether your /etc/resolv.conf has been transformed into a link by means of the command (file /etc/resolv.conf) above. If the reply is ASCII text, use chattr, otherwise...

If you use a static IP, the simplest thing is to add the following line (or some such thing)

 dns-nameservers 8.8.4.4 8.8.8.8

to the stanza defining the properties of your static interface in /etc/network/interfaces.

If you instead are on a laptop which connects all the time to different networks,you may follow LawrenceC's excellent suggestion. But, if you have resolvconf on your system, the correct (and simplest) way to provide for a fixed set of DNSs is to use the files in /etc/resolvconf/resolv.conf.d. In particular, the following files are used (see Stephane Graber's page):

base: Used when no other data can be found

head: Used for the header of resolv.conf, can be used to ensure a DNS server is always the first one in the list

tail: Any entry in tail is appended at the end of the resulting resolv.conf.

So place you favorite nameservers in head, as follows

  nameserver 8.8.8.8

and you are done.

Solution 2:

Add a similar line to /etc/dhcp/dhclient.conf using a root terminal:

supersede domain-name-servers 74.122.198.48, 50.116.23.211;

This causes dhclient, the program that issues a DHCP request and makes changes according to what the response told it, to replace the information it got from the DHCP server with the above before making changes.

The above are two OpenNIC DNS servers. Replace as needed for whatever servers you want to use.

Since this happens whenever you get config info from DHCP, so use dhclient -v eth0 or whatever interface to refresh it and see the change.

Solution 3:

It appears as mentioned by @Rik that the reason for DNS replacements on reboot is due to the network manager / utility that is set so. In Linux Mint, as per screen shot, I had my IPv4 method set to Automatic (DHCP) addresses only. You can then either enter the correct settings in /etc/resolv.conf or at the DNS servers section. I remember in Ubuntu 13.04 my name servers changed a few times over a long period of time, but after I actually went and edited them in the GUI, all seemed to be stable from there on forward.

enter image description here

enter image description here

Solution 4:

Had the same problem, but i use static IP address. The fix was just adding the dns in /etc/network/interfaces

iface eth0 inet static
    address [your ip>
    netmask [your netmask]
    network [your network]
    broadcast [your broadcast]
    gateway [your gateway]
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers [dns server1] [dns server2]
    dns-search [your domain]