OpenNTPD (part of the OpenBSD project) is a daemon that can be used to synchronize the system clock to internet time servers using the Network Time Protocol, and can also act as a time server itself if needed.

OpenNTPD (part of the OpenBSD project) is a daemon that can be used to synchronize the system clock to internet time servers using the Network Time Protocol, and can also act as a time server itself if needed.

Revision as of 06:22, 16 December 2012

OpenNTPD (part of the OpenBSD project) is a daemon that can be used to synchronize the system clock to internet time servers using the Network Time Protocol, and can also act as a time server itself if needed.

Warning: OpenNTPD is not currently maintained for Linux (see this thread): users interested in its functions should better use NTPd.

Installation

The default configuration is actually usable if all you want is to sync the time of the local computer. For more detailed settings, the /etc/ntpd.conf file must be edited:

To sync to a particular server, uncomment and edit the "server" directive. You can find the server's URL in your area at www.pool.ntp.org/zone/@.

server ntp.example.org

The "servers" directive works the same as the "server" directive, however, if the DNS name resolves to multiple IP address, ALL of them will be synced to. The default, "pool.ntp.org" is working and should be acceptable in most cases.

pool.ntp.org

Any number of "server" or "servers" directives may be used.

If you want the computer you run OpenNTPD on to also be a time server, simply uncomment and edit the "listen" directive.

For example:

listen on *

will listen on all interfaces, and

listen on 127.0.0.1

will only listen on the loopback interface.

Your time server will only begin to serve time after it has synchronized itself to a high resolution. This may take hours, or days, depending on the accuracy of your system.

If you would like to run OpenNTPD at boot, add openntpd the DAEMONS variable in your /etc/rc.conf following your network daemon.

DAEMONS=(syslog-ng network openntpd ...)

If openntpd is being used to set local system time only, it may be safely backgrounded.

DAEMONS=(syslog-ng network @openntpd ...)

To see the status of NTP syncing, visit /var/log/daemon.log and look for entries with "ntpd".

OpenNTPD adjusts the clock by small amounts at a time. It is designed this way to prevent sudden, large time fluctuations in your system, which could adversely affect system services (e.g., cron jobs). Thus, it can take some time to correct the time.

If your clock is off by more than 180 seconds you can try "ntpd -s -d" in the console. If ntpd is already running, you can simply restart it with sudo /etc/rc.d/openntpd restart, as the Arch openntpd package uses the "-s" flag by default. See man ntpd for more info. You can also set the system clock to as close to possible to the actual time and then let OpenNTPD fine tune the time.

Enable OpenNTPD through systemd

If you are using systemd init instead, you must enable the service related to OpenNTPD as follows:

# systemctl enable openntpd

Then reboot.

Alternatively start manually without autostart on boot:

# systemctl start openntpd

Making openntpd dependent upon network access

If you have intermittent network access (you roam around on a laptop, you use dial-up, etc), it does not make sense to have openntpd running as a system daemon on start up. Here are a few ways you can control openntpd based on the presence of a network connection. These instructions should also work for ntpd found further below.

Using netcfg

If you are using netcfg, you can also start/stop openntpd as a POST_UP/PRE_DOWN command in your network profile:

Using dhcpcd hooks

Troubleshooting

Error adjusting time

If you find your time set incorrectly and in log you see:

openntpd adjtime failed: Invalid argument

Try:

ntpd -s -d

This is also how you would manually sync your system.

Increasing time shift

Starting openntpd in the background could lead to synchronization errors between the actual time and the time stored on your computer. If you recognize an increasing time difference between your desktop clock and the actual time, try to start the openntpd daemon normal and not in the background.

Initialization Failure

Openntpd may fail to initialize properly if it is started before the network is fully configured. In some cases you may want to remove openntpd from the DAEMONS array in /etc/rc.conf and add the following line to /etc/rc.local:

(sleep 300 && /etc/rc.d/openntpd start) &

Note: This method is an alternative to the four methods listed above. The other three methods are preferred and work better. Use this as a last resort.

This will wait 5 minutes before starting openntpd, which should give the system sufficient time to set up the network properly. If your network settings change often, you may also consider restarting the daemon regularly with cron.