CentOS 和Ubuntu 設定阿裡雲源指令碼
#!/bin/bash
function get_release_name()
{
if grep -Eq "CentOS" /etc/*-release;then
release_name=CentOS
echo "CentOS"
fi
if grep -Eq "Ubuntu" /etc/*-release;then
release_name=Ubuntu
echo "Ubuntu"
fi
}
function centos_set_aliyun_source()
{
centos_version=`cat /etc/*-release|grep -w "VERSION"|awk -F "=" '{print $2}'|awk '{print $1}'|awk -F '"' '{print $2}'`
echo "CentOS Version is $centos_version"
yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
if [ $centos_version == "5" ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
elif [ $centos_version == "6" ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
elif [ $centos_version == "7" ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
elif [ $centos_version == "8" ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
fi
yum clean all
yum makecache
}
function ubuntu_set_aliyun_source()
{
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
ubuntu_version=`cat /etc/*-release|grep "DISTRIB_RELEASE"|awk -F "=" '{print $2}'`
echo "Ubuntu Version is $ubuntu_version"
if [[ $ubuntu_version == "18.04" ]]||[[ $ubuntu_version == "19.04" ]];then
sudo bash -c "cat >/etc/apt/sources.list" <<EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
elif [ $ubuntu_version == "19.10" ];then
sudo bash -c "cat >/etc/apt/sources.list" <<EOF
deb http://mirrors.aliyun.com/ubuntu/ eoan main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ eoan main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ eoan-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ eoan-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ eoan-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ eoan-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ eoan-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ eoan-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ eoan-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ eoan-backports main restricted universe multiverse
EOF
elif [ $ubuntu_version == "20.04" ];then
sudo bash -c "cat >/etc/apt/sources.list" <<EOF
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF
fi
sudo apt-get update
}
function main()
{
get_release_name
if [ $release_name == "CentOS" ];then
centos_set_aliyun_source
fi
if [ $release_name == "Ubuntu" ];then
ubuntu_set_aliyun_source
fi
}
main