27

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?

pim
  • 3,280
Nick Rice
  • 331
  • 1
  • 4
  • 9
  • 1
    Why not just install dnsmasq like you did on 14.04? – vidarlo May 04 '18 at 04: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 05 '18 at 00:34

5 Answers5

57

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
pim
  • 3,280
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.

Sebastian Stark
  • 6,122
  • 19
  • 48
  • 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 05 '18 at 00: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 05 '18 at 00: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 05 '18 at 00:25
  • @NickRice No problem with this, but maybe others want to try – Sebastian Stark May 05 '18 at 01: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 05 '18 at 07:41
  • Looking at your edit, /etc/NetworkManager/NetworkManager.conf did not have any mention of dnsmasq in it to start with, out of the box, and so there was nothing to change and restarting NetworkManager could make no difference. Did you mean to that dns=dnsmasq should be put in as per the accepted answer? If so, best change the bit that says to ensure it is not present to saying to ensure it is present. – Nick Rice May 05 '18 at 12:15
  • You could have NetworkManager control dnsmasq or not, that is what this line is about. In my answer I propose to have it not controlled by nm so you as a user have full control over it. This line might be present or not, depending on what the user has tried before or if they upgraded from some version that had it activated. – Sebastian Stark May 05 '18 at 12:19
  • As I put in the question, it was a completely clean install, so no previous history or anything else left over from a previous OS etc, and each thing I tried I went back to a fresh install. But moving on now. – Nick Rice May 05 '18 at 17:16
1

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!

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

  • 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 04 '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
0

It is possible to use dnsmasq-base that is already pre-installed in Ubuntu instead of dnsmasq for the same purpose.

So don't install dnsmasq but do all steps from here https://askubuntu.com/a/1031896/211368

If you install dnsmasq it will collide with already running dnsmasq-base and you'll get an error:
failed to create listening socket for port 53: Address already in use