lin 发表于 2025-6-16 16:37:15

更换 Linux 系统为阿里云镜像源详解

在使用 Linux 系统时,默认的软件源可能存在下载速度慢、不稳定或软件版本不够新等问题。为了解决这些问题,国内用户通常会将软件源更换为更快的镜像站,比如阿里云提供的开源镜像服务。本文将分别介绍如何在 CentOS 和 Ubuntu 系统中,将软件源更换为阿里云源,以提升软件包安装与更新的速度与效率。
一、CentOS 更换阿里云镜像源
1. 查看系统版本
先确认当前的 CentOS 版本:
cat /etc/redhat-release例如返回结果为:CentOS Linux release 7.9.2009 (Core),则版本为 7。
2. 备份原有源配置
cd /etc/yum.repos.d/mkdir backupmv *.repo backup/3. 下载阿里云 YUM 源配置文件
以 CentOS 7 为例:
curl -o CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo如果是 CentOS 8:
curl -o CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo4. 生成缓存
yum clean allyum makecache现在,你的 CentOS 系统已使用阿里云 YUM 源。
二、Ubuntu 更换阿里云镜像源
1. 查看系统版本
lsb_release -a例如返回:
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:      20.04
Codename:       focal说明当前版本为 Ubuntu 20.04,代号为 focal。
2. 备份原有源配置
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak3. 修改 sources.list
可以手动编辑:
sudo nano /etc/apt/sources.list将内容替换为:
deb http://mirrors.aliyun.com/ubuntu/ focal 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-security main restricted universe multiverse

或者使用命令直接替换:
sudo bash -c 'cat > /etc/apt/sources.list <<EOF
deb http://mirrors.aliyun.com/ubuntu/ focal 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-security main restricted universe multiverse
EOF'将 focal 替换为你当前系统对应的代号(如 bionic, jammy 等)。
4. 更新软件包索引
sudo apt update至此,Ubuntu 系统已经切换至阿里云源。
三、验证镜像是否生效
安装任意软件测试即可,比如安装 htop:
# CentOS
yum install -y htop# Ubuntu
sudo apt install -y htop如果下载速度明显提升,即表示已成功切换为阿里云镜像源。
四、注意事项

[*]阿里云源为中国大陆用户优化,国外用户建议使用其他 CDN 镜像。
[*]更换源前建议备份原始配置,防止配置错误导致系统无法更新。
[*]若使用了 proxy、VPN,可能会影响镜像源访问速度。
[*]部分特殊软件库(如 EPEL、Docker、Node.js)需要单独添加。
五、总结
阿里云提供了稳定且高速的软件源服务,适合国内用户使用。通过将 Linux 系统的软件源更换为阿里云镜像,可以大大提高系统安装和更新软件包的效率,是运维人员或开发者提升系统部署效率的一个重要操作步骤。

页: [1]
查看完整版本: 更换 Linux 系统为阿里云镜像源详解