Skip to content

Instantly share code, notes, and snippets.

@feryardiant
Last active June 25, 2018 04:42
Show Gist options
  • Save feryardiant/e264aebfec99e5ec00663bfe14662e43 to your computer and use it in GitHub Desktop.
Save feryardiant/e264aebfec99e5ec00663bfe14662e43 to your computer and use it in GitHub Desktop.
Wildcard subdomains of localhost by Evans.io

It always slightly niggled that I couldn’t just stick a *.localhost entry in /etc/hosts and have all subdomains of localhost resolve to 127.0.0.1. But setting up a local nameserver, and making sure external domains still got resolved properly, always seemed like too much faff. It turns out though, thanks to dnsmasq, that it’s actually very straightforward. (Instructions are for Ubuntu but dnsmasq runs on most *nix systems.)

  1. Install dnsmasq, a DNS forwarder which you can configure to reply to certain queries locally and forward all others on to your external nameservers:

    sudo aptitude install dnsmasq
    
  2. Configure it to resolve all localhost domains to 127.0.0.1. By default, dnsmasq will include all files in /etc/dnsmasq.d/ so create a new file here with a sensible name like /etc/dnsmasq.d/localhost.conf and add the following line:

    address=/localhost/127.0.0.1
    

    (Hopefully the syntax is obvious here. See the man page for more detail.)

  3. You now have a DNS forwarder up and running on localhost. The final step is to get your resolver to send queries to it. We do this by getting our DHCP client to add the local address to the list of nameservers it gets from the DHCP server. So edit /etc/dhcp3/dhclient.conf and add the following line:

    prepend domain-name-servers 127.0.0.1;
    

    (You’ll probably find that this line is already there and you just need to uncomment it.)

  4. To check everything’s working, reload the dnsmasq config and request a new lease from your DHCP server:

    sudo service dnsmasq restart
    sudo dhclient
    

And you should be good to go…

dig somesubdomain.localhost

Courtesy: http://evans.io/legacy/posts/wildcard-subdomains-of-localhost/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment