wildcard in hosts file

11,494

Solution 1

Instead of editing hosts file (which does not work for your requirement), setup a DNS server and setup Wildcard DNS A record -- like

*.example.dev IN  A  1.2.3.4

You mentioned subdomains, but I am assuming you actually want host entries (A address entries) under example.dev. Now abcd.example.dev indicates "abcd" is a host entry, and not a subdomain. Only if you say xyz.abcd.example.dev, then "abcd" becomes a subdomain. The key point is to say - since you want only abcd.example.dev - then you need only DNS A records and it is suggested as above.

Solution 2

I made this simple tool to take the place of hosts. Regular expressions are supported. https://github.com/stackia/DNSAgent Windows only. Probably working with Mono.

A sample configuration:

[
    {
        "Pattern": "^.*$",
        "NameServer": "8.8.8.8"
    },
    {
        "Pattern": "^(.*\\.googlevideo\\.com)|((.*\\.)?(youtube|ytimg)\\.com)$",
        "Address": "203.66.168.119"
    },
    {
        "Pattern": "^.*\\.cn$",
        "NameServer": "114.114.114.114"
    },
    {
        "Pattern": "baidu.com$",
        "Address": "127.0.0.1"
    }
]

Solution 3

AngryHosts can act as a simple DNS, and it's just a tiny software and very easy to use.

Solution 4

You can use echoipdns for this (https://github.com/zapty/echoipdns).

By running echoipdns dev all requests for .dev subdomains are redirected to 127.0.0.1, so any domain with xyz.dev etc will resolve to 127.0.0.1. You can use any other suffix also just replace dev with name you want.

Echoipdns is even more powerful, when you want to use your url from other machines in network you can still use it with zero configuration.

For e.g. If your machine ip address is 192.168.1.100 you could now use a domain name xyz.192-168-1-100.dev which will always resolve to 192.168.1.100. This magic is done by the echoipdns by looking at the ip address in the second part of the domain name and returning the same ip address on DNS query. You will have to run the echoipdns on the machine from which you want to access the remote system.

echoipdns also can be setup as a standalone DNS proxy, so by just point to this DNS, you can now use all the above benefits without running a special command every time, and you can even use it from mobile devices.

So essentially this simplifies the wildcard domain based DNS development for local as well as team environment.

echoipdns works on Mac, Linux and Windows.

NOTE: I am author for echoipdns.

Share:
11,494
pablo
Author by

pablo

Updated on June 13, 2022

Comments

  • pablo
    pablo almost 2 years

    It's not possible to use a wildcard in the hosts file on windows or linux. Is there a way to do it with a browser plugin or something else?

    I have a dev server on a vbox instance so it's practically LAN. I'm creating .dev domain for my virtual hosts so example.com becomes example.dev. For an application I'm creating random subdomains (abd34dn.example.dev) which should all point to the dev server's IP.

    What are my options?