On Ubuntu 14.04 I'm using dnsmasq to resolve wildcard example.com domains to the local machine (desktop computer at home).

After much reading around I cannot for the life of me sort out the same on a clean install of 18.04.

All I want to achieve at the moment is:

  • for ping example.com to ping 127.0.0.1 and not 93.184.216.34;
  • for ping anysubdomain.example.com to also ping 127.0.0.1;
  • and for ping google.com to ping the real google.com via the router/IP DNS.

Surely this must be simple, even trivial?

But I'm stumped. I can get the example.com's resolving but only at the expense of breaking everything else.

How can I do this?

  • 1
    Why not just install dnsmasq like you did on 14.04? – vidarlo May 4 at 4:58
  • 1
    @vidarlo Well of course that was the first thing I did, as I indicated in my question. But unlike in 14.04 that results in a "failed to create listening socket for port 53: Address already in use" error. The answer I accepted works fine without installing dnsmasq explicitly. – Nick Rice May 5 at 0:34
up vote 12 down vote accepted
+50

Here are the steps for ubuntu 18.04. It's a little bit long since systemd-resolved does not play very well with NetworkManager when configured with dnsmasq.

Yet I still recommend starting dnsmasq from NetworkManager, because network connectivity changes (WIFI, wired, ...) will be handled transparently.

Enable dnsmasq in NetworkManager

Edit the file /etc/NetworkManager/NetworkManager.conf, and add the line dns=dnsmasq to the [main] section, it will look like this :

[main]
plugins=ifupdown,keyfile
dns=dnsmasq

[ifupdown]
managed=false

[device]
wifi.scan-rand-mac-address=no

Let NetworkManager manage /etc/resolv.conf

sudo rm /etc/resolv.conf ; sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf

Configure example.com

echo 'address=/.example.com/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/example.com-wildcard.conf

Reload NetworkManager and testing

NetworkManager should be reloaded for the changes to take effect.

sudo systemctl reload NetworkManager

Then we can verify that we can reach some usual site :

dig askubuntu.com +short
151.101.129.69
151.101.65.69
151.101.1.69
151.101.193.69

And lastly verify that the example.com and subdomains are resolved as 127.0.0.1:

dig example.com askubuntu.example.com a.b.c.d.example.com +short
127.0.0.1
127.0.0.1
127.0.0.1
  • what do you mean with "systemd-resolved does not play very well with NetworkManager"? – Sebastian Stark May 4 at 10:32
  • 1
    When NetworkManager uses dns=dnsmasq, it should tell systemd-resolved to use dnsmasq, could be automatic. – pim May 4 at 11:50
  • Thank you, this worked perfectly! It seems I can't give you the bounty for another hour, and as it's gone one in the morning here I'm off to bed now and will do that when I switch back on tomorrow. – Nick Rice May 5 at 0:14
  • 1
    Awesome. I've read many different techniques. This is the one that finally worked. – Redsandro Jul 26 at 9:56
  • 2
    For those who prefer to revert back to systemd-resolved, /etc/resolv.conf points to /run/systemd/resolve/stub-resolv.conf by default. – Pothi Kalimuthu Aug 8 at 4:30

This won't be as simple as editing the hosts file. You have a couple of options:

This python DNS proxy that will handle wildcards in /etc/hosts

Using DNSmasq

  • Thanks. Your link to the DNSmasq question is what I am doing in 14.04. Just doing the same in a fresh 18.04 installation isn't working due to a port conflict. So out of the box something else needs doing compared to that. Looking at all that code needed for the python DNS proxy, I can't believe all that is necessary. It wasn't before except, I suppose, for those who insisted on using /etc/hosts. – Nick Rice May 4 at 21:20
  • Can you do netstat -tulpn to check what's using the port? EDIT: Nevermind, saw the accepted answer. the problem is systemd-resolve. – Harikrishnan R May 10 at 13:16

First make sure in /etc/NetworkManager/NetworkManager.conf the following line is not present or commented out:

dns=dnsmasq

Restart NetworkManager:

sudo systemctl restart NetworkManager

Make sure the NetworkManager-controlled dnsmasq is not running anymore, either by killing the process or rebooting your system.

Then install dnsmasq:

sudo apt install dnsmasq

Add the following to /etc/dnsmasq.d/example.com:

address=/example.com/127.0.0.1

Restart dnsmasq:

sudo systemctl restart dnsmasq

Now you should have a wildcard dns override for example.com.

  • I couldn't get this to work. Immediately following a clean minimal install (with the "erase disk" option) the sudo apt install dnsmasq gives a "failed to create listening socket for port 53: Address already in use" error. I continued to the end anyway in case a later step resolved that, but it didn't and this didn't work. – Nick Rice May 5 at 0:11
  • Then the dnsmasq that is started by NetworkManager was still running. So it should first be deactivated, eventually killed, and only then the dnsmasq package should be installed. – Sebastian Stark May 5 at 0:14
  • Thanks, Sebastian. However I already accepted pim's answer which worked flawlessly, so I'm able to move on now without playing around with it any more. – Nick Rice May 5 at 0:25
  • @NickRice No problem with this, but maybe others want to try – Sebastian Stark May 5 at 1:19
  • Yes of course, Sebastian. I did try your answer first because it was the simplest, and if all it needs is an added initial step then it's a very good one. – Nick Rice May 5 at 7:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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