Linux时钟同步(一)

Server:
1.安装ntp和ntpdate
2.修改/etc/ntp.conf文件,注释掉所有服务器,添加同步的目标主机ip(192.168.101.33)。
server 127.127.1.0
fudge 127.127.1.0 stratum 10
3.重启ntp服务:service ntp restart

Client:
4.安装ntp和ntpdate
5.修改/etc/ntp.conf文件,注释掉所有服务器,添加同步的目标主机ip(192.168.6.10)。
server 192.168.101.33
fudge 192.168.101.33 stratum 10
6.关闭ntp服务:service ntp stop
7.同步时间:ntpdate 192.168.101.33(注意,server重启后5分钟才可以同步成功)

Linux时钟同步(一)

补充:
service ntpd status #查看ntpd服务状态
service ntpd start #启动ntpd服务
service ntpd stop #停止ntpd服务
service ntpd restart #重启ntpd服务

1、检查ntp服务是否开机启动,将其设置为开机启动。

[[email protected] ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] ~]# runlevel
N3
[[email protected] ~]# chkconfig ntpd on
在运行级别2、3、4、5上设置为自动运行
[[email protected] ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[[email protected] ~]#
如果要设置在运行级别上自动运行,可以使用下面命令
chkconfig --level 345 ntpd on

2、可以用下面命令检测NTP服务是否运行

[[email protected] ~]# pgrep ntpd
2639
2641
[[email protected] ~]#netstat -tlunp | grep ntp #如果看到123端口,说明ntp服务成功启动。udp 0 0192.168.7.224:123 0.0.0.0:* 2639/ntpd
udp 0 0127.0.0.1:123 0.0.0.0:* 2639/ntpd
udp 0 00.0.0.0:123 0.0.0.0:* 2639/ntpd
udp 0 0fe80::250:56ff:feb3:b5:123 ::???? 2639/ntpd
udp 0 0 ::1:123 ::???? 2639/ntpd
udp 0 0 :::123 ::???? 2639/ntpd

3、查看ntp服务器有无和上层ntp连通
[[email protected] ~]# ntpstatsynchronised to NTP server (192.168.7.49) at stratum 6
time correct to within 440 ms polling server every 128 s
4、查看ntp服务器与上层ntp的状态
[[email protected] ~]# ntpq -p remote
Linux时钟同步(一)
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差
注意:NTP服务端重启后,客户机要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。
等待的时间可以通过命令 watch ntpq -p来监控。

5、要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。
Linux时钟同步(一)
第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。
remote 列出源的 IP 地址或主机名。
when 指出从轮询源开始已过去的时间(秒)。
poll 指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
reach 是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
offset 是源时钟与本地时钟的时间差(毫秒)。