自己写的ntp-4.2.8p15离线升级脚本
适用于Centos7
注意:切勿直接在实际环境中运行,仅供参考!
升级所需的相关依赖包下载,请点击这里
解决内网环境yum安装依赖包问题,请参见这篇文章
#!/bin/bash
#Centos7升级update-ntp-4.2.8p15 2021-8-10
#①先将.tar.gz包和.sh文件上传至服务器(同一目录下)
#②chmod +x update-ntp-4.2.8p15.sh
#③./update-ntp-4.2.8p15.sh
DIR=$(pwd)
Date_start=$(date +%s)
#1.查看当前ntp版本
ntpdate -v
date;echo -e "\n\033[32m 请按回车键或等待3s后自动开始升级 \033[0m";read -t 3 -p "----------------------------------------";
#2.解压基础依赖包、ntp源码包
if [ ! -d "/opt/rpm" ];then
tar -zxvf Base-dependency-rpm-ntp.tar.gz -C /opt/
fi
tar -zxvf ntp-4.2.8p15.tar.gz
chown -R root.root ntp-4.2.8p15
#3.创建本地yum源
mv /etc/yum.repos.d{,.bak}
mkdir /etc/yum.repos.d/
echo -e '
[localyum]
name=localyum
baseurl=file:///opt/rpm
enabled=1
gpgcheck=0
' > /etc/yum.repos.d/localyum.repo
if rpm -q createrepo &> /dev/null;then
echo -e "\n\033[32m 系统已安装createrepo,继续升级中... \033[0m"
else
cd /opt/rpm/
yum -y localinstall deltarpm-3.6-3.el7.x86_64.rpm
yum -y localinstall python-deltarpm-3.6-3.el7.x86_64.rpm
yum -y localinstall libxml2-2.9.1-6.el7.5.x86_64.rpm
yum -y localinstall libxml2-python-2.9.1-6.el7.5.x86_64.rpm
yum -y localinstall createrepo-0.9.9-28.el7.noarch.rpm
fi
#4.检查本地yum源
createrepo /opt/rpm/ | grep 'Sqlite DBs complete' &> /dev/null
if [ $? == 0 ]; then echo -e "\n\033[32m 本地yum源创建成功 \033[0m";
else
echo -e "\n\033[31m 创建yum源失败 \033[0m"; exit 1;
fi
yum repolist
yum makecache
#5.安装基础依赖包
yum install -y gcc gcc-c++ openssl-devel libstdc++* libcap*
#6.备份
cp -ar /etc/ntp /etc/ntp.bak`date +%Y%m%d`
cp /etc/ntp.conf /etc/ntp.conf.bak`date +%Y%m%d`
cp /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak`date +%Y%m%d`
cp /etc/sysconfig/ntpdate /etc/sysconfig/ntpdate.bak`date +%Y%m%d`
#7.卸载旧版本ntp
yum remove -y ntp ntpdate
#8.编译安装ntp
cd $DIR/ntp-4.2.8p15/
mkdir /usr/share/doc/ntp-4.2.8p15
./configure --prefix=/usr --bindir=/usr/sbin --sysconfdir=/etc --enable-all-clocks --enable-parse-clocks --docdir=/usr/share/doc/ntp-4.2.8p15
make clean && make -j8 && make install
#9.配置ntp
ln -s /usr/local/ntp-4.2.8p15 /usr/local/ntp
echo -e '
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
restrict 192.168.215.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
' > /etc/ntp.conf
cp /usr/sbin/ntpd /etc/init.d/ntpd
#10.启动ntp
ntpd -c /etc/ntp.conf
#11.检查ntp是否升级成功
ntpd --version
Current_version=$(ntpd --version 2>&1)
Successful_version="ntpd 4.2.8p15"
if [[ $Current_version =~ $Successful_version ]]; then echo -e "\n\033[32m 升级成功 \033[0m";
else
echo -e "\n\033[31m 升级失败 \033[0m"; exit 1;
fi
#12.查看ntp进程
ps -ef | grep ntpd
#13.删除文件
cd $DIR
rm -rf $DIR/Base-dependency-rpm-ntp.tar.gz
rm -rf $DIR/ntp-4.2.8p15.tar.gz
rm -rf $DIR/ntp-4.2.8p15
rm -rf $DIR/update-ntp-4.2.8p15.sh
rm -rf opt/rpm
rm -rf /etc/yum.repos.d/
mv /etc/yum.repos.d.bak /etc/yum.repos.d
Date_end=$(date +%s)
Difference=$(( Date_end - Date_start ))
echo 用时:$Difference seconds.