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源的设置。使用国内镜像源可以显著提高软件包的下载速度。

相关推荐
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.1 天前
进程间通信
linux
AI职业加油站1 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-052 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏2 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK2 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid