Ntp 服务配置

NTP

一、使用 ntpd 服务配置

step 1 配置 NTP

cat /etc/ntp.conf
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

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

restrict 127.0.0.1
restrict -6 ::1

restrict 192.168.1.1 mask 255.255.0.0 nomodify notrap

restrict 192.168.1.10
restrict 192.168.1.11
restrict 192.168.1.12

server 192.168.1.10 prefer
server 192.168.1.11
server 192.168.1.12
server 127.127.1.0

开启本地模式,防止远程 NTP 服务异常时 NTP 服务不可用。

step 2 配置防火墙

systemctl enable --now firewalld
firewalld-cmd --all-service=ntp --permaent
firewalld-cmd --reload

step 3 配置开机自启动

systemctl enable --now ntpd.service

step 4 禁用 chronyd 服务

因 ntp 服务与 chronyd 服务存在冲突,导致重启后服务无法正常工作,需要将 chronyd 服务禁用。

systemctl disable --now chronyd.service

二、使用 chronyd 服务配置

chronyd 为 ntpd 的替代品,具有一些 ntpd 不具备的特性,如 chronyd 同步的周期相对 ntpd 来说要快很多,时间跃进的方式种类也较多,常见的 server 端配置如下

# server 上游 ntp 服务地址 iburst 缩短前四次更新时间 prefer 优先使用这个源
server 192.168.1.2 iburst prefer
server 192.168.1.3 iburst
# chronyd 程序计算真实时间与系统时间增加或减少的比率
driftfile /var/lib/chrony/drift
# hwclock 程序所用到的文件
hwclockfile /etc/adjtime
# 内核每 11 分钟 RTC 时间复制到系统时间
rtcsync
# 允许 client 访问的范围
allow 192.168.0.0/16
# 开启本地模式 stratum 距离 ntp 服务的层数
local stratum 10
log statistics measurements tracking
logdir /var/log/chrony
# chronyd 程序计算系统时钟相对时钟源的快慢,默认值1000ppm,LAN 内值 5~10ppm
maxupdateskew 10.0

客户端程序执行

> chronyc  sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.1.2                  10   6   377   177    -37ns[-1067ns] +/- 4963ns
Infee Fang
Infee Fang
互联网二手搬砖工