
Centos7国内常用源
发布日期:2021-05-28 17:11:44
浏览次数:29
分类:精选文章
本文共 3141 字,大约阅读时间需要 10 分钟。
CentOS 镜像源的配置与快速切换
1. 基础知识
1.1 CentOS 镜像源的工作原理
在 CentOS 系统中,镜像源通常位于 /etc/yum.repos.d/
目录下的 repo
文件中。默认配置文件为 CentOS-Base.repo
,其内容如下:
[root@localhost yum.repos.d]# cat CentOS-Base.repo...[base]name=CentOS-$releasever - Basemirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra#baseurl=......
默认镜像站点为 http://mirrorlist.centos.org
,它根据客户端 IP 地址和镜像服务器的更新状态,自动选择最佳的镜像源。
2. 修改镜像源
2.1 操作前的备份
首先,建议备份现有的镜像源文件,以防万一:
mkdir -p /yum_bak && mv /etc/yum.repos.d/* /yum_bak/
2.2 添加或编辑镜像源
2.2.1 直接编辑
可以直接编辑 CentOS-Base.repo
文件,将镜像站点地址替换为你需要的镜像站点地址。
替换阿里云镜像站点为:
cat > /etc/yum.repos.d/CentOS-Base.repo << EOF[base]name=CentOS-$releasever - Base - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[updates]name=CentOS-$releasever - Updates - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[extras]name=CentOS-$releasever - Extrasfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[centosplus]name=CentOS-$releasever - Plusfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7EOF
2.2.2 使用 curl 下载镜像源文件
也可以通过 curl 命令快速获取镜像源文件并替换:
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo# 如果不是阿里云服务器,需手动修改内网域名:sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
3. 快速替换镜像站点
3.1 镜像源模板
通过模板形式,可以快速实现镜像站点的切换。以下是一个通用的镜像源模板:
echo '[base]name=CentOS-$releasever - Basefailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[updates]name=CentOS-$releasever - Updatesfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[extras]name=CentOS-$releasever - Extrasfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' > /etc/yum.repos.d/CentOS-Base.repo
3.2 国内常用镜像站点列表
3.2.1 网易镜像站点
sed -i 's#http://mirrors.aliyun.com/#http://tel.mirrors.163.com/#g' /etc/yum.repos.d/CentOS-Base.repo
3.2.2 腾讯镜像站点
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.cloud.tencent.com/#g' /etc/yum.repos.d/CentOS-Base.repo
3.2.3 清华大学镜像站点
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.tuna.tsinghua.edu.cn/#g' /etc/yum.repos.d/CentOS-Base.repo
3.2.4 中国科技技术大学镜像站点
sed -i 's#http://mirror ali yun com/#https://mirrors.ustc.edu.cn/#g' /etc/yum.repos.d/CentOS-Base.repo
3.2.5 华为镜像站点
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.huaweicloud.com/#g' /etc/yum.repos.d/CentOS-Base.repo
通过以上命令,可以快速切换至国内常用镜像站点。按照需求执行对应的 sed 命令即可完成镜像源的配置。
发表评论
最新留言
不错!
[***.144.177.141]2025年04月29日 01时37分11秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
小易的升级之路,找出字符串中第一个只出现一次的字符
2019-03-14
创建组出现错误:对COM组件的调用返回了错误 HRESULT E_FAIL。小敏
2019-03-14
数组去重的常用的几种方法
2019-03-14
Linux yum提示Loaded plugins错误的解决方法
2019-03-14
算法——203、移除链表元素(力扣)
2019-03-14
Netty的体系结构及使用
2019-03-14
xshell解决文本粘贴格式错误
2019-03-14
webpack新手教程2021
2019-03-14
什么是证券型代币?
2019-03-14
Android中获取并设置屏幕亮度
2019-03-14
Windows抓包工具-Fiddler
2019-03-14
Swift中使用DispatchGroup分组管理异步任务
2019-03-14
21-JS中常见的函数
2019-03-14
19-认识bootstrap
2019-03-14
为什么要使用UTF-8?
2019-03-14
Android多线程与双缓冲
2019-03-14
MVVM_Template
2019-03-14
栈上内存溢出漏洞利用之Return Address
2019-03-14
Bugku CTF web29(Web)
2019-03-14
网络+图片加载框架(英文版)
2019-03-14