Linux Tips guides, explanations and reader checklists on whilenetworking.com

How to Set Up a Local NTP Server Using Chrony or Ntpd on Linux

In environments where internet connectivity is unstable, unreliable, or strictly controlled by corporate firewalls, relying solely on public Network Time…

In environments where internet connectivity is unstable, unreliable, or strictly controlled by corporate firewalls, relying solely on public Network Time Protocol (NTP) servers can lead to significant synchronization issues. Latency spikes or packet loss when reaching external time sources like pool.ntp.org can cause clock drift that disrupts logging, certificate validation, and distributed system consistency. The solution is often a local NTP server. By running an internal time source—typically synchronized with a reliable upstream public server—you create a resilient time infrastructure for your LAN. This guide details how to configure both chrony, the modern standard for Linux distributions, and ntpd (the classic daemon) to serve time to your network clients effectively.

Why Use a Local NTP Server?

Before diving into configuration, it is important to understand the benefits of an internal time source. Public NTP servers are generally accurate, but they introduce variables: network latency, ISP blocking, and intermittent outages. A local server acts as a buffer. Your Linux machine synchronizes with a high-quality upstream (like Google’s or pool.ntp.org) once every few minutes, then instantly serves that time to any device on your local network that requests it.

This architecture ensures that even if the internet goes down for an hour, your internal devices continue to maintain accurate time based on their last known synchronized value. This is critical for:
* Log Correlation: Ensuring logs from different servers show a consistent timeline during incident response.
* Security Protocols: Preventing SSL/TLS certificate errors caused by clock skew.
* Database Integrity: Maintaining consistency in distributed databases like PostgreSQL or MongoDB clusters.

Installing and Configuring Chrony

chrony is generally preferred over ntpd for most modern Linux distributions (Ubuntu, Debian, RHEL, CentOS) because it handles network latency better and supports multiple time sources simultaneously. It also works well on systems with slow CPUs or limited RAM.

Step 1: Installation and Basic Configuration

First, install the package using your distribution’s package manager. On Ubuntu or Debian, run sudo apt update && sudo apt install chrony. On RHEL-based systems, use sudo dnf install chrony or sudo yum install chrony.

Once installed, edit the main configuration file located at /etc/chrony.conf. The default configuration usually points to public pools, which is a good starting point. To turn this machine into a server for your local network, you must add the line allow 192.168.0.0/16 (adjusting the subnet to match your specific LAN range). This tells chrony to accept queries from devices on that local network segment.

Step 2: Defining Upstream Sources

For maximum accuracy, define at least three upstream servers in the server lines of /etc/chrony.conf. It is best practice to mix different types of sources:
* One public pool (e.g., 0.us.pool.ntp.org) for general accuracy.
* One hardware time source if available (e.g., a GPS receiver or an atomic clock connected via USB).
* A second local server if you have a hierarchical setup (server-to-server sync).

Example configuration snippet:

pool 0.us.pool.ntp.org iburst
server time.google.com iburst prefer
allow 192.168.1.0/24
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
binddir9
logchange 0.5 3

Step 3: Enabling and Restarting the Service

After saving the configuration, restart the service to apply changes without rebooting the system: sudo systemctl enable --now chrony. Check the status with systemctl status chrony to ensure it is active and running. If errors occur during startup, review /var/log/syslog or journalctl -u chrony for specific diagnostic messages.

Configuring Ntpd (The Classic Approach)

While chrony is modern, ntpd remains in use on many enterprise systems and legacy hardware. It requires a slightly different approach to configuration and networking.

Step 1: Installing Ntpd

Install the daemon using your package manager: sudo apt install ntp or sudo yum install ntp. Note that on some newer distributions, the package name might differ (e.g., ntpdate is deprecated; always use the full ntpd service).

Step 2: Editing the NTP Configuration

The configuration file for ntpd is typically located at /etc/ntp.conf. Similar to chrony, you need to identify this host as a time server. Add the line restrict default kod nomodify notrap nopeer noquery limited to control access, and then add specific lines to allow your local subnet: restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap.

