31

For development purposes I want all DNS requests to the .dev TLD to forward to my own PC.

The hosts file does not suffice. I use multiple domains and also multiple subdomains. I would have to add a line for each combination.

I have read that a DNS forwarder like DNSmasq can be used to do the job (for example as described here). Also this software is already installed on Ubuntu 12.10.

The problem is that I fail to see how and where I should configure DNSmasq, i.e., where I should put this line:

address=/dev/127.0.0.1
1
  • 1
    You may not want to use .dev as Google took it. I'm still researching what they'll use it for and whether they plan for regular people to connect to it or just employees, but you may not want to redirect all access in case it's the former, unless this is purely a test box that will never connect to external hosts.
    – trysis
    Jun 3, 2015 at 18:40

4 Answers 4

22

In Ubuntu 12.10 or later you can do this with dnsmasq as run by NetworkManager.

  1. Create the directory mkdir /etc/NetworkManager/dnsmasq.d if it doesn't already exist.

    sudo mkdir /etc/NetworkManager/dnsmasq.d
    
  2. Toss the following line into /etc/NetworkManager/dnsmasq.d/dev-tld.

    address=/dev/127.0.0.1
    
  3. (Ubuntu 12.10) Restart NetworkManager.

    sudo service network-manager restart
    
  4. (Ubuntu > 13.04) Restart Dnsmasq.

    sudo service dnsmasq restart
    
  5. Enjoy the awesomeness.

enter image description here

7
  • 1
    Thanks to AbrahamVanHelpsing for the link to the arch wiki on how to do this.
    – jrg
    Dec 27, 2012 at 13:30
  • After following your steps still I get to see "unknown host" after running a ping. Do I need to configure/enable DNSMasq on some other place? Or configure my network properties in a different way? Thanks! Dec 27, 2012 at 18:11
  • Interesting. it's showing up fine for me.
    – jrg
    Dec 27, 2012 at 18:25
  • Ah, @RoelvanDuijnhoven - are you running 12.10 or 12.04? Works fine for me on 12.10.
    – jrg
    Dec 27, 2012 at 18:56
  • I am running 12.10. But I do need to mention that I have tried to alter some configuration file prior to asking a question here. Possibly these changes mitigate your solution. Javier Rivera's solution did the trick however! Dec 28, 2012 at 14:06
19

The complete standalone dnsmasq (DHCP and DNS server) is not installed by default in Ubuntu 12.04 and 12.10, but a package called dnsmasq-base is installed by default in Ubuntu Desktop 12.04 and 12.10. The dnsmasq-base package contains the dnsmasq binary and is used by NetworkManager.

To do what you want you will need to use dnsmasq as a caching DNS server. You need to:

  • Install dnsmasq sudo apt-get install dnsmasq
  • Change your network setting, so that your computer uses itself as it dns server.
  • Make the changes to the config files:

Create /etc/dnsmasq.d/dev-tld with these contents:

local=/dev/

address=/dev/127.0.0.5

The first command says *.dev requests can't be forwarded to your real DNS server. The second says *.dev resolves to 127.0.0.5 which is localhost.

  • Restart the dnsmasq service (not network-manager)
3
  • Thank you! It was extremely helpfull that I got to known that the DNSMasq present was not the real deal. After installing the complete package and configuring it in the way I described before I got it to work. :) Dec 27, 2012 at 18:15
  • 1
    I'd prefer the other answer for recent desktop installations. For recent desktop installations you would be running two dnsmasq instances this way. Your answer could be very useful in non-NetworkManager setups, older ones (not using dnsmasq) or even server installations!
    – gertvdijk
    Jan 8, 2013 at 19:31
  • Yep, other answer is best since Ubuntu 12.10.
    – BenjaminRH
    Mar 7, 2013 at 10:06
2

Just installed a fresh 19.04 and the only way I got it working was disabling systemd-resolved and have NetworkManager use dnsmasq instead for DNS. NetworkManager has built in support for dnsmasq and dnsmasq-base package is installed by default.

I am pointing multiple domains to different VMs like *.customerX.test to 192.168.33.10 and *.productY.test to 192.168.33.20 and so on. I would not use *.dev anymore but instead one of the reserved top level domains.

Solution

  1. Disable systemd-resolved

     sudo systemctl disable systemd-resolved.service
     sudo systemctl stop systemd-resolved.service
     sudo rm /etc/resolv.conf
    
  2. Edit /etc/NetworkManager/NetworkManager.conf to use dnsmasq for DNS.

     [main]
     dns=dnsmasq
    
  3. Put dnsmasq configuration in /etc/NetworkManager/dnsmasq.d/test-tld.conf

     address=/test/127.0.0.1
     address=/customerX.test/192.168.33.10
    
  4. and finally restart NetworkManager which will generate a new /etc/resolv.conf

     sudo systemctl restart network-manager.service
    
1

The following worked worked for me in Ubuntu 16.04:

  1. Install dnsmasq

    sudo apt-get -y install dnsmasq
    
  2. Edit dnsmasq.conf file:

    sudo nano /etc/dnsmasq.conf
    
  3. Add your command:

    address=/dev/127.0.0.1
    

    For wild card (*) then you can use dot (.) then dnsmasq to resolve WHATEWER_YOU_PUT_HERE.yourmachine.yourdomain to the same ip. E.g., address=/.localhost.dev/127.0.0.1

  4. Restart dnsmasq service:

    sudo /etc/init.d/dnsmasq restart
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .