I need to test sub-domains on my localhost. How can I effectively have this result of adding *.localhost.com to my /etc/hosts/ file?

If it's not possible, how do I work around this problem? I need to test wildcard sub-domains on my localserver. It is a Django devserver, can the Django dev server handle the sub-domains? Can some other piece of software/routing give me the end result I want?

  • 1
    Belongs on superuser ?
    – Paul R
    Mar 2, 2010 at 16:48
  • I know people say it's not possible! But how do I do it anyway! What is something outside of /etc/hosts/ I can use to get the effect. I'm testing a development server.
    – MikeN
    Mar 2, 2010 at 20:17
  • As most answers are focusing on your first question (localhost subdomain wildcards), I'll answer your secondary question as a comment: yes, the Django dev server is perfectly capable of handling localhost subdomains, you just have to convince your browser and your OS to send the traffic its way (using one of the various solutions below)! Mar 9, 2018 at 21:22

19 Answers 19


I have written a dns proxy in Python. It will read wildcard entries in /etc/hosts. See here: https://github.com/hubdotcom/marlon-tools/blob/master/tools/dnsproxy/dnsproxy.py

  • This is perfect! I have been looking for a simple solution like this for a long time (Working on OSX Mavericks BTW)
    – Billy Moon
    Nov 5, 2013 at 22:07
  • 2
    now, if we could only just pip install it :)
    – metakermit
    Mar 25, 2014 at 11:39
  • 1
    I installed this, did everything and ran it, but it does not block sites. Dec 13, 2015 at 18:47
  • 1
    Can not upvote this enough. I've needed this forever, stumbled upon this gem of a question and answer, and I'm a bit of a Python hacker, and this is a neat tidbit. Thanks for sharing!
    – Farley
    Sep 8, 2016 at 15:41

Install dnsmasq (I do this on all my Linux desktops as a DNS cache anyways). In dnsmasq.conf add the line:

  • 8
    Brilliant! Note for Mac users, it really is this simple: 1. sudo port install dnsmasq 2. edit /opt/local/etc/dnsmasq.conf 3. sudo port load dnsmasq
    – tomc
    Nov 16, 2011 at 11:57
  • 25
    OSX with brew: same as above but brew install dnsmasq Mar 7, 2013 at 21:41
  • 2
    fyi brew > port (re @MattHumphrey suggestion)
    – electblake
    Mar 22, 2013 at 2:57
  • 6
    Great tip. For an Ubuntu 14.04 desktop (which runs dnsmasq by default), create a file called /etc/NetworkManager/dnsmasq.d/dnsmasq-localhost.conf and put the line address=/localhost.com/ in it, then restart.
    – user38397
    May 21, 2015 at 18:48
  • 2
    How can I do this for multiple domains? And how do I refresh after editing the file? I want *.localhost and *.loc. I added two lines: address=/localhost/ and address=/loc/ For some reason only the first one works. And after commenting out both, and calling sudo service dnsmasq restart and sudo service network-manager restart I still have the *.localhost version working.
    – donquixote
    Sep 11, 2017 at 22:33

It is not possible to specify wildcards in the /etc/hosts file. Either specify the required hostnames explicitly or alternatively set up a local name server with the appropriate rules.


You need to set up a DNS server and have each client use it for resolution. The server itself can be something as "light" as dnsmasq or as heavy as BIND.

  • +1 for dnsmasq, which is really well-documented and easy to use May 2, 2011 at 12:24
  • dnsmasq is not able to do this. Dec 13, 2015 at 18:51
  • What part of the behavior described in the --address= section of the dnsmasq.conf man page doesn't cover the use case describe above? Dec 14, 2015 at 16:33

Simple Workflow (no need to install anything)

I personally like to create a PAC file for that and make my browser just use it.

Step 1: create a file e.g.: *.proxy.pac* somewhere (I use my $home folder)

Step 2: paste this code (example is with port 8000):

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*localhost")) {
    return "PROXY localhost:8000";
  return "DIRECT";

Step 3: Make your Browser use this PAC file.

Youtube Video for PAC & Firefox

Step 4: Now you can test your app by accessing: http://mysubdomain.localhost/

Step 5: Enjoy :)

  • 7
    Perhaps it is worth mentioning that this will only affect your browser. Other programs (like wget, will not get affected by this). That is not per se a problem, but perhaps it is better to mention this. Apr 24, 2018 at 21:02
  • If you go the .pac route and you're implementing a node.js server, you will be surprised to see that req.url is now an absolute URL. This is done because the assumption is that you want to write an actual proxy server, but it is surprising if you came to this technique just as a way to stop adding /etc/hosts entries for debugging. FYI to those who might go the same road I did. Jan 2, 2019 at 20:26
  • How I can apply this to Google Chrome/mium & Brave browser as well? Sep 16, 2021 at 11:27

I've tidied up an old project of mine:



  • linux where avahi and python-avahi are installable
  • you're ok with .local domains (avahi doesn't support any other kind)

