2025-03-19    2025-03-19    894 字  2 分钟

在虚拟机没有网络或者网络不稳定的情况下需要配置本地yum源

优点:由于数据从本地获取,下载和安装软件包的速度非常快,完全不需要网络

缺点:软件可能不是最新版,需要手动更新比较麻烦

一、配置本地yum源

首先将centos自带的网络yum源删掉(自带的默认从centos官网下载软件,服务器在国外,非常不稳定)

1
 [root@cloud ~]# rm -rf /etc/yum.repos.d/*

创建挂载点并挂载镜像

1
2
3
[root@cloud ~]# mkdir /opt/centos

[root@cloud ~]# mount /dev/cdrom /opt/centos/

mount: /dev/sr0 写保护,将以只读方式挂载。

创建并编写本地yum源配置文件,编写完成之后wq保存退出。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@cloud ~]# vi /etc/yum.repos.d/local.repo

[centos]

name=centos                 # Yum源的名称,用于描述这个源

baseurl=file:///opt/centos  # 本地Yum源的位置,指向挂载点目录

gpgcheck=0                  # 不进行GPG签名检查

enabled=1                   # 启用该Yum源

清除缓存原来的生成新的缓存并列出当前系统中已配置和启用的Yum仓库(repositories)及其状态。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@cloud ~]# yum clean all 
已加载插件:fastestmirror, langpacks

正在清理软件源: centos

Cleaning up list of fastest mirrors

Other repos take up 1.0 G of disk space (use --verbose for details)

[root@cloud ~]# yum makecache 

已加载插件:fastestmirror, langpacks

Determining fastest mirrors

centos                                                                                           | 3.6 kB  00:00:00     

(1/4): centos/group_gz                                                                           | 153 kB  00:00:00     

(2/4): centos/primary_db                                                                         | 3.3 MB  00:00:00     

(3/4): centos/other_db                                                                           | 1.3 MB  00:00:00     

(4/4): centos/filelists_db                                                                       | 3.3 MB  00:00:00     

元数据缓存已建立

[root@cloud ~]# yum repolist 

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

源标识                                                   源名称                                                    状态

centos                                                   centos                                                    4,070

repolist: 4,070

下载httpd测试一下

1
[root@cloud ~]# yum install -y httpd

完毕!

下载成功

二、配置网络yum源

优点:软件包和元数据由源服务器自动更新,国内镜像下载速度非常快

缺点:需要稳定的网络连接,网络不稳定或断开时无法使用

首先找到国内的镜像站(我这里以阿里云举例)

找到自己的Linux版本(以centos为例)

找到对应的系统版本(我的是7)并且复制这段命令到虚拟机粘贴

同样需要先删除原有的yum仓库

列出当前系统中已配置和启用的Yum仓库(repositories)及其状态。

1
2
3
[root@cloud ~]# rm -rf /etc/yum.repos.d/*

[root@cloud ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[root@cloud ~]# yum repolist 

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

源标识                                源名称                                                      状态

!base/7/x86_64                        CentOS-7 - Base - mirrors.aliyun.com                        10,072

!extras/7/x86_64                      CentOS-7 - Extras - mirrors.aliyun.com                         526

!updates/7/x86_64                     CentOS-7 - Updates - mirrors.aliyun.com                      5,890

repolist: 16,488

可以看到已经保存成功,网络yum源就配置好了