You are doing everything right like putting the entries in /etc/hosts
, server address in /etc/resolv.conf
, the /etc/nsswitch.conf
looks good too.
The problem you are having is due to the understanding of a very specific term "nameserver". All the commands used to resolve IP address to hostname and vice versa used the nameserver addresses from /etc/resolv.conf
unless mentioned explicitly. You have put nameserver 127.0.0.1
which is not a valid name server because it is not configured as a nameserver. It is there because of dnsmasq
which acts as a DNS cacher (and DHCP server) but host
, dig
, nslookup
take data from a valid, configured nameserver only.
As the host
, nslookup
, dig
commands will use the "nameserver" mentioned in /etc/resolv.conf
(unless specified), hostname resolution will not work using these in your case. Although the programs that use /etc/nsswitch.conf
or read /etc/hosts
will resolve the hostname to IP addresses and vice versa.
If you want to resolve hostnames from /etc/hosts
the you need to use getent
. For example to resolve "node1-VirtualBox", you need the following command :
getent hosts node1-VirtualBox
I faced a very similar issue (if not exactly the same) on Linux Mint 19 (Tara). I've managed to solve it by combining 3 different pieces of information.
It seems to all be related to recent changes with systemd-resolved.
First, yes I've needed to configure /etc/nsswitch.conf as you did and would expect. As long as dns comes before mdns you should be good. I ended with simply:
hosts: files dns myhostname
ref: https://unix.stackexchange.com/a/457172/271210
Prior to upgrading to this version of Mint, this is the only thing I needed to do. Now I also ended up making the below two other changes to get it working...
After that I've configured my search domain so systemd-resolved would work as I wanted. So I've edited the file /etc/systemd/resolved.conf, the Domains setting under the [resolve] section. In my case it ended up looking like:
[Resolve]
#DNS=
#FallbackDNS=
Domains=trilliant.local
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
ref: https://askubuntu.com/a/1031271/872881
I've also changed the avahi configuration to something else ("mdns" if I remember correctly, but it doesn't matter). It shouldn't be required however from my understanding. Just adding for completeness.
But none of it worked until I've called the following:
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
ref: https://askubuntu.com/a/938703/872881
After calling this, everything started working perfectly and as expected!
So it's possible I didn't really need to change the /etc/systemd/resolved.conf file but I kept this change since it made sense and allows me to only type a machine's name, without the complete FQDN, for DNS resolution to work.
Best Answer
I believe this is by design.
Ubuntu 17.04 has switched to systemd-resolved for name resolution and it uses only LLMNR (multicast name lookup) for single-label names resolution. See this link for details: https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html
For the reasoning behind this decision check out poettering's reply in this bug report: https://github.com/systemd/systemd/issues/2514
UPD: This issue can be worked around by using a domain for local network. If network interface is configured (either manually or by DHCP) to use a search domain then systemd-resolved will add this domain to single-label names and then look them up via unicast DNS.
Obviously the local DNS server should be reconfigured to recognise these domain. In case of dnsmasq that reads local host-to-IP pairs from /etc/hosts it can be accomplished by adding following instructions to dnsmasq.conf:
UPD2: Or you could just revert to dnsmasq as described here https://askubuntu.com/a/911432/692094