1.修改机器本地时间为标准时区时间
# 操作系统版本检查
cat /etc/redhat-release
# 操作系统默认语言检查
echo $LANG
echo 'export LANG=en_US.UTF-8' >> ~/.bashrc
# 操作系统时区设置
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2.安装ntp
# 检查npt安装版本
yum list installed | grep ntp
# 安装ntp(所有同步机器都要安装)
yum -y install ntp
# 非时间服务器(主)都安装ntpdate服务
yum install -y ntpdate
3.配置ntp时间同步
3.1配置ntp服务端(主)
#####
# 注释掉
#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 127.127.1.0
Fudge 127.127.1.0 stratum 10
#####
3.2配置npt客户端,除了服务端都要配置
#####
# 注释掉
#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.26.21
Fudge 192.168.26.21 stratum 10
#####
4.启动ntp
# 启动ntp
service ntpd start
# 设置开机启动
systemctl enable ntpd.service
# 查看状态
systemctl status ntpd.service
5.检查ntp同步状态
# 查看同步的是哪台服务器
ntpq -p
异常处理:localhost: timed out, nothing received
***Request timed out
问题:因为开启了Ipv6 ,默认ntpq 先走Ipv6的通道,而ECS linux 默认无法直接访问ipv6地址,因此会访问超时
修改配置:永久关闭ipv6
# 重启生效
service ntpd restart
##### 添加如下参数
# 禁用整个系统所有接口的IPv6
net.ipv6.conf.all.disable_ipv6 = 1
# 禁用某一个指定接口的IPv6(例如:eth0, eth1)
# net.ipv6.conf.eth1.disable_ipv6 = 1
# net.ipv6.conf.eth0.disable_ipv6 = 1
#####
6.时间差:ntpstat
异常1处理:inappropriate address 192.168.26.21 for the fudge command, line ignored
修改配置:vi /etc/ntp.conf
将fudge 192.168.26.21 stratum 10 改成 Fudge 192.168.26.21 stratum 10
7.同步时间
ntpdate bigdata01
8.定时同步时间
通过crontab编写定时任务
crontab -e
# 添加如下内容
29,59 * * * * /usr/sbin/ntpdate bigdata01 # 每小时的第29分和59分同步一次时间
到达时间点后通过date命令查看时间是否已经同步,当然配置间隔时间更短的任务,这样可以更快的看到效果。
参数hwclock -w
*/10 * * * * /usr/sbin/ntpdate 11.125.0.3;hwclock -w
hwclock -w
命令的参数-w
代表“同步”,其全称是--sync
。这个参数的作用是将系统时间(由/usr/sbin/ntpdate
设置的)同步到硬件时钟。简而言之,hwclock -w
将内存中的系统时间写入到硬件时钟中,确保即使在系统关闭后,硬件时钟也能保持正确的时间。