Linux基础14-NTP网络时间协议、Rsync+inotify实时同步
NTP网络时间协议
NTP(Network Time Protocol)网络时间协议基于UDP,用于网络时间同步的协议,使网络中的计算机时钟同步到UTC(世界统一时间),再配合各个时区的偏移调整就能实现精准同步对时功能。
chrony是网络时间协议NTP的实现方式, Chrony是一个开源的自由软件 ,在RHEL7/CentOS7操作系统已经是默认安装服务,它能保持系统时间与NTP时间服务器的时间保持同步,让时间始终保持同步。
软件包:chrony(默认安装)
服务名:chronyd
命令程序:chronyc
配置文件:/etc/chrony.conf
配置文件介绍:
vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#配置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
# Record the rate at which the system clock gains/losses time.
#记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 默认情况下,chronyd通过减慢或加快时钟速度来逐渐调整时钟。如果时钟与实际时间偏差太大,则需要很长时间才能纠正错误。这种方法叫做步进时钟(时间跳变)。
# 此处表示如果调整值大于1000秒,则这将使系统时钟步进,但仅在前10个时钟更新中。
makestep 1000 10
# Enable kernel synchronization of the real-time clock (RTC).
# 启用RTC(实时时钟)的内核同步
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
# 只允许192.168.网段的客户端进行时间同步
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
# NTP服务器不可用时,采用本地时间作为同步标准
#local stratum 10
# Specify file containing keys for NTP authentication.
# 指定包含NTP验证密钥的文件
#keyfile /etc/chrony.keys
# Specify directory for log files.
# 指定日志文件的目录
logdir /var/log/chrony
# Select which information is logged.
#将对系统增益或损耗率的估计值以及所做的任何转换记录的更改记录到名为的文件中tracking.log
#log measurements statistics tracking
常用命令:
查看时间同步源:chronyc sources -n
立刻手动同步时间:chronyc -a makestep
检查时间是否同步:chronyc tracking
案例需求:搭配一台NTP服务器,同步阿里云时间,当阿里云时间不可用时,使用本地时间,其他服务器同步这台服务器的时间,确保时间一致
修改服务端配置文件:
vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 注释其他server开头的配置,添加阿里云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
#添加阿里NTP服务
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
#允许所有网段客户端进行时间同步
allow 0.0.0.0/0
#NTP服务器不可用时,采用本地时间作为同步标准
local stratum 10
修改客户端配置文件:
vim /etc/chrony.conf
# 注释其他server开头的配置,添加本地NTP公共时间同步服务器
server 192.168.0.24 iburst
rsync远程同步
可实现本地或远程主机数据同步与增量备份的工具
软件安装:源码安装、二进制RPM安装
本地同步语法:
命令格式1:rsync 选项 本地目录A 本地目录B #同步整个目录
命令格式2:rsync 选项 本地目录A/ 本地目录B #只同步目录下的数据
常用选项:
-n #测试同步过程,不做实际修改
--delete #删除目标目录下多余的文档
-v #显示详细操作信息
-z #传输过程中启用压缩/解压缩
-a #递归同步。包含目录/子目录及所有文件、保留符号链接文件、保留文件权限、时间标记、保留文件的属主/属组标记、保留设备文件及其他特殊文件
远程同步语法:
命令格式1:rsync 本地目录 用户名@对方IP地址:/目标目录 #同步源目录本身
命令格式2:rsync 本地目录/ 用户名@对方IP地址:/目标目录 #同步源目录下内容
rsnyc+inotify实现同步
inotify是Linux内核用于通知用户空间的程序文件系统变化的机制,如文件增加、删除等事件可以立刻让用户得知
inotify使用方式
命令格式:inotify 选项 目标文件
常用选项
-m #持续监控,捕获一个事件后不退出
-r #递归监控,包括子目录
-q #减少屏幕输出信息
-e #指定监控项:modify(修改)、move(移动)、create(创建)、delete(删除)、attrib(权限)
实时监控脚本
实现从本地到远程的web目录推送,源目标:/var/www/html
#!/bin/bash
FROM_DIR="/var/www/html"
RSNYC_CMD="rsnyc -za --delete $FROM_DIR root@IP:/var/www/html"
while inotify -rqq -e modify,move,create,delete,attrib $FROM_DIR #q不显示输出
do
$RSNYC_CMD
done &
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律