Advantages over using dnsmasq or the python dns proxy:

  • other avahi/bonjour users on your local network can resolve the aliases you create and announce to the network ( providing you're allowing access to port 5353 )

You cannot use a wildcard in /etc/hosts.

Have a look here for a good walkthrough on how to accomplish on OS X using BIND, the built-in but inactive DNS server, and Apache.

  • 5
    Apache has nothing to do with subdomains.
    – Anonymous
    Mar 2, 2010 at 17:55

This DNS based solution worked perfectly in my case, without need to install anything : https://gist.github.com/fedir/04e60d679d5657d1f9f9aa10b3168282 (Mac OSX 10.9)


If you want to use dnsmasq with NetworkManager you can (or even must?) start dnsmasq from NetworkManager by adding


to /etc/NetworkManager/NetworkManager.conf. Then the dnsmasq config goes to /etc/NetworkManager/dnsmasq.conf or /etc/NetworkManager/dnsmasq.d/ resp.


Copying from this blog here is how to do it on mac:


~ brew install dnsmasq

~ vim /usr/local/etc/dnsmasq.conf

# This file will be added to the configuration

~ vim /Users/your_user_name/.dnsmasq/dnsmasq.conf

# example.localhost will be resolved as, including subdomains

~ sudo brew services stop dnsmasq

~ sudo brew services start dnsmasq

We need to tell macOS to use as the first DNS resolver.

enter image description here


Short answer:

Your /etc/hosts/ file won't let you use wildcards or port numbers. You will need to create one entry for each of your subdomain

  • 2
    What would let you specify port numbers? Not DNS AFAIK.
    – ptman
    Mar 3, 2010 at 11:45

The short answer is you don't. The longer answer is you need to be clearer on what you desire to actually achieve, because there is perhaps either a better way, and a different way to achieve it.

For web-hosting (I've never seen it used otherwise) is done in DNS in combination with a virtual hosting aware web server. For more information on wildcard DNS records (Wikipedia), and an article Wildcard hosting with Apache and Bind for Linux using bind and Apache.

At worst, you could use a local DNS server I suppose.


A common task for this subject is to map directories to subdomains. A very straightforward way for that is to append the directory-based entries automatically to the hosts file:


import os

hostsFile = open("/etc/hosts", "a+");

lines = hostsFile.readlines()

for fileName in os.listdir('/opt/subdomainDirs'):

    entryExists = False
    for line in lines:
        if fileName in line:
            entryExists = True  

    if not entryExists:
        hostsFile.write(" " + fileName + ".localhost\n");

Thank you tschundeee for what I consider to be the ultimate answer to this issue, wish I could just comment but here is the total configuration for those trying to accomplish the original goal (wildcards all pointing to same codebase -- install nothing, dev environment ie, XAMPP)

hosts file (add an entry)

file: /etc/hosts (non-windows)   example.local

httpd.conf configuration (enable vhosts)

file: /XAMPP/etc/httpd.conf

# Virtual hosts
Include etc/extra/httpd-vhosts.conf

httpd-vhosts.conf configuration

file: XAMPP/etc/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/path_to_XAMPP/htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs/example.local-error_log"
#    CustomLog "logs/example.local-access_log" common

restart apache

create pac file:

save as whatever.pac wherever you want to and then load the file in the browser's network>proxy>auto_configuration settings (reload if you alter this)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  return "DIRECT";

dnsmasq worked for me, except I had to make some additional steps.

Here is the full procedure:

  1. Prepend /etc/resolv.conf with the following line

  2. Add the following lines to /etc/dnsmasq.conf

  3. Restart dnsmasq (do not simply tell dnsmasq to reload the config files)

  • 1
    This will not work if your distro uses netplan (ubuntu 18.04 I'm looking at you)
    – MrMesees
    Feb 26, 2019 at 5:15
  • That's sad. Just found out netplan was first released in July 2016. I'm sure their way of doing it is "better."
    – jbangerter
    Aug 25, 2020 at 0:44

This might not be needed.

The .localhost suffix should be pointing to in your OS by default.

Currently using Ubuntu 20.04.5 LTS and it works out of the box without installing any additional packges.

So you can name your test domains as mytestdomain.localhost and that'll point to Anything with the .localhost suffix will point to

You can try it: ping helloworld.localhost, should point to localhost.

$ ping test.localhost
PING test.localhost(ip6-localhost (::1)) 56 data bytes
64 bytes from ip6-localhost (::1): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from ip6-localhost (::1): icmp_seq=2 ttl=64 time=0.079 ms
64 bytes from ip6-localhost (::1): icmp_seq=3 ttl=64 time=0.043 ms
64 bytes from ip6-localhost (::1): icmp_seq=4 ttl=64 time=0.079 ms
64 bytes from ip6-localhost (::1): icmp_seq=5 ttl=64 time=0.053 ms

This behavior is described in this RFC2606 https://datatracker.ietf.org/doc/html/rfc2606#section-2

Also described here: https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html#Synthetic%20Records


I was looking for a similar solution recently and discovered the localhost.direct project.

These guys implemented a brilliant idea which works on any OS. You don't need to install anything.

SSL certificates are included.


On Ubuntu 22.04 with Snaps enabled, nss-myhostname appears to be enabled, or there appears to also be an APT package libnss-myhostname. Seems you can use any *.localhost (or *.localhost.localdomain) without any confiuration:

$ getent ahosts xxx.yyy.localhost
::1             STREAM xxx.yyy.localhost
::1             DGRAM  
::1             RAW       STREAM       DGRAM       RAW    

If you do not mind relying on a third-party domain server, there are various easy options (no local OS changes needed). For example xxx.yyy. will resolve to Docs

You must log in to answer this question.

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