已索引

ntpd 的服务器和客户端都是使用的同一个软件包 ntp,同一个配置文件 /etc/ntp.conf,同一个服务 ntpd。
ntpd 使用 UDP/123 端口。

极简配置

通常来说,不论是配置NTP服务端,还是配置NTP客户端,我们只需要配置server参数,指定自己的上级服务器即可。
其它参数一般不会去动,但可以了解。

# vim /etc/ntp.conf
……
server 210.72.145.44 iburst
server 0.pool.ntp.org iburst
……

说明:

  • 在 system-confi-date 工具里,如果在 "Advanced Options" 里勾选“Speed up initial synchronization”,就会在所有 server 最后面加上 iburst,如果没勾选,则不添加 iburts。
  • iburst can reduce the time to synchronize the clock at initial startup (iburst).

参考文档:
https://www.ntp.org/documentation/4.2.8-series/confopt/
https://www.ntp.org/documentation/4.2.8-series/poll/

如果需要将服务器配置成NTP服务端,必须要有 server 参数,如果没有上层 server 可用,则必须将本地时钟作为时间源(见后文)。否则客户端不能与该时间服务器进行时间同步。ntp server和自身或者与其server的同步需要一个时间段,初次启动NTP服务,这个过程可能是5分钟,重启NTP服务,这个过程可能是几秒,如果NTP server还没有和其自身或者它的server同步上,客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。可以使用ntpq -p命令查看ntp server是否完成了自身的同步过程。

与本地时间同步

server  127.127.1.0     # local clocks
fudge   127.127.1.0 stratum 10     

说明:

  • 这儿定义了将本地时间做为时间源,即当网络上的server不可用时,使用服务器自身的时钟作为时间源。
  • 在 system-confi-date 工具里,如果在 "Advanced Options" 里勾选“Use Local Time Source”,就会有 server 127.127.1.0 这一行,但没有紧接的 fudge 这一行。如果没勾选则不会有。
  • fudge command can be used to configure reference clocks in special ways. It must immediately follow the server command which configures the driver.
  • stratum option specifies the stratum number assigned to the driver in the range 0 to 15, inclusive. This number overrides the default stratum number ordinarily assigned by the driver itself, usually zero. 默认参数文件配置为10,红帽培训环境的值为8。

参考文档:
https://www.ntp.org/documentation/4.2.8-series/clockopt/

访问控制

restrict <网络> <参数>
网络部分可也是IP信息,也可以是 default(默认,即所有的IP)
参数有以下几个。注意:如果没有指定参数,那就表示该网络没有任何限制!
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询

如下两行默认开启,第1行表示IPv4,第2行表示IPv6,默认允许所有IP进行时间同步,但不允许查询和修改。

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

如下两行默认开启,第1行表示IPv4,第2行表示IPv6,没有参数表示允许本机的所有操作。

restrict 127.0.0.1 
restrict -6 ::1

如果想对本地网络的机器开放更大的权限,可以将如下行取消注释,并根据实际情况进行修改。

# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

网上很多文档说必须开放本地网段的权限,其实没有必要, restrict default 已经默认允许了所有来源进行同步。

driftfile

driftfile /var/lib/ntp/drift(默认)
  • This file is used to record the frequency of the local clock oscillator.
  • If the file exists, it is read at startup in order to set the initial frequency and then updated once per hour or more with the current frequency computed by the daemon.
  • If the file name is specified, but the file itself does not exist, the starts with an initial frequency of zero and creates the file when writing it for the first time.
  • If this command is not given, the daemon will always start with an initial frequency of zero.
  • The file format consists of a single line containing a single floating point number, which records the frequency offset measured in parts-per-million (PPM)(百万分之一).
  • The file is updated by first writing the current drift value into a temporary file and then renaming this file to replace the old version.

参考文档:https://www.ntp.org/documentation/4.2.8-series/miscopt/

-- By 许望(RHCA、OCM、VCP)
最后修改:2023 年 09 月 15 日 04 : 03 PM
如果觉得我的文章对你有用,请随意赞赏