chrony实现局域网时间同步
chrony基于cs架构实现,c与s的区别在于chrony.conf中的allow选项:
打开allow选项表示允许其他主机从这台主机同步,即作为s
默认为关闭即表示不允许其他主机从这台主机同步,即为c
服务端
timedatectl set-timezone Asia/Shanghai --
时区修改为shanghai
yum install chrony --RHEL7默认已安装chrony,而没有安装ntpd. systemctl status chronyd --查看chronyd服务状态 systemctl enable chronyd --开机启动 vi /etc/chrony.conf --修改配置文件 #server 0.rhel.pool.ntp.org iburst --注释这4行,由于是内网环境,所以无法跟外部时间服务器进行时间同步。 #server 1.rhel.pool.ntp.org iburst #server 2.rhel.pool.ntp.org iburst #server 3.rhel.pool.ntp.org iburst server 192.168.100.1 iburst --添加这一行,表示与本机同步时间
#server cn.pool.ntp.org iburst --国内也常用这俩时间同步服务器
#server tw.pool.ntp.org iburst
# Allow NTP client access from local network. allow 192.168.100.0/24 --允许哪些服务器到这台服务器来同步时间
客户端
#server 0.rhel.pool.ntp.org iburst --注释这4行 #server 1.rhel.pool.ntp.org iburst #server 2.rhel.pool.ntp.org iburst #server 3.rhel.pool.ntp.org iburst server 192.168.100.1 iburst ---添加该行,表示到这台服务器去同步时间。
重启chrony服务,并开机自启
[root@rhel2 ~]# systemctl restart chronyd.service
[root@rhel2 ~]# systemctl enable chronyd.service
常用命令
1 | cat /etc/chrony .conf | grep - v ^ # | grep -v ^$ 查看配置文件(去掉注释行和空行) |
1 2 3 4 5 6 | 查看时间同步源: $ chronyc sources - v 查看时间同步源状态: $ chronyc sourcestats - v 校准时间服务器: $ chronyc tracking |
其他
查看时间同步
[root@rhel2 ~]# timedatectl Local time: Sat 2017-10-07 11:15:43 CST Universal time: Sat 2017-10-07 03:15:43 UTC RTC time: Sat 2017-10-07 03:02:26 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: yes --为yes表示已同步 RTC in local TZ: no DST active: n/a
手动修改时间时,必须把NTP enabled 设置为no.
[root@rhel1 ~]# date Sun Oct 8 11:00:51 CST 2017 [root@rhel1 ~]# timedatectl Local time: Sat 2017-10-07 11:31:45 CST Universal time: Sat 2017-10-07 03:31:45 UTC RTC time: Sun 2017-10-08 03:01:57 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a
如果把NTP enabled 修改为yes时,表示开启自动同步时间,此时,是不能手动修改时间的。
如果为No时,表示关闭自动同步时间。
[root@rhel1 ~]# timedatectl set-ntp yes [root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50" Failed to set time: Automatic time synchronization is enabled [root@rhel1 ~]# timedatectl set-ntp no [root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50" [root@rhel1 ~]# date Sun Oct 8 11:00:51 CST 2017
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人