75

My DNS server is 192.168.1.152.

This DNS is provided to clients by DHCP. The windows clients on my LAN resolve names properly using that DNS, but my Ubuntu VM doesn't.

The VM is set up with bridge networking and is being properly provided the DNS server, but my local hostnames aren't being resolved by nslookup or browsers.

Here is an nslookup of one of my local domains:

# nslookup unraid.local
Server:     127.0.0.53
Address:    127.0.0.53#53

** server can't find unraid.local: SERVFAIL

Here is what it should resolve by using my DNS server:

# nslookup unraid.local 192.168.1.152
Server:     192.168.1.152
Address:    192.168.1.152#53

Name:   unraid.local
Address: 192.168.1.152

/etc/resolv.conf has a wrong nameserver:

# 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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.

nameserver 127.0.0.53

I ran that command. Under DNS Servers, confusingly, it specifies the correct server (and my default gateway).

root@ubuntu:~# systemd-resolve --status
Global
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test

Link 2 (ens33)
      Current Scopes: DNS LLMNR/IPv4 LLMNR/IPv6
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 192.168.1.152
                      192.168.1.1

I don't want to "hard code" the DNS server's IP in a config file because I won't be able to resolve when I change networks.

How can I get resolvconf and NetworkManager to automatically set the DHCP server's IP in /etc/resolv.conf?

Victor
  • 9,244
FireSpore
  • 751

13 Answers13

87

Known systemd bug.

Temporary workaround with no need to reconfigure if the DNS IP's changes:

sudo rm -f /etc/resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
reboot
Victor
  • 9,244
teknopaul
  • 2,137
72

Try editing /etc/systemd/resolved.conf, adding your desired DNS server:

change this:

[Resolve]
#DNS=

to this (but use the one you want - this is an example):

[Resolve]
DNS=192.168.1.152

after that, restart the service:

service systemd-resolved restart

And when you check the status you should see

$ systemd-resolve --status
Global
         DNS Servers: 192.168.1.152

      DNSSEC NTA: 10.in-addr.arpa
                  16.172.in-addr.arpa
                  168.192.in-addr.arpa
                  17.172.in-addr.arpa
                  18.172.in-addr.arpa
                  19.172.in-addr.arpa
Zanna
  • 72,312
18

I finally got a solution for this problem for ubuntu 17.10. By default this version of Ubuntu uses systemd-resolved, which I hope is going to be stable for the next versions.

In order to use custom dns instead of the local systemd-resolved cache, do the following:

  1. add new nameservers. Edit the file in /etc/systemd/resolved.conf as sudoer. Here I've commented out the DNS entry and placed my dns [Resolve] DNS=10.96.0.10 8.8.8.8 8.8.4.4

  2. cancel the actual symlink to /etc/resolv.conf

  3. create a new symlink sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
  4. restart the service sudo service systemd-resolved restart
  5. restart the network manager sudo systemctl restart networking

And now if you dig to a name provided by your add dns, you should see the record resolved dig nexus.default.svc.cluster.mydomain

Last step is to update the order of resolution in /etc/nsswitch.conf, by placing the dns before the mdns4_minimal

hosts           files dns mdns4_minimal [NOTFOUND=return] resolve [!UNAVAIL=return] myhostname
3

Your /etc/resolv.conf is not the problem. systemd-resolved just defaults to unconfigured, so it fails all lookups. Feel free to rant about Unconfigured vs A Reasonable Default.

