linux 切換軟件源到國內代理加速地址

一、背景

經常會使用各種Linux的發行版本,很多時候需要使用yumapt去安裝軟件,但是Linux鏡像中的軟件源使用的是國外的地址,訪問速度非常慢,有些時候甚至訪問失敗,每次都需要去網上找對應的軟件源地址。

這種工作反反覆覆,我想着乾脆一次性收集我常用的Linux發行版本的軟件源替換方法,需要的時候直接將命令粘貼上去就可以了,現在將這個文章分享出來,希望能幫到有此類需求的朋友。

二、Centos 替換軟件源

centos目前我主要是使用的是 centos7、centos8,兩個版本替換步驟基本是一致的,但是具體命令有一些區別,千萬不要搞混了。

2.1 Centos7

安裝base reop源,接着備份舊的配置文件

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

修改爲阿里雲

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

更新

yum update

2.2 Centos8

備份原始軟件源文件

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
mv /etc/yum.repos.d/CentOS-PowerTools.repo /etc/yum.repos.d/CentOS-PowerTools.repo.backup
mv /etc/yum.repos.d/CentOS-Extras.repo.backup /etc/yum.repos.d/CentOS-Extras.repo.backup.backup
mv /etc/yum.repos.d/CentOS-centosplus.repo.repo /etc/yum.repos.d/CentOS-centosplus.repo.backup
mv /etc/yum.repos.d/CentOS-AppStream.repo.repo /etc/yum.repos.d/CentOS-AppStream.repo.backup

以上文件都需要重新命名,因爲阿里雲源裏Centos-8.repo包含了上述所有文件,如果按照傳統手法只更改CentOS-Base.repo這個文件 會出現一下提示

Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration

修改爲阿里雲

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

運行yum makecache生成緩存

yum clean all
yum makecache

三、Ubuntu

Ubuntu 的軟件源地址有很多種類型,需要將這些類型寫入到/etc/apt/sources.list文件中,寫入之前最好將其備份。

3.1 Ubuntu 18

備份命令如下所示

cp  /etc/apt/sources.list /etc/apt/sources.list.bak

將長常見的阿里雲軟件源地址寫入到文件,命令如下所示

tee /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-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports 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
EOF

更新軟件源信息命令如下所示

apt update

3.2 Ubuntu20

備份命令如下所示

cp  /etc/apt/sources.list /etc/apt/sources.list.bak

將長常見的阿里雲軟件源地址寫入到文件,命令如下所示

tee /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

更新軟件源信息命令如下所示

apt update

四、debian 需改軟件源

debian系統和Ubuntu替換方法基本是一致的,但是內容不一樣,內容不要搞混淆。

4.1 debian 10

備份命令如下所示

cp  /etc/apt/sources.list /etc/apt/sources.list.bak
tee /etc/apt/sources.list <<-'EOF'
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ buster/updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security/ buster/updates main non-free contrib
EOF

更新軟件源信息命令如下所示

apt update

4.2 debian 9

備份命令如下所示

cp  /etc/apt/sources.list /etc/apt/sources.list.bak
tee /etc/apt/sources.list <<-'EOF'
deb http://mirrors.aliyun.com/debian stretch main contrib non-free
deb-src http://mirrors.aliyun.com/debian stretch main contrib non-free
deb http://mirrors.aliyun.com/debian stretch-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian stretch-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free
EOF

更新軟件源信息命令如下所示

apt update

五、 Mac os

Mac OS 系統一般都會使用brew來安裝軟件,但是brew的源也是在國外,下載軟件總是很慢,這裏我們可以將一些常用的軟件倉庫設置未阿里雲的鏡像倉庫

5.1 brew替換軟件源

首先要替換的是brew本身的倉庫地址,這裏需要進入到brew的git倉庫目錄去,命令行如下所示

cd "$(brew --repo)"

進入到brew的git倉庫目錄後,需要將遠程地址修改爲阿里雲的倉庫地址,命令如下所示

git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

接下來需要替換homebrew-core的倉庫地址,同樣需要先進入此倉庫的目錄,命令如下所示

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

進入homebrew-core倉庫之後,需要替換遠程倉庫地址,命令如下所示

git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

兩個倉庫的地址都替換完成之後,需要讓brew生效,命令如下所示

brew update

作者:湯青松

日期:2021年9月16日

微信:songboy8888

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章