How to have ntpd serve only my local subnet

I have a few servers with public Internet IP addresses like A.B.C.x. One of my hosts (A.B.C.10) runs ntpd and I have it syncing it's time from europe.pool.ntp.org.

Now I only want to allow hosts from my subnet (A.B.C.x) to be able to sync to A.B.C.10. By default the whole world can sync to my NTP server. How do I accomplish this?


All examples I can find assume that I'm syncing to specific IP addresses but I sync to DNS names and as far as I can tell the IP addresses that the DNS names x.europe.pool.ntp.org point to are variable. So I can't setup exceptions in my firewall and I can't use the restrict option in ntp.conf because it too only accepts IP addresses and not DNS names (Oh! and restrict applies both to clients and to servers as firewall rules do!)


Solution 1:

You've got several options, and it depends on where firewalls are placed and/or which ones you prefer to work with. Ideally you would have a firewall that you can control on the subnet. Less ideally you'll only be dealing with a host level firewall on the NTP server. Either way the concept is the same.

For a subnet firewall:

  • Allow UDP port 123 out of the subnet only from A.B.C.10
  • Deny UDP port 123 from everything else.

For a host firewall on the NTP server:

  • Allow UDP port 123 from your subnet (and from localhost)
  • Deny UDP port 123 from everywhere else (a deny all rule later in the chain).

e.g. to allow 10.0.0.0/8:

# allow 10.0.0.0/8
iptables -A INPUT -s 10.0.0.0/8 -p udp -m udp --dport 123 -j ACCEPT
# allow localhost
iptables -A INPUT -s 127.0.0.0/8 -p udp -m udp --dport 123 -j ACCEPT
# allow NTP packets _from_ your host to everyone else
iptables -A OUTPUT -p udp --sport 123 --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
# allow replies from hosts you've sent NTP packets to
iptables -A INPUT  -p udp --sport 123 --dport 123 -m state --state ESTABLISHED -j ACCEPT
# the following is only useful if you have a policy ACCEPT for INPUT
iptables -A INPUT  -p udp -m udp --dport 123 -j DROP

Solution 2:

Basic ntp.conf for localnet serving look like that

####
driftfile       /etc/ntp.drift
disable         monitor
restrict -4     default kod nomodify nopeer noquery notrap
restrict -6     default kod nomodify nopeer noquery notrap
restrict        127.0.0.1
restrict        127.127.1.0
restrict -6     ::1

restrict        10.0.0.0    mask 255.0.0.0
restrict        172.16.0.0  mask 255.240.0.0
restrict        192.168.0.0 mask 255.255.0.0

server          0.pool.ntp.org       iburst
server          1.pool.ntp.org       iburst
server          2.pool.ntp.org       iburst
####

Two longest lines deny any access to the server by default and then other restric directives allow only specific hosts and subnets.