安装

配置

配置连接到 NTP 服务器

在你的 /etc/ntp.conf 配置文件中定义的第一件事是你机器想同步的服务器。

NTP servers are classified in a hierarchical system with many levels called strata: the devices which are considered independent time sources are classified as stratum 0 sources; the servers directly connected to stratum 0 devices are classified as stratum 1 sources; servers connected to stratum 1 sources are then classified as stratum 2 sources and so on.

It has to be understood that a server's stratum cannot be taken as an indication of its accuracy or reliability. Typically, stratum 2 servers are used for general synchronization purposes: if you do not already know the servers you are going to connect to, you should use the pool.ntp.org servers (alternate link) and choose the server pool that is closest to your location.

In order for this method to work you have to make sure that, when rc.local is executed, the network connection has already been initialized (for example you should not background essential network-related daemons in /etc/rc.conf)

Warning:

Using this method is discouraged on servers and in general on machines that need to run continuously for more than 2 or 3 days, as the system clock will be updated only once at boot time.

Running ntpd -qg as a cron event is to be avoided, unless you are aware of how your running applications would react to instantaneous system time changes.

If something other already takes care of updating the hardware clock, for example another operating system in dual boot, you should avoid starting hwclock.

作为守护进程运行

启动 ntpd

ntpd sets 11 minute mode, which syncs the system clock to hardware every 11 minutes. The hwclock daemon measures hardware clock drift and syncs it, which conflicts with ntpd.

Stop the hwclock daemon (if it is running):

# rc.d stop hwclock

Start the ntpd daemon:

# rc.d start ntpd

Add ntpd to your DAEMONS array so it starts automatically on boot and make sure hwclock is disabled:

/etc/rc.conf

DAEMONS=(... !hwclock ntpd ...)

NetworkManager

Note: ntpd should still be running when the network is down if the hwclock daemon is disabled, so you should not use this.

It is relatively difficult to be sure that your driftfile configuration is actually working without waiting a while, as ntpd does not read or write it very often. If you get it wrong, it will log an error; if you get it right, it will update the timestamp. If you do not see any errors about it after a full day of running, and the timestamp is updated, you should be confident of success.

Alternatives

Available alternative to NTPd are Chrony, a dial-up friendly and specifically designed for systems that are not online all the time, and OpenNTPD, part of the OpenBSD project and currently not maintained for Linux.