手记

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日

1人推荐
随时随地看视频
慕课网APP