时间同步的作用
- NTP (Network Time Protocol,网络时间协议)是用来使计算机时间同步的一种协议
- 它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒)
- 对于服务器群集来说,这个是一个很重要的服务,因为群集需要保证每个服务器的时间是保持一致的,这样它们在执行同一个任务时才不会出现有的服务器有滞后的现象,这样群集的状态才是健康的。
NTP的工作模式
- C/S模式
NTP的协议及端口号
- UDP协议123号端口
配置步骤
(1)配置环境
- 服务端:192.168.1.13
- 客户端1:192.168.1.14
- 客户端2:192.168.1.15
(2)全部关闭防火墙
[root@server4 ~]# systemctl stop firewalld
[root@server4 ~]# systemctl disable firewalld
[root@server4 ~]# setenforce 0
(3)全部安装 ntpdate 软件
[root@server4 ~]# yum -y install ntpdate
(4)服务器端同步 aliyun 时间
[root@server4 ~]# ntpdate ntp.aliyun.com
(5)服务器端为其他客户机提供服务需要安装 ntp 软件
[root@server4 ~]# yum -y install ntp
(6)修改 ntp 配置文件
[root@server4 ~]# vi /etc/ntp.conf
restrict default nomodify # 第8行
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 第17行,192.168.1.0 为本机上网网段
第 21 行到第 24 行删除,重新添加两行
fudge 127.127.1.0 stratum 10
server 127.127.1.0
(7)启动服务
[root@server4 ~]# systemctl start ntpd
[root@server4 ~]# netstat -anptu | grep ntpd
udp 0 0 192.168.1.13:123 0.0.0.0:* 78169/ntpd
(8)设置周期性计划任务
[root@server4 ~]# crontab -e
* * * * * /usr/sbin/update ntp.aliyun.com
(9)客户机设定同步源为 192.168.1.13
[root@server5 ~]# ntpdate 192.168.1.13
[root@server6 ~]# ntpdate 192.168.1.13
(10)客户机设置周期性计划任务
[root@server5 ~]# crontab -e
* * * * * /usr/sbin/update 192.168.1.13
[root@server6 ~]# crontab -e
* * * * * /usr/sbin/update 192.168.1.13