【Linux】CENTOS 7服务器chronyd同步本地时间服务器时间设置详解

本文主要介绍通过组件chronyd设置本地时间服务器作为时钟源服务端和客户端的配置

一、架构介绍

1、场景一

|----|--------|---------------------|-------------------------------|---------------------------------------------------|
| 序号 | 服务器名称 | 作用 | 网络 | 方式 |
| 1 | 外网服务器 | 同步网络时间 | 与内网服务器1通过端口通讯 | 通过脚本发送时间给内网服务器 |
| 2 | 内网服务器1 | 同步外网服务器时间并作为内网时间服务器 | 与外网服务器通过端口通讯,与内网服务器2可以相互ping通 | 1.通过脚本获取外网服务器时间 2.通过设置/etc/chrony.conf实现作为时间服务器功能 |
| 3 | 内网服务器2 | 同步内网服务器时间 | 与内网服务器1可以相互ping通 | 通过设置/etc/chrony.conf实现同步时间服务器时间 |

2、场景二

|----|-------|---------------------|------------------------|----------------------------------------|
| 序号 | 服务器名称 | 作用 | 网络 | 方式 |
| 1 | 外网服务器 | 同步网络时间 | 与内网服务器可以相互ping通 | 通过设置/etc/chrony.conf实现作为时间服务器功能和同步网络时间 |
| 2 | 内网服务器 | 同步外网服务器时间并作为内网时间服务器 | 与外网服务器可以相互ping通,但上不了外网 | 通过设置/etc/chrony.conf实现同步时间服务器时间 |

二、场景一实现

1、外网服务器发送时间脚本

外网服务器需要定时通过8877端口发送时间数据。

1.1 创建发送脚本:vi /usr/local/bin/send_time.sh

#!/bin/bash

在服务器B上运行

SERVER_IP="192.168.1.100" # 目标服务器A的IP

PORT=8877

获取当前时间并格式化

CURRENT_TIME=$(date "+%Y-%m-%d %H:%M:%S")

连接到远程服务器的8877端口

echo "CURRENT_TIME" \| nc SERVER_IP $PORT

echo "已发送时间 CURRENT_TIME 到 {SERVER_IP}:${PORT}"

1.2 设置脚本执行权限:chmod +x /usr/local/bin/send_time.sh

1.3 配置定时任务

crontab -e

* * * * * /usr/local/bin/send_time.sh >> /var/log/send_time.log 2>&1

1.4 查看日志验证发送情况:

tail -f /var/log/send_time.log

2、内网服务器1**创建监听脚本,**用于接收并设置时间

2.1 创建脚本 /usr/local/bin/listen_time.sh

vim /usr/local/bin/listen_time.sh

#!/bin/bash

在服务器A上运行(IP: 192.168.1.100)

PORT=8877

INTERFACE="0.0.0.0" # 监听所有网卡

echo "开始在 {INTERFACE}:{PORT} 监听时间同步请求..."

while true; do

明确指定监听所有IP的8877端口

TIME=(nc -l {INTERFACE} ${PORT})

if [[ "TIME" =\~ \^\[0-9\]{4}-\[0-9\]{2}-\[0-9\]{2}\\ \[0-9\]{2}:\[0-9\]{2}:\[0-9\]{2} ]]; then

echo "[(date)\] 接受到时间: TIME"

sudo date -s "$TIME"

else

echo "[(date)\] 错误: 无效时间格式 'TIME'"

fi

done

2.2 给脚本赋权:

chmod +x /usr/local/bin/listen_time.sh

2.3为脚本 配置systemd服务

vim /etc/systemd/system/listen_time.service

Unit

Description=Listen for Time Updates on Port 8877

After=network.target

Service

ExecStart=/usr/local/bin/listen_time.sh

Restart=always

Install

WantedBy=multi-user.target

2.4 配置开机自启动

--开机自启动

systemctl enable listen_time.service

---启动服务

systemctl start listen_time.service

2.5 测试内网服务器1的监听服务

在内网服务器1上查看日志,确认是否接收到时间

journalctl -u listen_time.service -f

3、配置内网服务器1为内网NTP服务器

3.1 vim /etc/chrony.conf

允许同步网络时间

allow 192.168.1.0/24

allow all #表示允许所有客户端来同步本机时间。

local stratum 10 ## 本机不同步任何主机的时间,本机作为时间源。(少了这一句,时钟源就配置有问题)

3.2 重启服务生效

systemctl restart chronyd

4、同步时间到内网服务器2

4.1 vim /etc/chrony.conf

server 192.168.1.2 iburst## 本机立即同步主机的时间,其中192.168.1.2 为外网服务器的IP 地址。

4.2 重启服务生效

systemctl restart chronyd

三、场景二实现

1、外网服务器配置,作为时间服务器,称为服务端

vim /etc/chrony.conf

允许同步网络时间

server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst

server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

allow 192.168.1.0/24

allow all #表示允许所有客户端来同步本机时间。

local stratum 10 ## 本机不同步任何主机的时间,本机作为时间源。(少了这一句,时钟源就配置有问题)

systemctl restart chronyd

2、内网服务器配置,同步时间服务器时间,称为客户端

vim /etc/chrony.conf

server 192.168.1.2 iburst## 本机立即同步主机的时间,其中192.168.1.2 为外网服务器的IP 地址。

3、修改配置后,服务端和客户端重启生效:

systemctl restart chronyd

4、在客户端上检查时间是否同步:

timedatectl

5、在客户端上检查时间源配置是否正确:

chronyc sources -v

相关推荐
`林中水滴`18 小时前
Linux Shell 命令:nohup、&、>、bg、fg、jobs 总结
linux·服务器·microsoft
appearappear18 小时前
阿里云同区域不同账户下两个服务器打通内网
服务器·阿里云·云计算
helloworddm18 小时前
防止应用多开-WPF
服务器·架构·c#
最后一个bug18 小时前
当linux触发panic后进行自定义收尾回调处理
linux·服务器·系统架构·bug
安当加密18 小时前
无网环境下的终端登录安全:一个被忽视的等保盲区
服务器·安全
石像鬼₧魂石19 小时前
服务器安全配置自查清单(可打印版)
运维·服务器·安全
微爱帮监所写信寄信19 小时前
微爱帮监狱寄信写信小程序信件内容实时保存技术方案
java·服务器·开发语言·前端·小程序
行初心19 小时前
uos基础 应用商店 查看应用程序的包名
运维
一只旭宝19 小时前
Linux专题十二:mysql数据库以及redis数据库
linux·数据库·mysql
赵民勇19 小时前
paste命令用法详解
linux·shell