栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

chrony局域网时间同步

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

chrony局域网时间同步

系统版本:CentOS Linux release 7.9.2009 (Core)
系统内核:5.4.157-1.el7.elrepo.x86_64
Bash:GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

脚本说明:
_FILEPATH 环境变量改成本地路径也可以执行,本脚本用的是清华源,但是只限于RPM包。

#!/bin/sh
# Author:
# Date: 2021/11/8
# Desc:
#     sysmtem: CentOS Linux release 7.9.2009 (Core)
#     kernel:5.4.157-1.el7.elrepo.x86_64


# environment
_FILEPATH=https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/Packages/chrony-3.4-1.el7.x86_64.rpm

# Determine whether the synchronization is complete
loop(){
	echo -n "Please Waiting"
	while true; do
		_para=`timedatectl | grep "NTP synchronized" | awk '{print $3}'`
		if [ ${_para} == 'yes' ]; then
			echo
			timedatectl
			exit 0
		fi
		echo -n "."
		sleep 1
	done
}
errors(){
    echo ""${_FILEPATH}" is not found."
    echo "Please check environment "_FILEPATH""
}

# Server端
chronyserver(){
	# download chrony-3.4-1.el7.x86_64.rpm
	rpm -ivh ${_FILEPATH} > /dev/null 2>&1
	if [[ `echo $?` -eq 0 ]]; then
		cp -a /etc/chrony.conf /etc/chrony.conf.`date +%s`
		cat > /etc/chrony.conf << EOF
#
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst
#
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.15.0/24
logdir /var/log/chrony
EOF
		systemctl enable chronyd && systemctl start chronyd
		loop
	fi
}

# Client端
chronyclient(){
	# download chrony-3.4-1.el7.x86_64.rpm
	rpm -ivh ${_FILEPATH} > /dev/null 2>&1
    if [[ `echo $?` -eq 0 ]]; then
		cp -a /etc/chrony.conf /etc/chrony.conf.`date +%s`
		cat > /etc/chrony.conf << EOF
#
server 192.168.15.7 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
		systemctl enable chronyd && systemctl start chronyd
		loop
	fi
}

uninstall(){	
	rpm -e chrony
}

# main
case $1 in 
	server)
	chronyserver
	errors
    ;;
    client)
    chronyclient
    errors
    ;;
    uninstall)
    uninstall
    ;;
    *)
    echo "EXP:
    chrony.sh [server/client/uninstall]"
    ;;
esac
# other
# 查看时间同步源
chronyc sources -v
# 查看时间同步源状态
chronyc sourcestats -v
# 校准时间服务器
chronyc tracking
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/450541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号