文章目录
- @[toc]
- 一、、配置chronyd
- 1)时区设置为本地时区
- 2)配置chrony服务端
- 3)配置chronyd客户端
- 二、chronyd常用命令
- 1)chronyd常用命令说明
- 2)timedatectl说明
- 3)设置时间
文章目录
- @[toc]
- 一、、配置chronyd
- 1)时区设置为本地时区
- 2)配置chrony服务端
- 3)配置chronyd客户端
- 二、chronyd常用命令
- 1)chronyd常用命令说明
- 2)timedatectl说明
- 3)设置时间
一、、配置chronyd
Centos7默认使用Chrony工具而非NTP进行时间同步,Chrony和NTP两者有冲突只能选其一,CDH6推荐使用Chrony。
chrony-2.2.1.tar.gz 下载地址:https://download.tuxfamily.org/chrony/
1)时区设置为本地时区
#停止ntp
ansible all -m raw -a"systemctl stop ntpdate"
ansible all -m raw -a"systemctl disable ntpdate"#硬件时钟设置为UTC
ansible all -m raw -a"timedatectl set-local-rtc 0"#设置本地时区,显示本地时间
ansible all -m raw -a"timedatectl set-timezone Asia/Shanghai"#手动加载RTC设置
ansible all -m raw -a"hwclock --systohc"#查看时间
ansible all -m raw -a"timedatectl"
#Firewalld设置
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload#iptables 防火墙设置
iptables -I INPUT -p udp --dport 123 -j ACCEPT
iptables -I INPUT -p udp --dport 223 -j ACCEPT
service iptables save
2)配置chrony服务端
#安装
ansible all -m raw -a"yum -y install chrony"##设置开机启动,并同时启动
ansible all -m raw -a"systemctl enable chronyd.service --now"#修改服务器配置⽂件
cp /etc/chrony.conf /etc/chrony.$(date +'%s')
cat > /etc/chrony.conf <<EOF
#编辑该行,设置ntp服务器地址,pool.ntp.org 替换为ntp服务器地址,注:保留1个即可,其他NTP服务器地址注销
pool pool.ntp.org iburst
#server ntp.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
#允许指定网络的主机同步时间,不指定就是允许所有,默认不开启
allow 0.0.0.0/0
#当外部时间服务器不可用时,采用本地时间作为同步标准
local stratum 10
logdir /var/log/chrony
EOF#看正确时间,进行修改
date -s 09:57:00
#同步到硬件时间
clock -w
#启动chronyd
systemctl restart chronyd
systemctl enable chronyd
3)配置chronyd客户端
#安装
yum -y install chrony# 修改服务器配置文件,以阿里云时间服务器为上游服务器
cp /etc/chrony.conf /etc/chrony.$(date +'%s')
cat > /etc/chrony.conf <<EOF
server 192.168.6.61 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF# 启动 chronyd服务
systemctl restart chronyd
systemctl enable chronyd#重启后等几分钟,让系统识别到时间服务器,期间可多次执行chronyc sources或chronyc sources -v查询状态。
[root@localhost yum.repos.d]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* h1 3 6 377 4 +874us[+1148us] +/- 49ms#查看当前系统时区
[root@localhost ~]# timedatectlLocal time: Mon 2023-12-11 10:05:51 CSTUniversal time: Mon 2023-12-11 02:05:51 UTCRTC time: Mon 2023-12-11 02:05:51Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes #ntp是否启用
NTP synchronized: yes #是否完成过一次ntp同步RTC in local TZ: no #是否启用rtc时间【我们为cst+8,所以这为no才是正常的】DST active: n/a
二、chronyd常用命令
1)chronyd常用命令说明
- 查看时间同步源状态: chronyc sourcestats -v 【-v是列出详细,可以不要】
- 立即校准时间服务器:chronyc tracking 【配置的时候默认是自动校准的,一般用于服务端修改时间了,客户端立即校准用】
- 启用NTP时间同步: timedatectl set-ntp yes【默认是启用的,timedatectl命令可以查看状态】
- 查看客户端连接情况【服务端执行】:chronyc clients
- 列出ntp状态详细:timedatectl
2)timedatectl说明
- 设置硬件时间
- 硬件时间默认为UTC: timedatectl set-local-rtc 1/0
- 设置硬件时钟为本地时间:timedatectl set-local-rtc 1
- 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
- 启用NTP时间同步: timedatectl set-ntp yes
- 开启 NTP:timedatectl set-ntp true/flase
- 校准时间服务器: chronyc tracking
3)设置时间
- 设置时间:timedatectl set-time HH:MM:SS
- 设置日期:timedatectl set-time YYYY-MM-DD
- 设置日期时间:timedatectl set-time “YYYY-MM-DD HH:MM:SS”【年月日时分秒】
- 修改报错执行:timedatectl set-ntp no
- 修改时间后执行:clock -w【写入硬件时间,一般2个时间同步修改】
【实例】
[root@ccx_server128 ~]# date
2022年 03月 01日 星期二 01:10:04 CST
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:10:34"
Failed to set time: Automatic time synchronization is enabled
[root@ccx_server128 ~]# timedatectl Local time: 二 2022-03-01 01:13:18 CSTUniversal time: 一 2022-02-28 17:13:18 UTCRTC time: 一 2022-02-28 17:13:18Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes
NTP synchronized: noRTC in local TZ: noDST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp no
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:13:55"
[root@ccx_server128 ~]# date
2022年 02月 28日 星期一 17:13:57 CST
[root@ccx_server128 ~]# timedatectl Local time: 一 2022-02-28 17:14:02 CSTUniversal time: 一 2022-02-28 09:14:02 UTCRTC time: 一 2022-02-28 09:14:02Time zone: Asia/Shanghai (CST, +0800)NTP enabled: no
NTP synchronized: noRTC in local TZ: noDST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp yes
[root@ccx_server128 ~]# # 然后我去客户端立即获取。。。多香啊
[root@ccx_server129 ~]# date
Tue Mar 1 01:15:13 CST 2022
[root@ccx_server129 ~]#
[root@ccx_server129 ~]# ntpdate 192.168.59.128
28 Feb 17:15:37 ntpdate[46458]: step time server 192.168.59.128 offset -28789.784156 sec
[root@ccx_server129 ~]# date
Mon Feb 28 17:15:39 CST 2022
【可参考】
- https://cuichongxin.blog.csdn.net/?type=blog