9

I'm using Ubuntu 11.10 and I manually configure DNS servers in /etc/resolv.conf but it gets somehow overwritten after I reboot. How can I prevent this?

Thanks.

mkhezr
  • 309
  • 1
  • 2
  • 7

8 Answers8

14

As you can read in the header of resolv.conf :

Dynamic resolv.conf file for glibc resolver generated by resolvconf

So, the resolv.conf is generated, if you want to keep the resolvconf configuration after reboot, you will have to edit /etc/resolvconf/resolv.conf.d/base. In that file, put in your info as you would in resolv.conf.

nameserver 8.8.8.8

Then regenerate resolv.conf with resolvconf:

sudo resolvconf -u
6

After reading other answers, I still needed something different for the following reasons:

  • I'm not using resolvconf, just plain /etc/resolv.conf.
  • Using chattr +i to lock down resolv.conf seems too hacky. I need Puppet to be free to make changes when necessary.

The best solution I found overrides the default behavior of dhclient using its documented hooks.

Create a new file at /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate with the following contents:

#!/bin/sh
make_resolv_conf() {
    :
}

Then make the file executable:

chmod +x /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate

Now when dhclient runs -- either on reboot or when you manually run sudo ifdown -a ; sudo ifup -a -- it loads this script nodnsupdate. This script overrides an internal function called make_resolv_conf() that would normally overwrite resolv.conf and instead does nothing.

This worked for me on Ubuntu 12.04.

richardkmiller
  • 2,902
  • 3
  • 31
  • 29
  • If you use 'chattr +i', some VPN SW (I.E.: pulse secure) could have problems when they try to set DNS to point to specific server addresses (I.E.: corporate ones). – Sir Jo Black Dec 05 '22 at 15:01
2

I figure that the NetworkManager is overwriting the /etc/resolv.conf file. In my case it was the order in which my DNS servers were listed was something I wanted to change. You can do that through the NetworkManager by editing your connection IP4V settings.

mkhezr
  • 309
  • 1
  • 2
  • 7
0

You have DHCP client doing this. Follow these instructions to override that.

jman
  • 11,334
  • 5
  • 39
  • 61
  • I tried the solution you linked to, but it does not work. It looks like the Network Manager is doing this as the first line in /etc/resolv.conf reads : "# Generated by NetworkManager". Generally, Is there a way to know the programs editing /etc/resolv.conf? – mkhezr Mar 12 '13 at 17:55
0

I use the following line : chattr +i /etc/resolv.conf

to undo use : chattr -i /etc/resolv.conf

Let me know if it worked...

0

NetworkManager can be configured to use manually entered IPv4 configuration, or get from DHCP IP/netmask/router only - in such a case it should not change /etc/resolv.conf

However, one may want have his own settings in the /etc/resolv.conf - like nameserver or domain to search; I just need a domain and I did by adding a file /etc/NetworkManager/dispatcher.d/99my_fix containing:

#!/bin/bash rc=/etc/resolv.conf; le="search my.domain" grep -q domain $rc && ! grep -q "$le" $rc && echo "$le" >> $rc

Of course I chmod-ed +x it. The NetworkManager invokes it after setting the /etc/resolv.conf and my script fixes it if nesessary; the first grep detects that network is up, the second that the fix was not applied - they both are necessary for the fix to be applied.

Jerzy
  • 1
0

I had the same problem and I edited my `/etc/dhcp/dhclient.conf' file by adding domain-name and domain-name-servers

supersede domain-name "local.com"; supersede domain-name-servers 192.168.56.103;

192.168.56.103 is my vm running bind9 and my domain name is local.com

and I have removed the same from request section as well.

san1512
  • 914
  • 1
  • 9
  • 16
0

If the network interfaces for your server instance is controlled by DHCP, the dhclient program will overwrite your /etc/resolv.conf file whenever the networking service is restarted.

You can fix the issue by editing the "/etc/dhcp/dhclient.conf" file and adding supersede statements for domain-name, domain-search and domain-name-servers as follows:

supersede domain-name "mydomain.com";
supersede domain-search "mydomain.com"
supersede domain-name-servers 8.8.8.8;

In this particular case the name server is located at "8.8.8.8" and the domain name is "mydomain.com". Substitute your particular information.

Note that each line is terminated by a semi-colon and the domain name is enclosed in double quotes.

  • I have a related question: https://askubuntu.com/questions/1453982/ubuntu-22-04-1-nameservers-keep-getting-overwritten – Ryan Feb 08 '23 at 03:11