12

I'm trying to add a search line to /etc/resolv.conf

I've added it directly, as an append command in /etc/dhcp/dhclient.conf and as a nameservers block in /etc/netplan/50-cloud-init.yaml.

After roughly an hour, the dhcp and netplan files are intact, but resolv.conf has reverted to not having my search.

I haven't changed /etc/network/interfaces because it says "ifupdown has been replaced by netplan(5) on this system."

Any thoughts on what might be overwriting /etc/resolv.conf besides those two things?

This is ubuntu 18.04 on EC2.

dspeyer
  • 303
  • 4
    does yours not have a "This file is managed by man:systemd-resolved(8). Do not edit." at the top? :-P – Rinzwind Jun 28 '19 at 19:55

2 Answers2

20

There is a complex competition to get control of the resolv.conf file, it is a very old competition.

Contenders that try to write a resolv.conf are resolvconf, dhcp, interfaces, network manager and recently systemd-resolved. Other programs also may use resolv.conf, like dnsmasq.

Thus, a simple solution doesn't work in all cases.

  • If you have the resolvconf program installed (whose main goal is to take ownership of the resolv.conf file) then: un-install it.

  • If your system uses DHCP to get a working IP (most probably you do), every hour or so (depending on system configuration) the IP gets renewed, that re-writes resolv.conf. Detect if this is the source of the problem.

  • The file /etc/interfaces may be used to change the resolv.conf configuration. Find out if it is (and erase it).

  • Network Manager could be configured to change what resolv.conf does. Detect (and erase) if it is doing so.

  • Systemd-resolved may be configured to take control of resolv.conf via a sym-link. Remove the link if it exists.

  • Some recommend to make resolv.conf not modifiable (I believe that is more a problem than a solution). Remove it if it exists.

After you have removed all the above: decide who should keep control of the resolv.conf file understanding that DHCP could update the file when a new DHCP lease is obtained. If the ISP (or upstream dhcp server owner) dns server is the one that should be used. DHCP leases could be configured to change the IP but not update the resolv.conf file, or an alternate dnsmasq/resolv.conf could be used if a local (127.0.0.1) DNS server (well, mostly like a catching server) is setup with dnsmasq. Of course, more complex configurations could be built with bind9, Unbound, NSD and many others.

Ask for more help if needed.

Related:

  1. How to stop dhclient from updating resolvconf on Debian?
  2. How do I stop Debian from overwriting /etc/resolv.conf and overwriting my VPN's nameservers?
  3. resolv.conf overwritten every time
  4. What overwrites /etc/resolv.conf on every boot?
  5. What causing resolv.conf overwritten ?
  6. CentOS 7 NetworkManager Keeps Overwriting /etc/resolv.conf
  7. How do I include lines in resolv.conf that won't get lost on reboot?
  8. Arch linux OpenResolv.
  • I'd love your help on this related question. Thanks! https://askubuntu.com/questions/1453982/ubuntu-22-04-1-nameservers-keep-getting-overwritten – Ryan Feb 08 '23 at 03:23
3

/etc/resolv.conf is a dynamic configuration file generated by the resolvconf process:

$ head /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1

This file is generated from the files located under /etc/resolvconf/resolv.conf.d/. To write your own changes to the contents of the /etc/resolv.conf simply add the lines that you would usually add to the resolv.conf file instead to the /etc/resolvconf/resolv.conf.d/base file.

Thegs
  • 686
  • I don't have resolvconf installed, so your answer is wrong for me and anyone else who also doesn't use resolvconf. You sound like someone who doesn't know that there was a time where you manually set up /etc/resolv.conf and NO PROCESS changed it. Ah, those were the days… – Jürgen A. Erhard Nov 15 '22 at 17:27
  • @JürgenA.Erhard I cut my teeth on Solaris 9, so you are incorrect on that. However the question is about and is tagged Ubuntu, which does use resolvconf by default. If neither my answer nor the higher rated answer contain the answer for your problem, please open a new question instead of bemoaning that an answer I wrote for someone else three years ago does not resolve your unrelated issue. – Thegs Nov 15 '22 at 18:14