Centos7修改成国内镜像源

前言

yum(Yellowdog Updater Modified)是一个在 CentOS、Fedora 和其他基于 Red Hat 的 Linux 发行版上常用的包管理器。Yum 负责从软件仓库中下载并安装软件包,并解决软件包之间的依赖关系。

默认情况下,CentOS 使用的是官方的 Yum 源,但有时用户可能希望将 Yum 源切换到国内的源提高下载速度和稳定性,特别是对于国内用户。(因为国内 Yum 源具有地理位置近、网络带宽高、稳定性好等优势,能更快下载软件包,及时更新,提高系统安装和维护效率。)

提示:先去学一下vim的使用

安装国内源的步骤

方法一

备份原始 Yum 配置文件

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base-backup.repo

修改文件

vim /etc/yum.repos.d/CentOS-Base.repo

添加下面的内容覆盖全部以前的内容

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

方法二

备份原始 Yum 配置文件

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base-backup.repo

下载国内的 Yum 源配置文件

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

刷新 yum 缓存

yum makecache

使用已更换的源进行系统软件包的更新

yum -y update

补充

wget 是一个命令行工具,用于从网络上下载文件。它支持通过 HTTP、HTTPS 和 FTP 协议下载文件,并且具有灵活的选项,可以用来执行各种下载任务。

方法一要先清除源文件里面的内容。可以用echo " " > /etc/yum.repos.d/CentOS-Base.repo
也可以用vim,在命令模式下(编辑模式退出命令模式:ESC 键盘左上角这个键),两次点击D就可以删除一行


觉得有用点个👍,谢谢

### 更换 CentOS 7 Yum 默认镜像源国内镜像源CentOS 7更换 Yum 的默认镜像源国内镜像源是一项常见的操作,可以显著提升软件包的下载速度并改善用户体验。以下是具体方法: #### 1. 备份原始 Yum 配置文件 为了防止意外情况发生,在更改之前建议备份现有的 `CentOS-Base.repo` 文件: ```bash cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ``` #### 2. 清理旧的 Yum 源 进入 `/etc/yum.repos.d/` 目录,并删除原有的国外 Yum 源文件: ```bash cd /etc/yum.repos.d/ rm -rf * ``` 此步骤会清空当前目录下的所有文件[^2]。 #### 3. 下载新的国内镜像源配置文件 可以选择网易、阿里云或其他国内镜像源作为替代方案。以下是以阿里云为例的操作方式: ```bash wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo ``` 该命令将下载阿里云提供的适用于 CentOS 7 的 Yum 配置文件[^4]。 如果更倾向于使用其他镜像源(如网易),可替换上述 URL 地址为对应的地址,例如: ```bash wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo ``` 这一步骤同样基于指定的国内镜像源配置更新[^1]。 #### 4. 安装必要的工具和支持库 确保系统已安装支持管理 Yum 源所需的工具以及设备映射器驱动程序依赖项: ```bash yum install -y yum-utils device-mapper-persistent-data lvm2 --skip-broken ``` 通过这条指令能够保障后续操作顺利执行[^5]。 #### 5. 更新缓存 最后刷新本地 Yum 缓存以应用最新的修改结果: ```bash yum clean all yum makecache ``` 清理全部现有数据后再重新构建索引,从而启用新设置后的国内镜像服务[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值