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/15
# Desc:
#     sysmtem: CentOS Linux release 7.9.2009 (Core)
#     kernel:5.4.157-1.el7.elrepo.x86_64


# environment
# Local variables
_PATH=/root/pkg/images
#_FILEPATH=https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/Packages/chrony-3.4-1.el7.x86_64.rpm
_FILEPATH=${_PATH}/{libseccomp-2.3.1-4.el7.x86_64.rpm,chrony-3.4-1.el7.x86_64.rpm}

# Turn on this variable when downloading from the Internet
#_FILECHRONY=https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/Packages/chrony-3.4-1.el7.x86_64.rpm

# 7.9 When the mini environment is installed, it prompts that a dependent package is required, and it must be installed here
_FILEDEPEND=https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/Packages/libseccomp-2.3.1-4.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 "\"${_FILECHRONY} or ${_FILEDEPEND}\" is not found."
    echo "Please check environment \"_FILECHRONY\""
}

# Server端
chronyserver(){
	# download chrony-3.4-1.el7.x86_64.rpm
	rpm -ivh ${_FILEDEPEND} ${_FILECHRONY} > /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 --now chronyd
		loop
	fi
}

# Client端
chronyclient(){
	# download chrony-3.4-1.el7.x86_64.rpm
	rpm -ivh ${_FILEDEPEND} ${_FILECHRONY} > /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 --now chronyd
		loop
	fi
}

uninstall(){	
	rpm -e chrony libseccomp
}

# 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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值