Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to test sub-domains on my localhost. How can I effectively have this result of adding *.localhost.com to m /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 dev server, 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 stackoverflow.com 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
add comment

13 Answers

up vote 25 down vote accepted

I have written a dns proxy in Python. It will read wildcard entries in /etc/hosts. See here: http://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py

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 :) –  kermit666 Mar 25 at 11:39
add comment

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

address=/localhost.com/127.0.0.1
share|improve this answer
    
Thank you! I've been wanting this for years. It works wonderfully. Now I don't have to edit /etc/hosts every time I make a new website to test locally. –  JasonWoof Oct 9 '10 at 22:30
    
dnsmasq.conf didn't exist by default. I created the file /etc/dnsmasq.conf and added just that line and started $sudo dnsmasq. The browsers picked up the changes without restart. –  so_mv Sep 13 '11 at 7:58
6  
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
4  
OSX with brew: same as above but brew install dnsmasq –  Matt Humphrey Mar 7 '13 at 21:41
1  
fyi brew > port (re @MattHumphrey suggestion) –  electblake Mar 22 '13 at 2:57
show 1 more comment

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
add comment

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
add comment

This DNS based solution worked perfectly in my case, without need to install anything : http://renebakx.nl/7/running-a-local-wildcard-dns-server-on-your-mac/ (Mac OSX 10.7)

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

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
3  
Apache has nothing to do with subdomains. –  Anonymous Mar 2 '10 at 17:55
add comment

I've tidied up an old project of mine:

https://github.com/airtonix/avahi-aliases

requirements:

  • 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
add comment

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
add comment

I would have commented on tomchuk's excellent answer, but I do not have enough reputation on this domain in the stackexchange world.

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

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

    listen-address=127.0.0.1
    address=/localhost.localdomain/127.0.0.1
    address=/localhost/127.0.0.1
    
  3. Restart dnsmasq

share|improve this answer
add comment

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:

#!/usr/bin/python

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("127.0.0.1 " + fileName + ".localhost\n");
share|improve this answer
add comment

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
1  
What would let you specify port numbers? Not DNS AFAIK. –  ptman Mar 3 '10 at 11:45
add comment

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
add comment

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

dns=dnsmasq

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
add comment

Your Answer

 
discard

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.