Centos环境中搭建chrony时间服务器
该脚本分为四部分:
- 安装chrony,并注释掉配置文件中原来的时间同步地址
- 帮助手册,指导使用脚本
- 配置服务端,修改时间地址为阿里云时间服务器,开放IP段,然后启动并设为开机自启
- 配置客户端,设置时间同步为服务端,然后启动并设为开机自启
#!/usr/bin/bash
# Author: lemon
# time: 2021.3.16
# 安装chrony服务,修改配置
install(){
yum -y install chrony &>/dev/null
sed -i 's/^server/#server/g' /etc/chrony.conf
}
# 帮助手册
helps(){
echo "############################################################"
echo "# 该脚本用于安装chronyc时间服务器,分别安装服务端和客户端 #"
echo "# 执行参数为: [sh chronyc.sh service||client] #"
echo "# service参数安装服务端client安装客户端使用时注意修改IP #"
echo "############################################################"
}
case $1 in
service)
install
cat >>/etc/chrony.conf<<EOF
# Start custom config
# add time server address
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server time3.aliyun.com iburst
server time4.aliyun.com iburst
server time5.aliyun.com iburst
server time6.aliyun.com iburst
server time7.aliyun.com iburst
# Allow NTP client access from local network
allow 192.168.144.0/16
# End custom config
EOF
# 启动服务
systemctl start chronyd && systemctl enable chronyd
chronyc sources
;;
client)
install
cat >>/etc/chrony.conf<<EOF
# Start custom config
# add time server address
server 192.168.144.10 iburst
EOF
# 启动服务
systemctl start chronyd && systemctl enable chronyd
chronyc sources
;;
*)
helps
;;
esac