Sub topic 16
Split-horizon DNS with views
Split-horizon DNS gives different answers to the same question depending on who asks. Clients inside your network might get www.example.com as 192.0.2.20. Clients on the internet get the public address 198.51.100.20. This chapter shows how to build that with BIND views. It also covers the Unbound equivalent and the failure modes.
This chapter assumes you can already serve an authoritative zone with BIND, write zone files, and use dig, named-checkconf and named-checkzone. It also assumes you understand ACLs and the difference between authoritative and recursive service.
When split-horizon is the right tool
Common reasons to use it:
- Internal clients should reach a service by its private address, while the public name resolves to a public address.
- Internal-only names such as
ldap.example.commust not appear in the public zone. - Internal clients need recursion, and the public must never get it.
Split-horizon is not an access-control mechanism. It hides data from clients that do not match, but it does nothing to stop connections to the hosts themselves. UDP source addresses can also be spoofed. Keep firewalls and service authentication in place.
How BIND views work
A view is a named group of options and zones. When a query arrives, named walks the views in the order they appear in named.conf. It uses the first view whose match-clients list contains the client's source address. That view alone answers the query. Each view also keeps its own cache.
Configuring the views
Put the more specific view first. The last view usually matches any as a catch-all.
acl "internal" {
127.0.0.1;
192.0.2.0/24;
};
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.10; };
dnssec-validation auto;
};
view "internal" {
match-clients { internal; };
recursion yes;
allow-recursion { internal; };
include "/etc/bind/named.conf.default-zones";
zone "example.com" {
type primary;
file "/etc/bind/db.example.com.internal";
};
};
view "external" {
match-clients { any; };
recursion no;
zone "example.com" {
type primary;
file "/etc/bind/db.example.com.external";
};
};Several rules in this file are easy to get wrong:
- Once you define one view, every zone must be inside a view. A zone left at the top level makes
named-checkconffail with an error about zones not being in a view. - The root hints zone (
zone ".") and the localhost zones must be in any view that does recursion. On Debian and Ubuntu they live innamed.conf.default-zones, so include that file inside the internal view, not at the top level. The external view does no recursion, so it does not need them. - A view's cache is separate, so a name looked up through the internal view is cached only there.
- Options set at the top level in
optionsapply to all views unless a view overrides them.
The two zone files
Both files describe the same zone name. Each needs its own serial number, and you must increase it whenever you change that file.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112801 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
3600 ) ; negative caching TTL
IN NS ns1.example.com.
ns1 IN A 192.0.2.10
www IN A 192.0.2.20
ldap IN A 192.0.2.30This is db.example.com.internal. It has private addresses and an internal-only ldap name. The public file keeps only what the world should see:
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112801 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
3600 ) ; negative caching TTL
IN NS ns1.example.com.
ns1 IN A 198.51.100.10
www IN A 198.51.100.20Validating and testing
Check the configuration and both zone files before you reload. named-checkzone takes a view-independent file, so you check each file separately.
sudo named-checkconf
sudo named-checkzone example.com /etc/bind/db.example.com.internal
sudo named-checkzone example.com /etc/bind/db.example.com.external
sudo rndc reloadTo reload one zone in one view, name both the class and the view:
sudo rndc reload example.com IN internalThen query from a host in each position. From a client in 192.0.2.0/24:
dig @192.0.2.10 www.example.com A +short
dig @192.0.2.10 ldap.example.com A +short
dig @192.0.2.10 example.org A +shortThe first query should return 192.0.2.20. The second should return 192.0.2.30. The third should resolve normally through recursion. From a host outside the internal ACL, www returns 198.51.100.20. ldap returns NXDOMAIN, and example.org returns status REFUSED, because recursion is off in that view.
Common problems
| Symptom | Likely cause |
|---|---|
| `named-checkconf` reports a zone "not in a view" | A zone or an `include` of default zones is outside every view |
| Internal clients get public answers | The internal ACL does not contain their source address, for example because of NAT or IPv6 |
| Recursion fails in the internal view | Root hints missing from that view |
| Secondary serves the wrong data | The secondary's transfer request matched the wrong view on the primary |
| Change not visible | Wrong file edited, serial not raised, or reload sent to the wrong view |
The secondary case needs more detail. A secondary asks the primary for a transfer, and the primary picks a view by the secondary's source address. If the secondary is outside the internal ACL, it receives the external zone. To control this, add a TSIG key to match-clients. For example, match-clients { key "internal-key"; }; selects the view by key rather than by address, and the secondary signs its requests with that key.
Split-horizon in Unbound
Unbound has a similar feature, also called views. A view holds local zones and local data. Each client network is mapped to a view with access-control-view.
server:
interface: 127.0.0.1
interface: 192.0.2.10
access-control: 127.0.0.0/8 allow
access-control: 192.0.2.0/24 allow
access-control: 0.0.0.0/0 refuse
access-control-view: 192.0.2.0/24 internal
view:
name: "internal"
local-zone: "example.com." transparent
local-data: "www.example.com. IN A 192.0.2.20"
local-data: "ldap.example.com. IN A 192.0.2.30"The transparent type answers the listed names from local data and resolves everything else in the zone normally. Unbound is a resolver, so this overrides what its own clients see. It cannot serve a different authoritative zone to the public. For that, use BIND views on the authoritative server.
Choosing a design
- Use BIND views when one server must be authoritative for the public and also serve internal clients.
- Use Unbound views when you only need internal clients to see overridden answers and a separate server handles public DNS.
- Prefer two separate servers, one internal and one public, when you can. It removes the shared configuration and the risk of a wrong ACL exposing internal names.
Exercise
A BIND server has two views, "internal" first (match-clients { 192.0.2.0/24; }) and "external" second (match-clients { any; }). A laptop on 192.0.2.77 queries a name, and a monitoring host on 203.0.113.5 queries the same name. Which view answers each one? Then, you edit db.example.com.external and run "rndc reload example.com IN internal". Why does the external answer not change?
Show answer
The laptop matches the first view, so the internal view answers. The monitoring host does not match the internal ACL, so it falls through to the external view. The reload names the internal view, so named reloads only the internal copy of the zone and never rereads the external file. The correct command is "rndc reload example.com IN external", or a plain "rndc reload". You should also raise the serial in the edited file.