57

I have a device that is already mapped to domain.tld. I now want to create a wildcard for all subdomains *.domain.tld so that they are mapped to the ip of domain.tld, too. How do I do this with dnsmasq?

cweiske
  • 30,033
  • 14
  • 133
  • 194
danb
  • 573
  • 1
  • 4
  • 5

2 Answers2

76

While the accepted answer may have solved the author's problem, it is misleading as it suggests that the leading dot would match subdomains only, which is not true.

dnsmasq ignores any leading dots, so that

address=/domain.tld/192.168.0.1

is equivalent to

address=/.domain.tld/192.168.0.1

or even

address=/......domain.tld/192.168.0.1

As of now (dnsmasq v2.76), there is unfortunately no way to specify some things (server, address, ipset) for

  • a single domain name only, i.e. domain name without its subdomains
  • only for the subdomains of a domain name

The only thing you can do, is to specify something for a domain name and all its subdomain and have override it for specific subdomains, e.g.

address=/domain.tld/192.168.0.1
address=/sub.domain.tld/10.10.0.1

This single domain name only case, may be tackled with different options, such as host-record, srv-record, which may be sufficient in some cases, but certainly not in all. It won't work for example, if you really need to use the server or ipset option for a single domain name only.

Sebastian Schrader
  • 1,453
  • 15
  • 19
  • Which version of dnsmasq supports overrides as stated above? I've got dnsmasq 2.76-g0007ee9 from Asus Merlin and it doesn't work – Hengjie Jan 16 '17 at 21:32
  • @Hengjie I just tested the overrides in the above example configuration with a build of the official 2.76 release of dnsmasq. I put the two lines in a temporary config file, executed `dnsmasq -p 5353 -k -C /tmp/dnsmasq.conf` and tested with `dig @127.0.0.1 -p 5353 sub.domain.tld`. Anyway I don't recall that overriding settings this way has been added in any particular version of dnsmasq. It should basically work with any dnsmasq version. – Sebastian Schrader Feb 01 '17 at 17:22
  • is it possible to do `address=.example/*/foo` ? doesn't seem to work for me. – chovy Mar 25 '17 at 03:55
  • @chovy You should probably ask a separate question. It is unclear, what you intend to do and what doesn't work. Something which I can definitely say is, that the wildcard in the address option is `#` not `*`. – Sebastian Schrader Mar 27 '17 at 18:03
  • 1
    Is it possible to do something like `address=localhost.*/127.0.0.1`? That is point any host with a subdomain of localhost to 127.0.0.1? – CMCDragonkai Jul 13 '17 at 02:14
  • @CMCDragonkai To the best of my knowledge, no it's not possible. – Sebastian Schrader Jul 13 '17 at 10:28
  • Is the order important? In the above example, what would `sub2.sub.domain.tld` resolve to? – chizou Apr 11 '19 at 01:36
  • @SebastianSchrader seems '#' can be used alone, not like #.example.com, right? – Y. King Oct 29 '20 at 07:59
  • `#` is a special value handled by dnsmasq, that matches any domain, it can't be used as a wildcard for parts of a domain. – Sebastian Schrader Oct 29 '20 at 15:57
  • You can add all subdomains overrides to `dnsmasq-hosts` file and instruct `dnsmasq` to use this file with arg `-H`. -H, --addn-hosts= Additional hosts file. Read the specified file as well as /etc/hosts. If --no-hosts is given, read only the specified file. This option may be repeated for more than one additional hosts file. If a directory is given, then read all the files contained in that directory. – niziak Jan 11 '21 at 08:49
  • Wildcards in dnsmasq works out of the box... `address=/domain.tld/192.168.0.1` domain.tld and also any subdomain e.g. sub.domain.tld will be translated to 192.168.0.1. When we add entry `address=/another.domain.tld/192.168.0.255` it will be translated to 192.168.0.255 but any not defined subdomain to 192.168.0.1 – mikep Jun 14 '22 at 15:37
74

In the dnsmasq.conf file, add the line

address=/.domain.tld/192.168.0.1

But use the IP you actually want as that end bit

cweiske
  • 30,033
  • 14
  • 133
  • 194
EkriirkE
  • 2,277
  • 19
  • 13
  • 7
    I would usually create a separate configuration file for each host and place it in the '/etc/dnsmasq.d/' directory. The advantage to this is not having to deal with such a monolithic file when it comes time for maintenance. – Luke A. Leber Nov 07 '15 at 22:41
  • 1
    @LukeA.Leber that sound even more tedious as finding the correct host file would be the same or even less "maintainable" as a line in a file. Then on top of locating the file you have to make the same edit(s). More work. – EkriirkE Nov 14 '15 at 14:57
  • 8
    See below: a leading dot is not a wildcard – Cédric Nilly Nov 05 '16 at 18:33
  • but this can only add one record for a wilcard domain, how can i have multiple record? – Y. King Oct 29 '20 at 07:56
  • 2
    Wildcards in dnsmasq works out of the box... `address=/domain.tld/192.168.0.1` domain.tld and also any subdomain e.g. sub.domain.tld will be translated to 192.168.0.1. So entering dot at the start of misleading. When we add entry `address=/another.domain.tld/192.168.0.255` it will be translated to 192.168.0.255 but any not defined subdomain to 192.168.0.1 – mikep Jun 14 '22 at 15:35