自动初始化centos7 的脚本

[1].自动修改并切换DNS地址
[2].自动切换安装新的YUM源和EPAL源
[3].自动同步当前服务器的时间(NTP)
[4].自动升级系统中已经安装的软件包

wget https://mirrors.jshainei.com/smb/codesrc/shell/centos7_auto.sh && bash centos7_auto.sh

脚本源代码


# 定义外网镜像站的地址
MirrorHost="mirrors.jshainei.com/smb/autoInstall"


# 自动设置 YUM 源
# 支持1.清华源 2.阿里源 3.腾讯源 4.华为源
function AutoConfigYum(){

	echo "系统将自动设置 YUM 的Base 源 和 EPAL 源,您可在下列镜像源中选择,默认清华源"
	echo "[1]. 清华大学开源镜像(OpenTUNA)   https://opentuna.cn/"
	echo "[2]. 阿里巴巴开源镜像站           https://mirrors.aliyun.com/"
	echo "[3]. 腾讯软件源                   https://mirrors.cloud.tencent.com/"
	echo "[4]. 华为开源镜像站               https://mirrors.huaweicloud.com/"
	echo -n "请输入欲设置的YUM源的标号:" 
	read Sid
	case "$Sid" in 
	"1") MirrorSource="tsinghua";;
	"2") MirrorSource="aliyun";;
	"3") MirrorSource="qcloud";;
	"4") MirrorSource="huawei";;
	"*") MirrorSource="tsinghua";;
	esac
	
	# 从远端下载对应系统版本的repo 文件,并更新yum 的配置
	DownloadUrl="https://${MirrorHost}/mirrors/repo/${MirrorSource}/Centos-7.repo"
	cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
	curl -o /etc/yum.repos.d/CentOS-Base.repo $DownloadUrl
	
	# 删除腾讯云可能存在的centos-epel源的配置文件
	if [ ! -f "/etc/yum.repos.d/CentOS-Epel.repo" ]; then
		rm -rf CentOS-Epel.repo
	fi
	yum install -y "https://${MirrorHost}/mirrors/epel/epel-release-latest-7.noarch.rpm"
	
	# 更新EPAL 源的配置信息
	if [[ $MirrorSource == "aliyun" ]]
	then
		sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
		sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
		sed -i "s/pgcheck=1/pgcheck=0/g" /etc/yum.repos.d/epel.repo
		sed -i "s@https\?://download.fedoraproject.org/pub@https://mirrors.aliyun.com@g" /etc/yum.repos.d/epel.repo
		
	elif [[ $MirrorSource == "tencent" ]]
	then
	sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
	sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
	sed -i "s/pgcheck=1/pgcheck=0/g" /etc/yum.repos.d/epel.repo
	sed -i "s@https\?://download.fedoraproject.org/pub@https://mirrors.cloud.tencent.com@g" /etc/yum.repos.d/epel.repo
	
	elif [[ $MirrorSource == "huawei" ]]
	then
	sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
	sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
	sed -i "s/pgcheck=1/pgcheck=0/g" /etc/yum.repos.d/epel.repo
	sed -i "s@https\?://download.fedoraproject.org/pub@https://repo.huaweicloud.com@g" /etc/yum.repos.d/epel.repo
	
	else
	sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
	sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
	sed -i "s/pgcheck=1/pgcheck=0/g" /etc/yum.repos.d/epel.repo
	sed -i "s@https\?://download.fedoraproject.org/pub@https://opentuna.cn@g" /etc/yum.repos.d/epel.repo
	fi
	
	cp /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 /etc/pki/rpm-gpg/RPM-GPG-KEY-7

	yum clean all
	yum makecache
	echo "======================================="
	echo "|YUM 源、EPAL源已经更新完成           "
	echo "|镜像源:${MirrorSource}               "
	echo "======================================="
}

# 设置当前的服务器的DNS 地址
function Config_DNS(){
	rm -rf /etc/resolv.conf
	echo -e "nameserver 223.5.5.5\nnameserver 119.29.29.29" >> /etc/resolv.conf
	echo "======================================="
	echo "|DNS服务器地址已经更改                "
	echo "|主DNS地址:阿里云 223.5.5.5           "
	echo "|副DNS地址:腾讯云 119.29.29.29        "
	echo "======================================="
}

# 同步当前服务器的时间信息
function Config_SeverTime(){
	yum install ntp -y
	wget -O /etc/ntp.conf "https://${MirrorHost}/source/ntp.conf"
	service ntpd stop
	ntpdate ntp.aliyun.com
	echo "======================================="
	echo "|同步服务器时间成功                    "
	echo "|NTP服务器地址: ntp.aliyun.com         "
	echo "======================================="
}


echo "==================================="
echo "Centos7 快速初始化脚本             "
echo "将自动执行下列操作                 "
echo "[1].自动修改并切换DNS地址          "
echo "[2].自动切换安装新的YUM源和EPAL源  "
echo "[3].自动同步当前服务器的时间(NTP)  "
echo "[4].自动升级系统中已经安装的软件包 "
echo "==================================="
echo -n "输入Y确定执行:" 
read choose
	if [ "$choose" == "Y" -o "$choose" == 'y' ];then
		echo "[1].自动修改并切换DNS地址"
		sleep 2
		Config_DNS
		clear
		echo "[2].自动切换安装新的YUM源和EPAL源"
		sleep 2
		AutoConfigYum
		clear
		echo "[3].自动同步当前服务器的时间(NTP)"
		sleep 2
		Config_SeverTime
		clear
		echo "[4].自动升级系统中已经安装的软件包"
		sleep 2
		yum update -y
		clear
		echo '运行结束,感谢使用!'
	else
		echo "用户终止了运行!"
	fi
	
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注