很多应用服务需要时间同步,这时候可以设置一台ntp服务器,其他客户端连接这台服务器就行。
服务端A:192.168.1.9 客户端:192.168.1.7
1、安装ntp服务(操作机器AB)
[root@bvm ~]# yum install ntp -y
[root@node1 ~]# systemctl start ntpd
[root@node1 ~]# systemctl enable ntpd
2、服务端A修改配置文件/etc/ntp.cnf(操作机器A)
添加
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #允许内网其他机器同步时间
3、客户端:192.168.1.7修改配置文件/etc/ntp.cnf(操作机器B)
注释掉默认的server 0-3
添加
server 192.168.1.7
restrict 192.168.1.7 nomodify notrap noquery
fudge 127.127.1.0 stratum 8
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.1.7
restrict 192.168.1.7 nomodify notrap noquery
fudge 127.127.1.0 stratum 8
查看网络中可用的ntp服务器
[root@bvm ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.1.7 5.103.139.163 2 u 34 64 377 0.194 15.762 17.033
4、客户端同步(操作机器B)
①手动ntpdate同步:ntpdate -u 192.168.1.7
然后crontab -e 设置定时即可实现自动同步
[root@bvm ~]# ntpdate -u 192.168.1.7
28 Dec 20:27:06 ntpdate[7170]: adjust time server 192.168.1.7 offset 0.027655 sec
如果使用ntpdate 192.168.1.7会报错,因为客户端也开启了ntpd服务,需要关掉ntpd,systemctl stop ntpd。
[root@bvm ~]# ntpdate 192.168.1.7
28 Dec 20:27:16 ntpdate[7171]: the NTP socket is in use, exiting
②自动同步:ntpstat
[root@bvm ~]# ntpstat
unsynchronised
polling server every 64 s
显示unsynchronised,需要等待15分钟左右
[root@bvm ~]# ntpstat
synchronised to NTP server (192.168.1.7) at stratum 3
time correct to within 208 ms
polling server every 64 s