Manually add nameservers to systemd-resolved. (editing per Olorin's comment below to add mkdir, correct path /etc not /lib, in order to survive system updates)

sudo mkdir -p /etc/systemd/resolved.conf.d
sudo nano /etc/systemd/resolved.conf.d/00-my-dns-server-is.conf

Add:

[Resolve]
Cache=yes
DNS=192.168.1.152

Then...

sudo systemctl daemon-reload

systemd-resolved is smart, but, unconfigured as it is, by package maintainers, it just LOOKS stupid because package maintainers do not believe in A Reasonable Default. We can put 13 internet root servers in there aka "djb way", or 10 opennic servers: https://pastebin.com/JBfYVVtG or the three fastest opennic servers, as measured by namebench. Plus ISP nameservers, sure. Plus Google, sure. systemd-resolved is not the problem. I am the problem.

Martin Thornton
  • 5,996
  • 12
  • 32
  • 43
2

On my system I found a bad symlink: /etc/resolv.conf was a symlink which points to /run/systemd/resolve/stub-resolv.conf

This file contains only one line:

nameserver 127.0.0.53#53

As a result, the local network's DNS lookup was often missing.

So, instead I changed /etc/reolv.conf to point to /run/systemd/resolve/resolv.conf

and now works correctly.

Benjamin R
  • 2,952
1

I put together a script that works around the issue of Netplan not currently supporting the ability to supersede DHCP supplied DNS servers and domain search order. It creates the Netplan yaml file and configures systemd-resolved to work as expected.

################## Start Netplan config (renderer: NetworkManager)
#  Some useful commands for customisation
# NetConn=$(nmcli device show|grep GENERAL.CONNECTION|head -n1|awk '{print $2}')
# IP=$(nmcli device show|grep IP4.ADDRESS|head -n1|awk '{print $2}')
# GATEWAY=$(nmcli device show|grep IP4.GATEWAY|head -n1|awk '{print $2}')
# sed -i 's/renderer: networkd/renderer: NetworkManager/' //etc/netplan/01-netcfg.yaml
######################################
systemctl start NetworkManager
NIC=$(nmcli device show|grep GENERAL.DEVICE|head -n1|awk '{print $2}')

#####   create Netplan yaml config file
cat >/etc/netplan/01-netcfg.yaml <<EOF 
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
  network:
   version: 2
   renderer: NetworkManager
   ethernets:
EOF
echo "    ${NIC}:" >> /etc/netplan/01-netcfg.yaml
cat >>/etc/netplan/01-netcfg.yaml <<EOF
      dhcp4: yes
      nameservers:
        search: [abc.domain.edu, def.domain.edu]
        addresses: [10.10.11.22, 10.10.11.23]

EOF

#work around DNS resolv bug
systemctl stop systemd-resolved
sed -i 's/#DNS=/DNS=10.10.11.22 10.10.11.23/' /etc/systemd/resolved.conf
sed -i 's/#Domains=/Domains=abc.domain.edu def.domain.edu' /etc/systemd/resolved.conf
systemctl start systemd-resolved
systemctl restart NetworkManager
netplan apply
reboot
############### End Netplan Config
Fabby
  • 35,017
1

What I did, after this happened to me when I upgraded from a much older Ubuntu release and my name resolution broke. First I tried the 00-yadayadayada approach above - but that did NOT work.

cd /etc mv resolv.conf resolv.conf-I-HATE-SYSTEMD cp resolv.conf-I-HATE-SYSTEMD resolv.conf

And then edited the file /etc/resolv.conf. The first gets rid of the pesky symbolic link (and documents that was done) so that subsequent reboots won't overwrite it (I hope).

What all of this demonstrates is THE ABSOLUTE FOLLY THAT IS SYSTEMD. What the fsck does a resolver configuration have to do with system startup order, etc., such that systemd should be involved AT ALL. What a piece of junk.

ARRRRRRRRGH. What a piece of junk systemd is. Too many programmers with too much time on their hands to lazy to do the "un fun" jobs of actually writing and fixing APPLICATIONS, seeing it as somehow "beneath" them. Bah.

JRJ

cube1us
  • 31
1

The solution that worked for me is posted here on my blog

Use nano editor to edit these 2 files

nano /etc/resolv.conf
nano /etc/resolvconf/resolv.conf.d/head

Add DNS servers you want to use

nameserver 9.9.9.9
nameserver 127.0.0.1

Then restart the service

service systemd-resolved restart

Final check:

cat /etc/resolv.conf

Do nslookup

nslookup google.com 

i just used this and it fixed the problem on my laptop at home, but some time in the future when I am out of my home network I can see that it will cause problems, because the primary and secondary dns server will still be my home servers with their lan addresses.

Edoo
  • 21
  • 3
0

It's strange but the only solution which worked for me on Ubuntu 18.04 was the one I found here:

First, change /etc/resolv.conf by setting the desired nameserver:

# nano /etc/resolv.conf

Set, for instance,

nameserver 8.8.8.8

And then do

# chattr +i /etc/resolv.conf

This protects /etc/resolv.conf file so that no one can modify it including root user.

damadam
  • 2,873
Artem S
  • 101
0

I just went through this again on another machine. I decided to live with systemd's resolved and with networkmanager by editing the resolved.conf file, /etc/systemd/resolved.conf, and setting networkmanager to use the same dns server. Stop the stub listener from taking over dns:

/etc/systemd/resolved.conf:
[Resolve]
DNS=192.168.0.1
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
DNSStubListener=no

...then go into NetworkManager and stop nm from using dhcp to acquire a nameserver IP. Turn off Automatic for DNS, set the nameserver IP.

0

I've only been able to get name resolution to work with Docker by disabling systemd-resolved completely:

systemctl stop systemd-resolved
systemctl disable systemd-resolved

Then edit /etc/resolv.conf like you would have back in the pre-systemd days.

EDIT: This approach isn't aggressive enough. If your Docker container runs for long enough, Systemd will restart systemd-resolved and DNS resolution will break again. Some people have reported success by running this command:

systemctl mask systemd-resolved

...which is like systemctl disable but stronger. In my Docker deployments I resort to removing the systemd-resolved binary to make sure that Systemd cannot restart it under any circumstances. The command to do that is:

rm -f /lib/systemd/systemd-resolved
0

Use cat and see if /etc/resolvconf/run/interface/* is anything you do NOT want, comment it later using vi.

-3

I have the same problem. Each reboot, nothing will resolve. I've uninstalled everything and reinstalled it about 50 times already. It's simply just broken.

so the fix is.... "just apply a few settings each time you boot your pc, which allows 90% of host names to resolve but wget and apt-get updates and upgrades will fail randomly for no reason"

cat /etc/systemd/resolved.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=yes
#MulticastDNS=yes
#DNSSEC=no
#Cache=yes
#DNSStubListener=udp

When using Ubuntu 17.10 Desktop, you have to edit the one file that says DO NOT EDIT THIS FILE BLAH BLAH BLAH

well that's the ONLY way it works~!

root@nas:~# cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 8.8.8.8

So remove the 127.0.0.53 IP as the name server and enter another one, google for example. It appears my actual DNS IP address doesn't resolve host names either (even though it works on 10 other machines and devices in the house), but google's works fine.

ravery
  • 6,924