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 and not;
  • for ping anysubdomain.example.com to also ping;
  • 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 '18 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 '18 at 0:34

Here are the steps for ubuntu since 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 :




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/' | 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

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

dig example.com askubuntu.example.com a.b.c.d.example.com +short
  • 1
    When NetworkManager uses dns=dnsmasq, it should tell systemd-resolved to use dnsmasq, could be automatic. – pim May 4 '18 at 11:50
  • 1
    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 '18 at 0:14
  • 2
    Awesome. I've read many different techniques. This is the one that finally worked. – Redsandro Jul 26 '18 at 9:56
  • 3
    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 '18 at 4:30
  • 1
    Works on Ubuntu 20.04 as well – sola Aug 5 '20 at 20:01

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


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:


Restart dnsmasq:

sudo systemctl restart dnsmasq

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

  • 1
    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 '18 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 '18 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 '18 at 0:25
  • @NickRice No problem with this, but maybe others want to try – Sebastian Stark May 5 '18 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 '18 at 7:41

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 '18 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 '18 at 13:16

Based on pim's answer, I have created a gist containing a simple Bash script which uses DNSMasq in combination with NetworkManager to setup the .test domain pointing to

See the gist at https://gist.github.com/archan937/d35deef3b1f2b5522dd4b8f397038d27.

You can execute the script with the following command:

curl -sL https://gist.githubusercontent.com/archan937/d35deef3b1f2b5522dd4b8f397038d27/raw/setup-dnsmasq.sh | sudo bash


  • This is awesome, but it doesn't work for me. It just ends up with could not ping foo.test – SimaPro Jan 20 at 20:41
  • Hey, @SimaPro. I increased the ping timeout to 1 second. Does that solve your issue? – Paul Engel Jan 21 at 21:56

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.