Clarifying four modes of handling /etc/resolv.conf in systemd-resolved

systemdsystemd-resolved

I have read about systemd-resolved.service https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html and learnt four modes of handling /etc/resolv.conf.

  1. /run/systemd/resolve/stub-resolv.conf
  2. /usr/lib/systemd/resolv.conf
  3. /run/systemd/resolve/resolv.conf
  4. /etc/resolv.conf may be managed by other package

I have read it for several times, but still feel confused about how to determine which mode of /etc/resolv.conf I should choose as a normal user.

For example, I try to add some custom dns servers, so,

  1. Add DNS=8.8.8.8 8.8.4.4 in /etc/systemd/resolved.conf and check /run/systemd/resolve/resolv.conf, 8.8.8.8 and 8.8.4.4 exist in it.
  2. If symlinking /run/systemd/resolve/resolv.conf to /etc/resolv.conf, 8.8.8.8 and 8.8.4.4 are gone in
    /run/systemd/resolve/resolv.conf.

Update 1:

test@instance-1:~$ cat /run/systemd/resolve/resolv.conf 
...
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 8.8.8.8
nameserver 8.8.4.4

test@instance-1:/etc$ sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf 
test@instance-1:/etc$ ls -alh /etc/resolv.conf 
lrwxrwxrwx 1 root root 32 Mar 18 07:22 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
test@instance-1:/etc$ sudo reboot

test@instance-1:~$ cat /etc/resolv.conf 
domain c.prime-poetry-197705.internal
search c.prime-poetry-197705.internal. google.internal.
nameserver 169.254.169.254

test@instance-1:~$ cat /run/systemd/resolve/resolv.conf 
domain c.prime-poetry-197705.internal
search c.prime-poetry-197705.internal. google.internal.
nameserver 169.254.169.254

test@instance-1:~$ ls -alh /etc/resolv.conf 
lrwxrwxrwx 1 root root 32 Mar 18 07:22 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf

Update 2:
symlinking from /etc/resolv.conf

test@instance-1:~$ sudo ln -sf /etc/resolv.conf /run/systemd/resolve/resolv.conf 
test@instance-1:~$ ls -alh /run/systemd/resolve/resolv.conf 
lrwxrwxrwx 1 root root 16 Mar 18 07:51 /run/systemd/resolve/resolv.conf -> /etc/resolv.conf
test@instance-1:~$ sudo reboot

test@instance-1:~$ ls -alh /run/systemd/resolve/resolv.conf 
-rw-r--r-- 1 systemd-resolve systemd-resolve 603 Mar 18 07:52 /run/systemd/resolve/resolv.conf

Best Answer

My guess is that you are getting your IP configuration from DHCP, which overrides the DNS information in your resolved.conf file (from systemd.network(5)):

[DHCP] SECTION OPTIONS

[...]

UseDNS= When true (the default), the DNS servers received from the DHCP server will be used and take precedence over any statically configured ones.

This corresponds to the nameserver option in resolv.conf(5).

Try adding the following to your {networkname}.network file (in /etc/systemd/network):

[DHCP]
UseDNS=false
Related Question