14

I can not retain name server address I set via /etc/resolv.conf. Every time I set the value, the network manager overrides it with default gateway on reboot.

How to set it right?

I would prefer a way other than changing name server in router.

  • You need to change it with the NetworkManager (like it is stated on the first line). This file is re-generated on every boot by the NetworkManager. It depends on your linux-distro (which you didn't mention) how to access this NetworkManager. – Rik Nov 18 '13 at 10:08
13

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.

  • 2
    @dhiller Ultrasawblade changed his name to LawrenceC, it's the one below. – MariusMatutiae Jun 21 '16 at 9:11
  • Very nice and interesting post, I'd like you to add some documentation links about this topic for users that want to know more about the topic, Thx – Philippe Gachoud Aug 3 '18 at 13:19
  • /etc/resolvconf/ doesn't exist by default on Ubuntu 18.04. – Dan Dascalescu Oct 21 at 4:42
9

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.

  • 1
    Instead of just overriding the DNS (usually router), one could add the line prepend domain-name-servers 8.8.8.8 for example. This would use the ISP's DNS when available. – Axel Latvala Aug 23 '16 at 21:54
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

3

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]
  • Yes, that fixes it because NetworkManager ignores interfaces listed in /etc/network/interfaces. Good catch! – Izzy Nov 7 '16 at 20:58
1

If you used wicd, right click the icon and click connection info. When the window opens up, click the properties of the one you are connected to.

Check use static DNS and in the line that says DNS server 1 type in the IP for the Google DNS 8.8.8.8 and in the line for DNS server 2 type 8.8.4.4 then click "ok".

If you're receiving errors from the defaults, you can contact your ISP and mention it to them. They usually assign DNS servers to your modem when you connect with them.

0

Hacking your way in by chattr +i or similar is a quick work-around at best, and should be avoided whenever possible (at least for long-term – as said, it's a short-term work-around until one finds time for the real solution). As already pointed out, this is a job for the NetworkManager, which offers a CLI we can use for that:

# find which interface to adjust
nmcli device
# output looks like:
# DEVICE  TYPE      STATE      CONNECTION
# enp0s3  ethernet  connected  my-office
# lo      loopback  unmanaged  --
# From that, we need the CONNECTION.

# Now add our "search domains":
nmcli connection modify my-office ipv4.dns-search example1.com
nmcli con mod my-office +ipv4.dns-search example2.com

# And our name servers:
nmcli con modify my-office ipv4.dns 213.73.91.35
nmcli con modify my-office +ipv4.dns 87.118.100.175
# note the "+" in the second line: add another one,
# while the first line overwrites the existing entry.
# we could have done that with a single call:
nmcli con modify my-office ipv4.dns "213.73.91.35 87.118.100.175"

# OK, so now let's tell it to ignore updates from the DHCP server:
nmcli con modify my-office ipv4.ignore-auto-dns yes

# and apply the modifications to our running configuration
nmcli con up my-office

If you use a static IP address, you can also use the solution from kanelbolle´s answer.

0

Amazing how frustrating this little problem can get, when the solutions given for it no longer work on Ubunutu 18.04.

What worked for me was to simply overwrite the nameserver value in /etc/resolv.conf on every boot. sudo edit /etc/rc.local and add this line:

 sed -i "s/nameserver 127.0.0.53/nameserver 8.8.8.8/" /etc/resolv.confsudo /etc/rc.local

protected by Community Jul 4 '16 at 21:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.