阿里云云服务器Linux系统更新yum源Shell脚本
所有执行的脚本都需要root身份来执行,执行方法:以root身份执行命令:bashxxx.sh
功能:自动检测系统并更新源
适用系统版本:兼容线上所有linux版本
执行方法:以root身份执行命令:bashupdate_source.sh
解决了什么问题:一键式检测系统并更新源
给客户带来了好处:用户只需执行该脚本一次即可自动检测系统并更新源。
备注:由于系统版本都有支持的周期所以部分源可能会出现不可用的情况,包括官方的源,这是正常情况
#!/bin/bash ######################################### #Function:updatesource #Usage:bashupdate_source.sh #Author:Customerservicedepartment #Company:AlibabaCloudComputing #Version:2.0 ######################################### check_os_release() { whiletrue do os_release=$(grep"RedHatEnterpriseLinuxServerrelease"/etc/issue2>/dev/null) os_release_2=$(grep"RedHatEnterpriseLinuxServerrelease"/etc/redhat-release2>/dev/null) if["$os_release"]&&["$os_release_2"] then ifecho"$os_release"|grep"release5">/dev/null2>&1 then os_release=redhat5 echo"$os_release" elifecho"$os_release"|grep"release6">/dev/null2>&1 then os_release=redhat6 echo"$os_release" else os_release="" echo"$os_release" fi break fi os_release=$(grep"AliyunLinuxrelease"/etc/issue2>/dev/null) os_release_2=$(grep"AliyunLinuxrelease"/etc/aliyun-release2>/dev/null) if["$os_release"]&&["$os_release_2"] then ifecho"$os_release"|grep"release5">/dev/null2>&1 then os_release=aliyun5 echo"$os_release" elifecho"$os_release"|grep"release6">/dev/null2>&1 then os_release=aliyun6 echo"$os_release" else os_release="" echo"$os_release" fi break fi os_release=$(grep"CentOSrelease"/etc/issue2>/dev/null) os_release_2=$(grep"CentOSrelease"/etc/*release2>/dev/null) if["$os_release"]&&["$os_release_2"] then ifecho"$os_release"|grep"release5">/dev/null2>&1 then os_release=centos5 echo"$os_release" elifecho"$os_release"|grep"release6">/dev/null2>&1 then os_release=centos6 echo"$os_release" else os_release="" echo"$os_release" fi break fi os_release=$(grep-i"ubuntu"/etc/issue2>/dev/null) os_release_2=$(grep-i"ubuntu"/etc/lsb-release2>/dev/null) if["$os_release"]&&["$os_release_2"] then ifecho"$os_release"|grep"Ubuntu10">/dev/null2>&1 then os_release=ubuntu10 echo"$os_release" elifecho"$os_release"|grep"Ubuntu12.04">/dev/null2>&1 then os_release=ubuntu1204 echo"$os_release" elifecho"$os_release"|grep"Ubuntu12.10">/dev/null2>&1 then os_release=ubuntu1210 echo"$os_release" else os_release="" echo"$os_release" fi break fi os_release=$(grep-i"debian"/etc/issue2>/dev/null) os_release_2=$(grep-i"debian"/proc/version2>/dev/null) if["$os_release"]&&["$os_release_2"] then ifecho"$os_release"|grep"Linux6">/dev/null2>&1 then os_release=debian6 echo"$os_release" else os_release="" echo"$os_release" fi break fi break done } modify_rhel5_yum() { wget-O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyuncs.com/repo/Centos-5.repo sed-i's/aliyun/aliyuncs/g'/etc/yum.repos.d/CentOS-Base.repo sed-i's/\$releasever/5/'/etc/yum.repos.d/CentOS-Base.repo yumcleanmetadata yummakecache cd~ } modify_rhel6_yum() { wget-O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyuncs.com/repo/Centos-6.repo sed-i's/aliyun/aliyuncs/g'/etc/yum.repos.d/CentOS-Base.repo sed-i's/\$releasever/6/'/etc/yum.repos.d/CentOS-Base.repo yumcleanmetadata yummakecache cd~ } update_ubuntu10_apt_source() { echo-e"\033[40;32mBackuptheoriginalconfigurationfile,newnameandpathis/etc/apt/sources.list.back.\n\033[40;37m" cp-fp/etc/apt/sources.list/etc/apt/sources.list.back cat>/etc/apt/sources.list<<EOF #ubuntu debhttp://cn.archive.ubuntu.com/ubuntu/maverickmainrestricteduniversemultiverse deb-srchttp://cn.archive.ubuntu.com/ubuntu/maverickmainrestricteduniversemultiverse #163 debhttp://mirrors.163.com/ubuntu/maverickmainuniverserestrictedmultiverse deb-srchttp://mirrors.163.com/ubuntu/maverickmainuniverserestrictedmultiverse debhttp://mirrors.163.com/ubuntu/maverick-updatesuniversemainmultiverserestricted deb-srchttp://mirrors.163.com/ubuntu/maverick-updatesuniversemainmultiverserestricted #lupaworld debhttp://mirror.lupaworld.com/ubuntu/maverickmainuniverserestrictedmultiverse deb-srchttp://mirror.lupaworld.com/ubuntu/maverickmainuniverserestrictedmultiverse debhttp://mirror.lupaworld.com/ubuntu/maverick-securityuniversemainmultiverserestricted deb-srchttp://mirror.lupaworld.com/ubuntu/maverick-securityuniversemainmultiverserestricted debhttp://mirror.lupaworld.com/ubuntu/maverick-updatesuniversemainmultiverserestricted debhttp://mirror.lupaworld.com/ubuntu/maverick-proposeduniversemainmultiverserestricted deb-srchttp://mirror.lupaworld.com/ubuntu/maverick-proposeduniversemainmultiverserestricted debhttp://mirror.lupaworld.com/ubuntu/maverick-backportsuniversemainmultiverserestricted deb-srchttp://mirror.lupaworld.com/ubuntu/maverick-backportsuniversemainmultiverserestricted deb-srchttp://mirror.lupaworld.com/ubuntu/maverick-updatesuniversemainmultiverserestricted EOF apt-getupdate } update_ubuntu1204_apt_source() { echo-e"\033[40;32mBackuptheoriginalconfigurationfile,newnameandpathis/etc/apt/sources.list.back.\n\033[40;37m" cp-fp/etc/apt/sources.list/etc/apt/sources.list.back cat>/etc/apt/sources.list<<EOF #12.04 debhttp://mirrors.aliyuncs.com/ubuntu/precisemainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/precise-securitymainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/precise-updatesmainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/precise-proposedmainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/precise-backportsmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/precisemainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/precise-securitymainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/precise-updatesmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/precise-proposedmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/precise-backportsmainrestricteduniversemultiverse EOF apt-getupdate } update_ubuntu1210_apt_source() { echo-e"\033[40;32mBackuptheoriginalconfigurationfile,newnameandpathis/etc/apt/sources.list.back.\n\033[40;37m" cp-fp/etc/apt/sources.list/etc/apt/sources.list.back cat>/etc/apt/sources.list<<EOF #12.10 debhttp://mirrors.aliyuncs.com/ubuntu/quantalmainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/quantal-securitymainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/quantal-updatesmainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/quantal-proposedmainrestricteduniversemultiverse debhttp://mirrors.aliyuncs.com/ubuntu/quantal-backportsmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/quantalmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/quantal-securitymainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/quantal-updatesmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/quantal-proposedmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyuncs.com/ubuntu/quantal-backportsmainrestricteduniversemultiverse EOF apt-getupdate } update_debian_apt_source() { echo-e"\033[40;32mBackuptheoriginalconfigurationfile,newnameandpathis/etc/apt/sources.list.back.\n\033[40;37m" cp-fp/etc/apt/sources.list/etc/apt/sources.list.back cat>/etc/apt/sources.list<<EOF #debian6 debhttp://mirrors.aliyuncs.com/debian/squeezemainnon-freecontrib debhttp://mirrors.aliyuncs.com/debian/squeeze-proposed-updatesmainnon-freecontrib deb-srchttp://mirrors.aliyuncs.com/debian/squeezemainnon-freecontrib deb-srchttp://mirrors.aliyuncs.com/debian/squeeze-proposed-updatesmainnon-freecontrib EOF apt-getupdate } ####################Start################### #checklockfile,onetimeonlyletthescriptrunonetime LOCKfile=/tmp/.$(basename$0) if[-f"$LOCKfile"] then echo-e"\033[1;40;31mThescriptisalreadyexist,pleasenexttimetorunthisscript.\n\033[0m" exit else echo-e"\033[40;32mStep1.Nolockfile,begintocreatelockfileandcontinue.\n\033[40;37m" touch$LOCKfile fi #checkuser if[$(id-u)!="0"] then echo-e"\033[1;40;31mError:Youmustberoottorunthisscript,pleaseuseroottoinstallthisscript.\n\033[0m" rm-rf$LOCKfile exit1 fi echo-e"\033[40;32mStep2.BegentochecktheOSissue.\n\033[40;37m" os_release=$(check_os_release) if["X$os_release"=="X"] then echo-e"\033[1;40;31mTheOSdoesnotidentify,Sothisscriptisnotexecutede.\n\033[0m" rm-rf$LOCKfile exit0 else echo-e"\033[40;32mThisOSis$os_release.\n\033[40;37m" fi echo-e"\033[40;32mStep3.Begentomodifythesourceconfigrationfileandupdate.\n\033[40;37m" case"$os_release"in redhat5|centos5|aliyun5) modify_rhel5_yum ;; redhat6|centos6|aliyun6) modify_rhel6_yum ;; ubuntu10) update_ubuntu10_apt_source ;; ubuntu1204) update_ubuntu1204_apt_source ;; ubuntu1210) update_ubuntu1210_apt_source ;; debian6) update_debian_apt_source ;; esac echo-e"\033[40;32mSuccess,exitnow!\n\033[40;37m" rm-rf$LOCKfile