Unlike chrony, ntpd does not automatically bind to all interfaces by default for security reasons. You may need to explicitly define the interface or use listen on directives depending on your distribution’s version. Ensure your upstream servers are listed near the top of the file with high priority values if you have multiple sources.

Step 3: Starting the Daemon

Restart the service with sudo systemctl restart ntp. Be aware that ntpd can be sensitive to clock changes; it is designed to adjust time gradually once it has been running for a while to avoid disrupting applications relying on wall-clock time. If you need to force an immediate sync, ensure the system clock is roughly correct before starting the daemon.

Handling Security and Access Control

Running an NTP server introduces a potential attack vector if not configured correctly. Attackers can attempt to inject false time data or overwhelm your server with queries (NTP amplification attacks).

To mitigate these risks:
* Disable UDP Port 123 Outbound: If your local clients do not need to query external servers, ensure your firewall blocks outbound traffic on port 123 from the NTP server itself, except for the specific upstream IPs you trust.
* Use MD5 or TLS: Modern chrony and ntpd versions support authentication. You can configure peers using MD5 keys to verify that time packets originate from a trusted source. This prevents man-in-the-middle attacks where an attacker tries to spoof your upstream server’s IP.
* Rate Limiting: Ensure your firewall limits the number of UDP requests per second from any single IP address. Tools like ntpd have built-in rate limiting, but additional iptables rules can provide a safety net.

Real-World Example: A Small Office Setup

Consider a small office with a Linux Ubuntu server at IP 192.168.1.10 and a subnet of 192.168.1.0/24. The goal is to provide stable time to laptops, printers, and IoT devices on the LAN.

  1. Upstream Selection: The admin selects time.google.com as the primary source because it has low latency and high accuracy. They also add pool.ntp.org as a backup in case Google’s service is unavailable.
  2. Local Synchronization: The server runs chrony, syncing to Google every 64 seconds. Even if the internet cuts out, the server retains its last known time with minimal drift (less than a second per day).
  3. Distribution: The config file includes allow 192.168.1.0/24. When a laptop connects to Wi-Fi and runs ntpdate -q 192.168.1.10, it receives an immediate response without needing to traverse the firewall to reach the internet.
  4. Verification: The admin uses chronyc tracking to monitor the offset, showing that the local server is synchronized within milliseconds of the upstream source.

Common Mistakes to Avoid

When setting up a local NTP server, several pitfalls can lead to failure:
* Ignoring Leap Seconds: Some configurations mishandle leap seconds, causing discontinuities in time logs. Ensure your distribution handles this automatically (most do).
* Overloading the Server: Do not run ntpd or chrony on a machine that is also acting as a heavy database server or web host. NTP daemons require CPU cycles to process packets; overload them, and time accuracy degrades.
* Wrong Subnet Masks: A common error is allowing the wrong network range. If you allow 192.168.0.0/16 but your actual LAN is 10.0.0.0/8, unauthorized devices from different networks could potentially query your server, increasing exposure.
* Disabling Logging: Always keep logging enabled. If the time jumps unexpectedly, logs in /var/log/chrony.log or ntpdc -q peers will help diagnose if an upstream server failed or if a network glitch occurred.

Conclusion and Final Checklist

Setting up a local NTP server is a straightforward process that significantly improves the reliability of your Linux infrastructure. Whether you choose the user-friendly chrony or the legacy ntpd, the core principles remain the same: configure reliable upstream sources, restrict access to your trusted local network, and monitor performance regularly.

Quick Setup Checklist:
1. [ ] Install chrony (recommended) or ntp.
2. [ ] Edit configuration to list at least two upstream public servers.
3. [ ] Add firewall rules (allow) for your specific LAN subnet.
4. [ ] Enable the service and set it to start on boot.
5. [ ] Verify synchronization using chronyc tracking or ntpq -p.
6. [ ] Test connectivity from a client machine within the same network.

By implementing these steps, you ensure that your entire local network operates with precise timekeeping, safeguarding your logs, security certificates, and system integrity against the volatility of public internet connections.