1.搭建ntp服务器,并查询状态
2.搭建ntp客户端,从上面的ntp服务器上同步时间
3.搭建ntp客户端,并设置使用key认证登录
安装软件:[root@localhost ~]# yum install ntp -y
重启服务[root@localhost ~]# systemctl restart ntpd
可使用如下命令来查看NTP服务器是否已经顺利更新了自己的时间
[root@localhost ~]# ntpstat 该命令可列出我们的NTP服务器是否已经与上层连接。
[root@localhost ~]# ntpstat
unsynchronised
polling server every 64 s
等一会儿再查看
[root@localhost ~]# ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
time correct to within 111 ms
polling server every 128 s
NTP服务器已经成功与上层建立连接。时间已经校正约111ms,每隔128s会主动去更新时间。
服务端(192.168.206.141):
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/ntp.conf
restrict 192.168.206.0 mask 255.255.255.0 限制当前网段的主机可以同步时间
server 127.127.1.0 设置上层NTP服务器为自己
fudge 127.127.1.0 stratum 8 虚拟自己在第8层
[root@localhost ~]# systemctl restart ntpd
客户端(192.168.206.138):
[root@localhost ~]# ntpdate -q 192.168.206.141 查看上层服务器的状态
server 192.168.206.141, stratum 9, offset -0.002161, delay 0.02641
29 Mar 19:02:53 ntpdate[43576]: adjust time server 192.168.206.141 offset -0.002161 sec
[root@localhost ~]# ntpdate 192.168.206.141 更新时间
29 Mar 19:02:08 ntpdate[43567]: step time server 192.168.206.141 offset 32331.572864 sec
使用计划任务自动更新客户端的时间
编辑/etc/crontab文件
-
-
-
-
- root /usr/sbin/ntpdate 192.168.206.141 > /dev/null 2>&1
-
-
-
标准输出(stdout):代码为1,使用>或>>;
标准错误输出(stderr):代码为2,使用2>或2>>。
2>&1 意思是把标准错误输出重定向到标准输出