195 lines
7.7 KiB
Markdown
195 lines
7.7 KiB
Markdown
# Hosts files (local names, dnsmasq style)
|
||
|
||
EasyDNS can answer names from files in `/etc/hosts` format, the way dnsmasq does by
|
||
default. A name found there is answered **locally and first** – before local zones, the
|
||
block lists, forwarding and recursion. Everything else continues to the usual resolution.
|
||
|
||
```yaml
|
||
hosts:
|
||
files: ["/etc/hosts"] # [] (off)
|
||
ttl: 0 # [0] TTL of the answers, like dnsmasq local-ttl
|
||
domain: lan # [none] also answer bare names as <name>.lan
|
||
reload_interval: 5s # [5s] check the files for changes; 0s = only on reload
|
||
```
|
||
|
||
The feature is off while `files` is empty.
|
||
|
||
## File format
|
||
|
||
The standard hosts format, one address per line, followed by one or more names:
|
||
|
||
```
|
||
# /etc/hosts
|
||
127.0.0.1 localhost
|
||
::1 localhost ip6-localhost ip6-loopback
|
||
192.168.1.1 router gw
|
||
192.168.1.10 nas nas.home # comments are allowed at the end of a line
|
||
fd00::10 nas
|
||
192.168.1.50 git.linuxiarz.pl # override a public name inside the LAN
|
||
```
|
||
|
||
* IPv4 and IPv6 addresses; a link-local zone index (`fe80::1%eth0`) is ignored,
|
||
IPv4-mapped addresses (`::ffff:192.0.2.1`) are treated as IPv4.
|
||
* All names on a line get the address (A or AAAA). The same name may appear on several
|
||
lines; all its addresses are returned (duplicates are removed).
|
||
* Names are case-insensitive. Lines that do not parse are skipped and counted in the
|
||
`hosts files loaded … skipped_lines=N` log message.
|
||
* Files are read as UTF-8 (invalid bytes are replaced); files over 64 MiB are refused.
|
||
|
||
## Answers
|
||
|
||
| Query | Answer |
|
||
| --- | --- |
|
||
| `A` / `AAAA` for a name in the files | its addresses of that family, AA flag set, TTL = `hosts.ttl` |
|
||
| `A` for a name that has only IPv6 addresses (or the reverse), or any other type (`MX`, `TXT`, …) | **NODATA** (NOERROR, empty answer) – the question is not forwarded |
|
||
| `ANY` | all A and AAAA addresses of the name |
|
||
| `PTR` for an address from the files | the **first name** of the **first line** with that address (as glibc and dnsmasq do); with `domain:` set, a bare name is returned qualified (`nas.lan.`) |
|
||
| a name not in the files | normal processing: local zones, policy, forwarding, recursion |
|
||
|
||
`0.0.0.0` / `::` lines are answered as written (handy for blocking a name), but no PTR
|
||
record is made for them.
|
||
|
||
The NODATA rule means a name in the hosts files "owns" the name: an `AAAA` query for an
|
||
IPv4-only host returns an empty answer at once instead of a slow or misleading upstream
|
||
lookup. This is what dnsmasq does as well.
|
||
|
||
## Bare names and `domain`
|
||
|
||
A name without a dot (`nas`) is answered as `nas.` – clients that try the plain name, and
|
||
dnsmasq-style setups that rely on it, work unchanged.
|
||
|
||
With `domain: lan`, every bare name is also answered as `<name>.lan` (dnsmasq
|
||
`expand-hosts` + `domain=lan`). Names that already contain a dot are not changed. To make
|
||
the whole domain local – unknown `*.lan` names get NXDOMAIN and are never sent to the
|
||
Internet, like dnsmasq `local=/lan/` – also list it in `recursion.local_only_suffixes`:
|
||
|
||
```yaml
|
||
hosts: { files: ["/etc/hosts"], domain: lan }
|
||
recursion:
|
||
local_only_suffixes: [lan]
|
||
```
|
||
|
||
(Names such as `local`, `home.arpa`, `test`, `localhost` and the private reverse zones are
|
||
local-only already; see [hybrid.md](hybrid.md).)
|
||
|
||
## Several files and globs
|
||
|
||
```yaml
|
||
hosts:
|
||
files:
|
||
- /etc/hosts
|
||
- /etc/easydns/hosts.d/*.hosts # dnsmasq addn-hosts / hostsdir
|
||
- lan.hosts # relative to the directory of config.yaml
|
||
```
|
||
|
||
* `*` and `?` are allowed in the file name (not in directory names). Hidden files (`.x`)
|
||
and editor backups (`x~`) are ignored; matching files are read in alphabetical order.
|
||
* A missing file is not an error: `check-config` reports a warning and the file is read
|
||
as soon as it appears. A glob that matches nothing is simply empty.
|
||
* Files are read in the order listed; for PTR records the first line wins across all files.
|
||
|
||
## Who gets these answers
|
||
|
||
Hosts answers are given to clients that may resolve, i.e. clients in
|
||
`recursion.allowed_clients` (the same list that controls forwarding and recursion).
|
||
Other clients – for example the Internet asking an authoritative server – never see the
|
||
contents of the hosts files; they get the usual answer (local zone or REFUSED).
|
||
|
||
Precedence for allowed clients:
|
||
|
||
1. **hosts files**
|
||
2. local authoritative zones (most specific zone in the client's view)
|
||
3. local-only names (RFC 6761/6303 and `recursion.local_only_suffixes`) → NXDOMAIN
|
||
4. allow/block policy
|
||
5. conditional forwarding, then the default resolution
|
||
|
||
Hosts entries therefore win over a zone that contains the same name and are **not**
|
||
subject to the block lists (they are your own data, like zone records). A name that
|
||
leaves a local zone through a CNAME is looked up in the hosts files too before it is
|
||
forwarded.
|
||
|
||
## Changes and reload
|
||
|
||
* Every `reload_interval` (default 5 s) EasyDNS compares the modification time and size
|
||
of the files (and the list of files matched by the globs). On a change the files are
|
||
re-read in the background and swapped in atomically; cached answers are dropped at once.
|
||
No restart or reload is needed after editing `/etc/hosts`.
|
||
* `reload_interval: 0s` turns the check off; the files are then read at start and on
|
||
`easydns reload` / `systemctl reload easydns` / `kill -HUP` (dnsmasq behaviour).
|
||
* A configuration reload always re-reads the files.
|
||
|
||
Log line after each load:
|
||
|
||
```
|
||
INFO hosts files loaded files=2 names=17 skipped_lines=0
|
||
```
|
||
|
||
## Checking
|
||
|
||
```sh
|
||
easydig @127.0.0.1 nas A
|
||
easydig @127.0.0.1 nas.lan AAAA
|
||
easydig @127.0.0.1 -x 192.168.1.10
|
||
```
|
||
|
||
With `logging.query_log: true` these answers are logged with `source=local
|
||
note="hosts file"`.
|
||
|
||
## Migrating from dnsmasq
|
||
|
||
| dnsmasq | EasyDNS |
|
||
| --- | --- |
|
||
| reads `/etc/hosts` (default) | `hosts: { files: ["/etc/hosts"] }` |
|
||
| `no-hosts` | leave `hosts.files` empty (default) |
|
||
| `addn-hosts=/path/file` | add the file to `hosts.files` |
|
||
| `hostsdir=/path` | `hosts.files: ["/path/*"]` (re-read automatically) |
|
||
| `expand-hosts` + `domain=lan` | `hosts.domain: lan` |
|
||
| `local=/lan/` | `recursion.local_only_suffixes: [lan]` |
|
||
| `local-ttl=N` | `hosts.ttl: N` |
|
||
| `server=127.0.0.1#55` | `upstreams.servers: ["127.0.0.1:55"]` + `resolution.default: forward` |
|
||
| `server=/corp/10.0.0.1` | `forward_zones: [{ domain: corp, upstreams: ["10.0.0.1"] }]` |
|
||
| `no-resolv` | (EasyDNS never reads `/etc/resolv.conf`) |
|
||
| `port=54`, `listen-address=0.0.0.0`, `bind-interfaces` | `server.listen: ["0.0.0.0:54"]` |
|
||
| `bogus-priv` | built in: private reverse zones are local-only (RFC 6303) |
|
||
| `dnssec` | `recursion.dnssec_validation: true` (validation applies to recursion) |
|
||
| `clear-on-reload` | a configuration reload drops the answer cache; `easydns cache clear` empties the resolver cache |
|
||
| `enable-ra`, DHCP | not provided – EasyDNS is a DNS server only |
|
||
|
||
Example – a dnsmasq forwarder on port 54 in front of a resolver on 127.0.0.1:55,
|
||
answering `/etc/hosts` first:
|
||
|
||
```yaml
|
||
server:
|
||
mode: forwarder
|
||
listen: ["0.0.0.0:54"]
|
||
recursion:
|
||
allowed_clients: ["acl:lan", "::1/128"]
|
||
acls:
|
||
lan: ["192.168.0.0/16", "10.0.0.0/8", "127.0.0.1/32"]
|
||
hosts:
|
||
files: ["/etc/hosts"]
|
||
resolution:
|
||
default: forward
|
||
upstreams:
|
||
servers: ["127.0.0.1:55"]
|
||
```
|
||
|
||
## Docker
|
||
|
||
Inside a container `/etc/hosts` is the container's own file. Mount the host's file (or a
|
||
dedicated one) read-only and point `hosts.files` at it:
|
||
|
||
```yaml
|
||
volumes:
|
||
- /etc/hosts:/etc/easydns/hosts:ro
|
||
```
|
||
|
||
```yaml
|
||
hosts: { files: ["/etc/easydns/hosts"] }
|
||
```
|
||
|
||
A single-file bind mount keeps the original inode; editors that replace the file (write
|
||
a new file and rename it) break such a mount. Mount a directory instead
|
||
(`./hosts.d:/etc/easydns/hosts.d:ro` with `files: ["/etc/easydns/hosts.d/*"]`) if the
|
||
file is edited on the host.
|