Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to test sub-domains on my localhost. How can I effectively have this result of adding * 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?

share|improve this question

migrated from Mar 2 '10 at 17:06

This question came from our site for professional and enthusiast programmers.

Belongs on superuser ? – Paul R Mar 2 '10 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 '10 at 20:17

14 Answers 14

up vote 32 down vote accepted

I have written a dns proxy in Python. It will read wildcard entries in /etc/hosts. See here:

share|improve this answer
Thanks! This is exactly what I needed! Works great! – MikeN May 17 '11 at 20:06
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 '13 at 22:07
now, if we could only just pip install it :) – metakermit Mar 25 '14 at 11:39
I installed this, did everything and ran it, but it does not block sites. – DisplayName Dec 13 '15 at 18:47

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

share|improve this answer
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 '11 at 11:57
OSX with brew: same as above but brew install dnsmasq – Matt Humphrey Mar 7 '13 at 21:41
fyi brew > port (re @MattHumphrey suggestion) – electblake Mar 22 '13 at 2:57
This example doesn't work for me, possibly because I'm using for local serving. Don't worry, I already changed the IP to the correct one but DNS is not resolving. Any ideas, is this example correct for "non-" IP's? – Brian Oct 27 '13 at 19:55
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=/ in it, then restart. – user38397 May 21 '15 at 18:48

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.

share|improve this answer

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.

share|improve this answer
+1 for dnsmasq, which is really well-documented and easy to use – David Schmitt May 2 '11 at 12:24
dnsmasq is not able to do this. – DisplayName Dec 13 '15 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? – Gerald Combs Dec 14 '15 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 :)

share|improve this answer

This DNS based solution worked perfectly in my case, without need to install anything : (Mac OSX 10.7)

share|improve this answer
Such an amazing solution for Mac OSX. – iDev247 Sep 6 '12 at 7:25

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.

share|improve this answer
Apache has nothing to do with subdomains. – Anonymous Mar 2 '10 at 17:55

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 )
share|improve this answer

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.

share|improve this answer

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");
share|improve this answer

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 admin@example.local
    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";
share|improve this answer

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

share|improve this answer

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

share|improve this answer
What would let you specify port numbers? Not DNS AFAIK. – ptman Mar 3 '10 at 11:45

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.