5

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
  • What does /run/systemd/resolve/resolv.conf contain after you apply the symlink? Without seeing the contents of your file (before and after), it's hard to say what's happening. Could you have possibly reversed the order of the files when symlinking? – ErikF Mar 18 '18 at 5:28
  • Like @ErikF said, make sure /run/systemd/resolve/resolve.conf ` is symlinked from /etc/resolv.conf, not to. I.e. /etc/resolv.conf -> /run/systemd/resolve/resolve.conf. – Johan Myréen Mar 18 '18 at 7:13
  • @JohanMyréen, I mistook the direction of symlinking, but everything under /run remain in memory and the symlink is gone after reboot, isn't it? And I still cannot find where my custom dns servers are. – netcaf Mar 18 '18 at 8:15
  • 1
    The direction in Update 1 is correct. /etc/resolv.conf should be a symlink pointing to /run/systemd/resolve/resolve.conf. – Johan Myréen Mar 18 '18 at 9:04
  • @JohanMyréen I tried it on another computer and /etc/resolv.conf was not be updated by other unknown reason. It worked as expected. Thank you. – netcaf Mar 18 '18 at 12:50
3

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
| improve this answer | |
  • I checked my system, and systemd-networkd is not enabled. So there is nothing under /etc/systemd/network. I tried to enable and start it and add configuration like you said, but /etc/resolv.conf is still changed to the content in my post by someone unknown. – netcaf Mar 18 '18 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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