一、时间服务器Server端配置
0.防火墙配置
Server端须开通123upd的端口。Client端似乎不需要添加防火墙配置。
-A INPUT -p udp --dport 123 -j ACCEPT
1.安装ntp
yum install ntp -y
2.编辑/etc/ntp.conf配置文件
配置文件中的全部内容如下所示,主要限制了ip段可以来访问同步时间但是不能修改时间,限制了世界时间可以来修改时间。
从三个世界时间来同步时间,perfer代表优先。
设置日志位置和pid位置。
driftfile /var/lib/ntp/drift
restrict 172.19.150.0 mask 255.255.255.0 nomodify notrap
restrict 172.19.11.0 mask 255.255.255.0 nomodify notrap
restrict 172.19.140.0 mask 255.255.255.0 nomodify notrap
restrict times.aliyun.com nomodify
restrict ntp.aliyun.com nomodify
restrict cn.pool.ntp.org nomodify
server times.aliyun.com iburst prefer
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
logfile /var/log/ntpstats/ntpd.log # 定义ntp日志目录
pidfile /var/run/ntp.pid # 定义pid路径
keys /etc/ntp/keys
3.启动NTP并设置开机启动
chkconfig ntpd on
service ntpd start
3.5 硬件时间与系统时间同步
(目前此步骤还不确定生效的验证方式和作用)
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
vi /etc/sysconfig/ntpd # 实现硬件时间与系统时间同步
----------------------------------------------------------
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes
4.验证NTP服务启动情况
ss -tunlp|grep ntp
可以用来检测ntpd服务的端口情况,默认端口是123.
ss -tunlp|grep ntp
-----------------------------
tcp UNCONN 0 0 172.19.25.66:123 *:* users:(("ntpd",49820,19))
tcp UNCONN 0 0 127.0.0.1:123 *:* users:(("ntpd",49820,18))
tcp UNCONN 0 0 *:123 *:* users:(("ntpd",49820,16))
tcp UNCONN 0 0 fe80::f816:3eff:fec8:4706:123 :::* users:(("ntpd",49820,21))
tcp UNCONN 0 0 ::1:123 :::* users:(("ntpd",49820,20))
tcp UNCONN 0 0 :::123 :::* users:(("ntpd",49820,17))
ntpstat
可以确认本地NTP与上层NTP服务器是否联通。
ntpstat
---------------------------
synchronised to NTP server (120.25.115.19) at stratum 3
time correct to within 34 ms
polling server every 64 s
ntpq -p
可以查看本地NTP需进行同步的公网NTP服务器状态。
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*120.25.115.19 10.137.53.7 2 u 60 64 177 40.669 6.162 1.107
+203.107.6.88 10.137.38.86 2 u 56 64 177 15.177 6.799 0.764
ntpq -p 参数详解
remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
如果有[*] 代表目前正在使用当中的上层NTP服务器。
如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选NTP服务器
如果有[-] 代表同步的该NTP服务器被认为是不合格的NTP Server
如果有[x] 代表同步的外网NTP服务器不可用
refid :指的是给上层NTP服务器提供时间校对的服务器。
St:上层NTP服务器的级别。
When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
Poll :本地主机与上层NTP服务器进行时间校对的周期(单位:s)
reach:八进制数,表示最近8次时钟同步包接收情况(1表示接收成功,0表示接收失败。每接收一个包左移一位。对于一个运行较长时间的NTP client而言,这个值应该是377->11,111,111,即最近8次包接收均成功;否则表示有丢包情况发生).
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位为10^(-6) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。
二、客户端NTP配置
客户端配置文件:主要修改restrict限制部分和Server选择部分。
restrict 172.19.25.66 nomodify notrap noquery
restrict 127.0.0.1
restrict -6 ::1
server 172.19.25.66 prefer
fudge 172.19.25.66 stratum 10
修改完配置文件后,要先执行ntpdate一下,显示了调整时间的信息。
ntpdate -u 172.19.25.66
------------------------------
17 Jun 15:20:26 ntpdate[5484]: adjust time server 172.19.25.66 offset 0.022266 sec
否则我遇到的是会显示一种重启中...
ntpstat
----------------
unsynchronised
time server re-starting
polling server every 8 s
然后启动NTP,过几分钟ntpstat会正常。
service ntpd start
ntpstat
---------------
synchronised to NTP server (172.19.25.66) at stratum 4
time correct to within 991 ms
polling server every 64 s
---------------
ntpq -p
-----------------
remote refid st t when poll reach delay offset jitter
==============================================================================
*172.19.25.66 120.25.115.19 3 u 59 64 17 0.688 -6.971 8.804
120.25.115.19 10.137.53.7 2 u 59 64 17 39.528 -7.119 6.037
至此,时间同步就成功了。
网友评论