Linux 系统使用 iso 镜像文件或光盘配置本地YUM 源的最简单方式 - 遠離塵世の方舟

时间:2024-04-17 18:06:18

Linux 系统使用 iso 镜像文件或光盘配置本地YUM 源的最简单方式

1、分配光驱

选择本地的iso系统镜像文件,或者在光驱中放入系统安装盘。之后,在桌面可以看到RHEL-7.2-Server的光盘图标。

2、查看光驱挂载的位置

使用df -h 命令可以看到光驱或镜像文件挂载的位置。

3、修改本地yum源的配置文件

 

[root@rhel7 ~]# mv /etc/yum.repos.d/redhat.repo /etc/yum.repos.d/redhat.repo_bak   ---备份原来的配置文件
[root@rhel7 ~]# cat /etc/yum.repos.d/cdrom.repo ---使用vi命令新建cdrom.repo,添加内容如下
[cdrom]
name = cdrom
baseurl = file:///run/media/root/RHEL-7.2\ Server.x86_64/     ----注意该路径不能直接用上图中显示的挂载路径,因为中间有空格,要添加反斜杠转义。
gpgcheck = 0
enable = 1
[root@rhel7 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root  8.5G  3.1G  5.5G  36% /
devtmpfs               986M     0  986M   0% /dev
tmpfs                 1001M  192K 1001M   1% /dev/shm
tmpfs                 1001M  8.8M  992M   1% /run
tmpfs                 1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1              497M  140M  358M  29% /boot
tmpfs                  201M  4.0K  201M   1% /run/user/42
tmpfs                  201M   20K  201M   1% /run/user/0
/dev/sr0               3.8G  3.8G     0 100% /run/media/root/RHEL-7.2 Server.x86_64
[root@rhel7 ~]# cd /run/media/root/RHEL-7.2 Server.x86_64 
bash: cd: /run/media/root/RHEL-7.2: No such file or directory
[root@rhel7 ~]# cd /run/media/root/RHEL-7.2\ Server.x86_64/
[root@rhel7 RHEL-7.2 Server.x86_64]# ls
addons  GPL       LiveOS      release-notes            RPM-GPG-KEY-redhat-release
EFI     images    media.repo  repodata                 TRANS.TBL
EULA    isolinux  Packages    RPM-GPG-KEY-redhat-beta
[root@rhel7 RHEL-7.2 Server.x86_64]# 

 

4、测试Yum源是否可用

[root@rhel7 RHEL-7.2 Server.x86_64]# yum install httpd
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7 for package: httpd-2.4.6-40.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================
 Package                         Arch                       Version                             Repository                 Size
================================================================================================================================
Installing:
 httpd                           x86_64                     2.4.6-40.el7                        cdrom                     1.2 M
Installing for dependencies:
 apr                             x86_64                     1.4.8-3.el7                         cdrom                     103 k
 apr-util                        x86_64                     1.5.2-6.el7                         cdrom                      92 k
 httpd-tools                     x86_64                     2.4.6-40.el7                        cdrom                      82 k
 mailcap                         noarch                     2.1.41-2.el7                        cdrom                      31 k

Transaction Summary
================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 1.5 M
Installed size: 4.3 M
Is this ok [y/d/N]: 
……

但是使用这种方式配置的本地YUM源的缺点是你得把ISO系统镜像一直挂载到系统里,如果你本地删除了镜像文件,则yum命令就不可用了。还有个问题就是,下次重启系统时,会自动从光驱启动到系统安装界面。

优点是配置简单、快速,想用的时候直接分配光驱,不想用时就移除,这样也不会占用存储空间。或者如果你的磁盘空间比较大,你也可以把整个光盘中的文件拷贝到系统中的某个目录下,如果修改cdrom.repo配置文件的中baseurl参数到对应目录即可。