傲来云 发表于 2025-7-7 16:50:09

【Linux 运维】如何正确更换 Linux 软件源(以 CentOS / Ubuntu 为例)

在日常运维中,很多朋友安装软件时发现速度奇慢,或者提示“找不到包”,多数情况跟你用的是 国外默认软件源 有关。
换源,就是把系统的软件安装地址(repo)换成国内更快的镜像,比如阿里云、清华、中科大等。对国内用户来说,这是一件非常有必要的操作。

一、为什么要换源?
默认源一般是国外的,国内访问速度慢
软件包更新不及时
某些版本源已经停止维护
有时会出现 `yum timeout` 或 `apt-get 404` 错误
所以,换成国内源能显著提升软件安装速度和稳定性。

二、CentOS 系统换源(以 CentOS 7 为例)
1. 备份原有源配置
cd /etc/yum.repos.d/
mkdir backup && mv *.repo backup/2. 下载阿里云 CentOS 源
curl -o CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo3. 清除缓存并生成新缓存
yum clean all
yum makecache完成后,安装软件速度会明显变快:
yum install -y wget git curl
三、Ubuntu / Debian 系统换源(以 Ubuntu 20.04 为例)
1. 备份原 sources.list 文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak2. 替换为阿里云镜像(以下以 Ubuntu 20.04 为例)
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
EOF3. 更新软件包索引
apt update安装时速度会快很多,例如:
apt install -y nginx htop unzip
四、换源后遇到的问题及建议
提示签名错误:可以尝试执行 `apt update --allow-unauthenticated` 或手动添加公钥。
yum 源冲突:确保只保留一个有效的 `.repo` 文件,其他的先移走或注释掉。
多版本系统:选择与你系统对应的版本源,不要随意混用。

总结
换源是每个 Linux 运维人员上手后的第一步。一个合适、快速、稳定的软件源,不仅能加速部署流程,还能减少网络问题带来的故障。

页: [1]
查看完整版本: 【Linux 运维】如何正确更换 Linux 软件源(以 CentOS / Ubuntu 为例)