CentOS更换国内阿里云yum源超详细教程
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1. 前言</li><li>2. 打开终端</li><li>3. 确保虚拟机已经联网</li><li>4. 备份现有yum配置文件</li><li>5. 下载阿里云yum源</li><li>6. 清理缓存</li><li>7. 重新生成缓存</li><li>8. 测试安装gcc</li><li>Centos更换yum源文件<ul class="second_class_ul"><li>1.备份原数据源文件</li><li>2.创建新yum源文件</li><li>3.更新yum源缓存</li></ul></li></ul></div><p class="maodian"></p><h2>1. 前言</h2><p>有些同学在安装完CentOS操作系统后,在系统内安装比如:gcc等软件的时候出现这种情况:(网络不可达)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q04F.jpg" /></p>
<p>我们可以通过更换为国内yum源的方式来解决这个问题,下面是教程:</p>
<p class="maodian"></p><h2>2. 打开终端</h2>
<p>你可以打开系统终端,也可以打开SSH工具操作。这一步不是必须的,为的是照顾小白同学,不知道什么是SSH工具。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q0123.jpg" /></p>
<p class="maodian"></p><h2>3. 确保虚拟机已经联网</h2>
<p>首先要确保你的虚拟机已经联网,别闹乌龙再是你虚拟机没联网造成的!!!<br />(这一步其实也是为了照顾小白同学)</p>
<p>输入以下命令:</p>
<div class="dxycode"><pre class="brush:bash;">ping www.baidu.com</pre></div>
<p>出现类似以下输出代表已联网:</p>
<blockquote><p><code>Ctrl+Z</code> 或 <code>Ctrl+C</code> 可以退出 <code>ping</code> 命令</p></blockquote>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q1Y3.jpg" /></p>
<p class="maodian"></p><h2>4. 备份现有yum配置文件</h2>
<p>在更改任何配置之前,我们都要养成备份的好习惯!!很多同学都是让改什么配置,直接上手就改,最后导致系统卡开机界面,然后又不知道自己改了什么!</p>
<p>输入以下命令备份:</p>
<p>(注意:这里因为我们是重新下载一份配置文件,所以这里用的是 <code>mv</code> 命令。如果是需要在原文件修改的场景是用 <code>cp</code> 命令复制一份)</p>
<div class="dxycode"><pre class="brush:bash;">mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak</pre></div>
<ul><li>/etc/yum.repos.d/CentOS-Base.repo 是yum的配置文件</li><li>/etc/yum.repos.d/CentOS-Base.repo.bak 是我们备份的配置文件</li></ul>
<p>备份完我们可以用以下命令查看一下:</p>
<div class="dxycode"><pre class="brush:bash;">ls /etc/yum.repos.d</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q1351.jpg" /></p>
<p class="maodian"></p><h2>5. 下载阿里云yum源</h2>
<blockquote><p>国内的yum源,推荐使用阿里云的。要用其他的也可以,把后面的链接换一下就行了。</p></blockquote>
<p>输入以下命令:</p>
<p>(我这里的系统是 <code>CentOS7</code>,所以下的是 <code>Centos-7.repo</code>,你们根据自己的系统版本选择)</p>
<div class="dxycode"><pre class="brush:bash;">wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q1244.jpg" /></p>
<p>下载完,我们看一下是不是有了:</p>
<div class="dxycode"><pre class="brush:bash;">ls /etc/yum.repos.d</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q2950.jpg" /></p>
<p>最后用 cat 命令查看一下我们下载的配置文件内容是不是已经是阿里云的了:</p>
<div class="dxycode"><pre class="brush:bash;">cat /etc/yum.repos.d/CentOS-Base.repo</pre></div>
<p>ok,没问题</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q3a9.jpg" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q3639.jpg" /></p>
<p class="maodian"></p><h2>6. 清理缓存</h2>
<p>输入以下命令:</p>
<div class="dxycode"><pre class="brush:bash;">yum clean all</pre></div>
<p><code>yum clean all</code> 命令用于清理 <code>YUM (Yellowdog Updater Modified)</code> 包管理器的所有缓存。当你执行这个命令时,YUM 会清除所有已下载的元数据和软件包缓存。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q4c3.jpg" /></p>
<p class="maodian"></p><h2>7. 重新生成缓存</h2>
<p>输入以下命令:</p>
<p>(这个过程的耗时会根据你的网速而定,网速快执行越快,耗时越小)</p>
<div class="dxycode"><pre class="brush:bash;">yum makecache</pre></div>
<p>当你运行 <code>yum makecache</code> 命令时,YUM 会执行以下操作:</p>
<ul><li>检查配置文件:YUM 会读取配置文件,通常位于 /etc/yum.repos.d/ 目录下,这些文件定义了可用的软件仓库。</li><li>生成缓存:YUM 会为每个配置好的仓库生成一个缓存。这涉及到从每个仓库的元数据服务器下载必要的信息,例如软件包列表、版本等,并将其存储在本地文件系统上(默认位置通常是 /var/cache/yum/)。</li></ul>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q4618.jpg" /></p>
<p class="maodian"></p><h2>8. 测试安装gcc</h2>
<div class="dxycode"><pre class="brush:bash;">yum install gcc</pre></div>
<p>可以看到不会再出现网络不可达的情况了:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121Q5495.jpg" /></p>
<p>安装完成:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250520/1-250520121QA92.jpg" /></p>
<p class="maodian"></p><h2>Centos更换yum源文件</h2>
<p class="maodian"></p><h3>1.备份原数据源文件</h3>
<div class="dxycode"><pre class="brush:bash;">mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup</pre></div>
<p class="maodian"></p><h3>2.创建新yum源文件</h3>
<p>创建新yum源文件</p>
<div class="dxycode"><pre class="brush:bash;">vi /etc/yum.repos.d/CentOS-Base.repo</pre></div>
<p>源文件内容为</p>
<div class="dxycode"><pre class="brush:bash;"># CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#released updates
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7</pre></div>
<p class="maodian"></p><h3>3.更新yum源缓存</h3>
<div class="dxycode"><pre class="brush:bash;">#清理缓存
yum clean all
#创建缓存
yum makecache
#查看缓存list
yum repolist</pre></div>
頁:
[1]