26

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?

2
  • 1
    Why not just install dnsmasq like you did on 14.04?
    – vidarlo
    May 4, 2018 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, 2018 at 0:34

4 Answers 4

52
+50

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 :

[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
12
  • 1
    When NetworkManager uses dns=dnsmasq, it should tell systemd-resolved to use dnsmasq, could be automatic.
    – pim
    May 4, 2018 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, 2018 at 0:14
  • 2
    Awesome. I've read many different techniques. This is the one that finally worked.
    – Redsandro
    Jul 26, 2018 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. Aug 8, 2018 at 4:30
  • 1
    Works on Ubuntu 20.04 as well
    – sola
    Aug 5, 2020 at 20:01
2

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.

8
  • 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, 2018 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. May 5, 2018 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, 2018 at 0:25
  • @NickRice No problem with this, but maybe others want to try May 5, 2018 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, 2018 at 7:41
0

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

2
  • 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, 2018 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. May 10, 2018 at 13:16
0

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 127.0.0.1

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

Cheers!

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