1

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

127.0.0.1 *.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?

2

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 127.0.0.1:

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

Then to test:

$ nslookup thepcspy.com
Server:     127.0.1.1
Address:    127.0.1.1#53

Name:   thepcspy.com
Address: 127.0.0.1

And a subdomain:

$ nslookup testing.thepcspy.com
Server:     127.0.1.1
Address:    127.0.1.1#53

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

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.