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

相关推荐
阿里云大数据AI技术15 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
orion571 天前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站2 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户120487221612 天前
Linux驱动编译与加载
linux·嵌入式
火车叼位2 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站3 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维