CentOS7 下配置多台服务器时间同步
现有三台虚拟机,如果虚拟机之间实间差距太大就会导致一些服务异常
1 | 192.168.139.121 master |
安装 NTP
先查看 ntp 是否已经安装:rpm -qa | grep ntp
如果没有 yum -y install ntp
安装完成后设置 ntp 开机启动并启动 ntp,如下:1
2
3systemctl enable ntpd
systemctl start ntpd
主服务器均能连接外网
所有机器都能够访问外网的话,我们可以使用 ntpdate 来进行时间同步
前提是我们服务器上面有 ntp ,如果没有的话可以使用 yum 安装
同步网络时间
其中 ntp 一种网络时间协议,ntpdate 是时间同步软件的命令, ntp.api.bz 是个稳定的 ntp 时间同步服务器集群。
1 | ntpdate -u ntp.api.bz |
编辑定时任务
当然你可以使用 linux 系统的定时任务来设置每隔多长时间同步一次,使用 linux 系统下使用 crontab -e 可以编辑任务。
这里我是用 crontab -e 添加了一个定时每分钟同步时间的任务 ,然后将执行日志写到了 /tmp/dateSync.log 下面
1 | crontab -e |
crontab 规则在线生成:https://www.bejson.com/othertools/cron/
NTP 服务器
我们把这台能连接外网的服务器当作时间服务器,整个集群服务器向该时间服务器同步时间,然后咱们内网的这台时间服务器向公网时间服务器同步时间,然后内网时间服务器不能联网,则自己设置时间,所有的集群服务器以它的时间为准。
配置 NTP 服务器
使用 master 作为 NTP 服务器
- 修改 NTP 配置文件
vim /etc/ntp.conf
1 | # For more information about this file, see the man pages |
- 重启 NTP 服务器
1 | systemctl restart ntpd |
配置 NTP 客户端
- 安装 NTP
1 | yum -y install ntp |
- 修改
/etc/ntp.conf
配置文件
注释以下内容新增以下内容1
2
3
4# 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 iburst1
2
3
4
5server 192.168.139.121
restrict 192.168.139.121 nomodify notrap noquery
server 127.127.1.0 # local clock
Fudge 127.127.1.0 stratum 10
修改后重启 ntpd 服务1
2
3
4systemctl restart ntpd
systemctl enable ntpd
# 检查是否启动成功
systemctl status ntpd
使用 ntpq -p
查看网络中的 NTP 服务器,同时显示客户端和每个服务器的关系
使用 ntpstat 命令查看时间同步状态,这个一般需要 5-10 分钟后才能成功连接和同步。所以,服务器启动后需要稍等下:
刚启动的时候,执行 ntpstat
一般会提示以下信息:1
2
3unsynchronised
time server re-starting
polling server every 64 s
注意
- 防火墙是否关闭
- 123 端口是否开发 (如果防火墙已关闭请忽略)
- 关闭 Selinux