2 Step Howto to set up a local wildcard domain resolution on Ubuntu/Linux Mint

Primary tabs

So today it occurred to me as I was doing web development on several different Vagrant instances simultaneously on my work laptop, that it'd be very convenient to have a single wildcard domain on my local machine that, for example, resolved all domains of the form "*.localhost" (note, the domain "*.local" is normally associated with "zeroconf" sites, and so it can lead to weird errors if you use it) to the localhost IP, 127.0.0.1. Normally, one would have to manually create a separate entry in /etc/hosts, like

127.0.0.1 somedevdomain.localhost someotherdomain.localhost

Having to do that is annoying. Although it'd be easy to type, something like

127.0.0.1 *.localhost

doesn't work. Thankfully, it turns out that it's quite easy to do something almost as easy on a modern Ubuntu-based Linux distro (I have Linux Mint 17.2, based on Ubuntu 14.04 - this might work on earlier distros and on Debian, but I haven't tested it - feel free to let me know)...

Modern Ubuntu-based Linux distros use the very convenient NetworkManager app to manage all network connectivity on desktop machines. I didn't realise this, but it integrates the very useful open source DNSMasq application (I use it on my DD-WRT equipped router to provide local DNS resolution on my home network, so that locally hosted services resolve to internal IPs on the local LAN when I'm home, but resolve to the external static IP when I'm outside the home network) which, it turns out, provides a trivial way to achieve wildcard local DNS resolution. Here it is in two steps (replace my domain with your own preferred domain name):

  1. Create the following file using your preferred editor (I used vim, some might use emacs, nano, or something else - the sudo will trigger you to enter you password so you can edit the file owned by the root user):
    sudo vim /etc/NetworkManager/dnsmasq.d/dnsmasq-localhost.conf

    with the following line:
    address=/localhost/.localhost/127.0.0.1
    

    save the file and exit.
  2. Then restart the NetworkManager:
    sudo service network-manager restart

At that point, your new configuration should be in place and active. You can test it by typing, at the commandline:

 

dig local

 

or you can try:

 

dig randomsubdomain.localhost

 

both should return this:

; <<>> DiG 9.9.5-3ubuntu0.5-Ubuntu <<>> local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41430
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;localhost.    IN A
;; ANSWER SECTION:
localhost.   0 IN A 127.0.0.1
;; Query time: 1 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Thu Nov 05 11:16:59 NZDT 2015
;; MSG SIZE  rcvd: 39

You might need to install dnsutils if your system complains you don't have dig installed:

sudo apt-get install dnsutils

Add new comment

Note to commenters: due to problems with spam comments, your comment will only appear on this site after it's been deemed (by me) to be legitimate.