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,350
Nick Rice
  • 331

5 Answers5

59

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,350
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.

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!

Paul Engel
  • 11
  • 2
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

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