CentOS中设置yum源

在CentOS中设置yum源可以分为以下几个步骤。我将以设置阿里云镜像源为例进行说明:

1. 备份原有yum源

bash 复制代码
# 备份原有repo文件
sudo cp -r /etc/yum.repos.d /etc/yum.repos.d.backup

# 删除原有repo文件
sudo rm -rf /etc/yum.repos.d/*.repo

2. 下载新的yum源文件

根据你的CentOS版本选择相应的源:

CentOS 7

bash 复制代码
# 下载阿里云Base源
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 下载EPEL源
sudo yum install -y epel-release
sudo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

CentOS 8

bash 复制代码
# 下载阿里云Base源
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

# 下载EPEL源
sudo dnf install -y epel-release
sudo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-8.repo

3. 生成缓存

bash 复制代码
# 清理缓存
sudo yum clean all

# 生成新缓存
sudo yum makecache

# 更新系统
sudo yum update -y

4. 其他常用镜像源

如果需要使用其他镜像源,可以替换URL:

清华镜像源

  • CentOS 7: https://mirrors.tuna.tsinghua.edu.cn/repo/centos-repo/7.repo
  • CentOS 8: https://mirrors.tuna.tsinghua.edu.cn/repo/centos-repo/8.repo

网易镜像源

  • CentOS 7: http://mirrors.163.com/.help/CentOS7-Base-163.repo
  • CentOS 8: http://mirrors.163.com/.help/CentOS8-Base-163.repo

5. 手动配置yum源文件示例

如果需要手动创建,可以编辑 /etc/yum.repos.d/centos.repo

ini 复制代码
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

6. 验证yum源

bash 复制代码
# 查看已配置的yum源
yum repolist all

# 测试安装软件包
yum search nginx

7. 常见问题解决

如果遇到GPG key错误:

bash 复制代码
# 导入GPG key
sudo rpm --import https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

如果遇到缓存问题:

bash 复制代码
# 完全清理
sudo yum clean all
sudo rm -rf /var/cache/yum
sudo yum makecache

这样就完成了CentOS yum源的设置。使用国内镜像源可以显著提高软件包的下载速度。

相关推荐
人工智能训练6 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
微露清风7 小时前
系统性学习Linux-第二讲-基础开发工具
linux·运维·学习
不会代码的小猴8 小时前
Linux环境编程第六天笔记--system-V IPC
linux·笔记
阳光九叶草LXGZXJ8 小时前
达梦数据库-学习-48-DmDrs控制台命令(同步之Manager、CPT模块)
linux·运维·数据库·sql·学习
诸神缄默不语8 小时前
Linux命令行教程
linux
小二李10 小时前
第11章 nestjs服务端开发:登录鉴权
运维·服务器
i建模10 小时前
如何在Arch Linux中重设忘记的root密码
linux·运维·服务器
chatexcel11 小时前
元空AI+Clawdbot:7×24 AI办公智能体新形态详解(长期上下文/自动化任务/工具粘合)
运维·人工智能·自动化
kida_yuan11 小时前
【Linux】运维实战笔记 — 我常用的方法与命令
linux·运维·笔记
@syh.11 小时前
【linux】进程控制
linux