I need to add a rule for all subdomains of a domain. I imagine it should looks like this: *.example.com

As you can see, I need to redirect all requests to any subdomain on example.com to localhost. Well, I know that it is impossible to do it with hosts file. What else can I use in this case?


As you say, you can't put wildcards in /etc/hosts. It just doesn't work.

That really leaves you with one option: mangle the actual DNS manually. There are several ways you could do this but two spring to mind as the best options...

You could run a fully-flegded DNS server locally. This can be a good idea if your internet connection is slow and you'd like all devices on the network to proxy through this server, or you run many domains that you want to override, or some other reason you want to step into the first ring of hell... It won't take a week, but it does requires learning stuff.

Or you could just override dnsmasq's settings. Ubuntu through network-manager is already running a sort of DNS proxy in the form of dnsmasq and you can tell it to override various domains, quite easily. Here's how I'd route *.thepcspy.com (my domain) to

echo address=/thepcspy.com/ | sudo tee -a /etc/NetworkManager/dnsmasq.d/local
sudo service network-manager restart

Then to test:

$ nslookup thepcspy.com

Name:   thepcspy.com

And a subdomain:

$ nslookup testing.thepcspy.com

Non-authoritative answer:
Name:   testing.thepcspy.com

To revert these changes, just edit (or delete) /etc/NetworkManager/dnsmasq.d/local and restart network-manager again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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