Administrator
发布于 2024-10-11 / 276 阅读
0
0

Linux 更换 yum、apt 镜像源

一、前言

参考:https://developer.aliyun.com/article/827901

主要讲 CentOS、Fedora、Ubuntu、Debian 这四个 Linux 发行版软件源地址的更换,更换成国内的镜像地址,加速访问。其他发行版诸如 OpenSUSE、FreeBSD 可参考上面的阿里云社区文章。写于 2024/10/10,请仔细甄别文章的时效性。

二、CentOS

CentOS 原来是一个基于 Red Hat Enterprise Linux(RHEL)源代码构建的社区版操作系统,由于 2018 年 IBM 收购 Red Hat,上下游关系置换,CentOS 改为 RHEL 上游的滚动发行版 Linux(CentOS Stream)。

1、备份

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

2、下载软件源文件

注意,下面地址中的 Centos-7.repo 中的 7 对应的是你的 CentOS 版本,如果你是 CentOS8,那需要修改为 Centos-8.repo。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 如果安装不了 wget、curl, 可以在您的个人电脑浏览器直接访问链接下载, 然后 ftp 上传到 CentOS 上。

3、更新软件源缓存

yum clean all && yum makecache

三、Fedora

Fedora 是由 RedHat 创建,由社区驱动的 Linux 版本。

Fedora 是 RHEL (Enterprise Linux)的上游版本;RHEL 是 CentOS 的上游版本(CentOS 8 以下)。

同属 RedHat 系的成员,它们的配置大同小异,有一点不同的是包管理器 YUM 改为更加高效的 DNF。其实从 CentOS 8 开始,yum 就被 dnf 取代作为默认的软件包管理器,但是 yum 命令仍然存在,并且大多数情况下会被映射到 dnf 命令。

# 1、备份
mv /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak
mv /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak

# 2、下载软件源
wget -O /etc/yum.repos.d/fedora.repo http://mirrors.aliyun.com/repo/fedora.repo
wget -O /etc/yum.repos.d/fedora-updates.repo http://mirrors.aliyun.com/repo/fedora-updates.repo

# 3、更新源缓存(clean all 一般是安装有问题才会清除, makecache 就够了)
dnf clean all
dnf makecache

四、Ubuntu

1、备份

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

2、查看 Ubuntu 版本代号

cat /etc/os-release

Ubuntu 发布版本的官方名称是 Ubuntu X.YY,例如 20.4,X 表示年份,YY 表示发布的月份。Ubantu 每两年的 4 月份会发布一个长期支持版本(LTS),支持期长达五年。近期发布的版本和代号:

  • Ubuntu 20.04 LTS,代号为 Focal Fossa

  • Ubuntu 22.04 LTS,代号为 Jammy Jellyfish

  • Ubuntu 24.04 LTS,代号为 Noble Numbat

  • 同理,下一 LTS 版本,预计在 2026 年 4 月发布:Ubuntu 26.04 LTS

用上述命令获取到代号的第一个单词,例如 20.04Focal,源配置文件 sources.list 需要用到。

如下面介绍的阿里源(20.04 LTS),如果你是 22.04 LTS,只需要把 focal 替换成 jammy 即可。

3、修改源文件 sources.list

阿里源(20.04 LTS):

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src 是下载软件源代码, 一般不需要
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

清华源(20.04 LTS):

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
  • 方式一:如果能安装 vim:apt-get update && apt-get install -y vim, 可以通过 vim 来修改。

  • 方式二:安装不了可以考虑本地编辑好再上传到 ubuntu 上。

  • 方式三:使用 sed 替换源地址:

sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
  • 方式四:或者用 echo + 重定向,如下。

echo 'deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse' > sources.list
  • 方式五:或者用 echo + tee,-a 表示追加:

# 1、清空文件
echo "" > /etc/apt/sources.list
# 2、追加写入
echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" | tee -a /etc/apt/sources.list  
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" | tee -a /etc/apt/sources.list  
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" | tee -a /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" | tee -a /etc/apt/sources.list  
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse" | tee -a /etc/apt/sources.list  

4、更新软件源缓存

apt-get update

五、Debian

Ubuntu 是 Debian 系的,配置基本上 Ubuntu 一样,源地址有所不同。

清华源:

deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free
  • 方式三:使用 sed 替换源地址:

sudo sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list


评论