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

湯青松發表於2021-09-17

一、背景

經常會使用各種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

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章