linux 切换软件源到国内代理加速地址

简介: 经常会使用各种Linux的发行版本,很多时候需要使用yum和apt去安装软件,但是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


目录
相关文章
|
2月前
|
存储 缓存 Linux
如何在Linux环境下对pip的缓存地址进行修改
如何在Linux环境下对pip的缓存地址进行修改
|
存储 编译器
Linux--程序地址空间
Linux--程序地址空间
|
2月前
|
安全 Linux 网络安全
Linux使用HTTP隧道代理代码示例模版
Linux使用HTTP隧道代理代码示例模版
20 0
|
4天前
|
存储 Linux 程序员
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
|
7天前
|
网络协议 Linux 开发工具
|
12天前
|
安全 Linux 编译器
【linux进程(七)】程序地址空间深度剖析
【linux进程(七)】程序地址空间深度剖析
|
12天前
|
Shell Linux 程序员
【linux进程(六)】环境变量再理解&程序地址空间初认识
【linux进程(六)】环境变量再理解&程序地址空间初认识
|
12天前
|
网络协议 Linux 程序员
【Linux】虚拟机ipv4地址消失,主机ping不通
【Linux】虚拟机ipv4地址消失,主机ping不通
21 0
|
13天前
|
存储 Linux 调度
Linux的学习之路:12、地址空间(续)与进程的创建、终止和等待
Linux的学习之路:12、地址空间(续)与进程的创建、终止和等待
17 0
|
13天前
|
Linux C++
Linux的学习之路:11、地址空间
Linux的学习之路:11、地址空间
16 0