Tell me more ×
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
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

migrated from stackoverflow.com Mar 2 '10 at 17:06

9 Answers

up vote 15 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

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". Done.

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
4  
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
1  
OSX with brew: same as above but brew install dnsmasq – Matt Humphrey Mar 7 at 21:41
fyi brew > port (re @MattHumphrey suggestion) – electblake Mar 22 at 2:57

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

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

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

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

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

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

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.