时间同步是利用ntp(network time protocol)协议时网络中的各个计算机时间达成同步。
一、ntp服务器
1. 在集群所有机器安装ntp
yum install ntp
2. 在server节点启动ntp
systemctl start ntpd
systemctl enable ntpd
3. 修改server配置文件
server 127.127.1.0 #设置自身为ntp服务器,没有外网可以禁止掉其他server字段
restrict x.x.x.x mask 255.255.255.0 #把自身网段加进去
4.修改client配置文件
server x.x.x.x #添加server地址,没有外网可以禁止掉其他server字段
5.重启所有ntp服务
systemctl restart ntpd
6.client节点手动同步时间
ntpdate -u x.x.x.x
二、chrony
chrony是一个实现了ntp协议的开源软件。chrony可以更快的同步,从而最大程度减少时间和频率误差;能够更好地响应时钟频率的快速变化。
chrony主要包含chronyd和chronyc两个程序。chronyd是后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步;chronyc是命令行用户工具,用于监控或配置参数,可以控制本机的chronyd实例也可以控制远程的chronyd实例。
1.安装chrony
yum install chrony
2.配置
vi /etc/chrony.conf
Server xxx.xxx.xxx.xxx iburst #如果server配置本机ip代表与本机同步,可以作为server被其他客户端同步
driftfile /var/lib/chrony/drift #根据实际时间计算出增减时间的比率,重启后为系统时钟作出补偿
makestep 1.0 3 #如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进
rtcsync #启用实时时钟(RTC)的内核同步
logdir /var/log/chrony
3.启动服务
systemctl enable chronyd
systemctl start chronyd
三、常用命令
1.date
print or set the system date and time
date #显示时间
date -s ‘yyyy-mm-dd hh:mm:ss’ #设置时间
date +%s #unix时间戳
2.timedatectl
Control the system time and date (centos7之后才出现)
timedatectl list-timezone #列出时区
timedatectl set-timezone Asia/Shanghai #设置时区
timedatectl set-time HH:MM:SS #设置时间
timedatectl set-ntp yes #set使用-ntp参数启用或禁用系统时钟与远程NTP服务器的自动同步,yes是开启