Wildcard subdomains with dnsmasq
Asked Answered
H

2

60

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?

Humidify answered 10/3, 2014 at 22:57 Comment(0)
P
75

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

Prow answered 21/3, 2014 at 5:57 Comment(5)
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.Chianti
@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.Prow
See below: a leading dot is not a wildcardSuperorder
but this can only add one record for a wilcard domain, how can i have multiple record?Sociality
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.1Amphistylar
S
79

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.

Staysail answered 26/5, 2016 at 0:9 Comment(11)
Which version of dnsmasq supports overrides as stated above? I've got dnsmasq 2.76-g0007ee9 from Asus Merlin and it doesn't workWear
@Wear 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.Staysail
is it possible to do address=.example/*/foo ? doesn't seem to work for me.Satinwood
@Satinwood 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 *.Staysail
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?Froghopper
@Froghopper To the best of my knowledge, no it's not possible.Staysail
Is the order important? In the above example, what would sub2.sub.domain.tld resolve to?Anniceannie
@SebastianSchrader seems '#' can be used alone, not like #.example.com, right?Sociality
# is a special value handled by dnsmasq, that matches any domain, it can't be used as a wildcard for parts of a domain.Staysail
You can add all subdomains overrides to dnsmasq-hosts file and instruct dnsmasq to use this file with arg -H. -H, --addn-hosts=<file> 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.Bjork
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.1Amphistylar
P
75

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

Prow answered 21/3, 2014 at 5:57 Comment(5)
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.Chianti
@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.Prow
See below: a leading dot is not a wildcardSuperorder
but this can only add one record for a wilcard domain, how can i have multiple record?Sociality
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.1Amphistylar

© 2022 - 2024 — McMap. All rights reserved.