在一般情况下,有外网的存在,可以直接执行yum命令,使用yum库,但在Linux安装软件过程中,通常只有内网环境,无法上网,不能链接外网的yum源,因此需要配置本地yum源,使其能在内网使用yum安装相关软件
准备工作
上传镜像文件
这边使用xftp文件传输工具将本地文件上传到虚拟机/root目录下
上传镜像文件为:CentOS-7-x86_64-DVD-2009.iso
上传镜像原因,镜像中存在使用命令的软件包,我们需要对其进行挂在后引用
进入虚机中使用命令查看上传镜像(直接进入超级用户模式下)
[root@cs001 ~]# pwd
/root
[root@cs001 ~]# ls
anaconda-ks.cfg sshtest.exp 图片 桌面
CentOS-7-x86_64-DVD-2009.iso 公共 文档
file.txt 模板 下载
initial-setup-ks.cfg
关闭NetworkManager
centos7默认使用NetworkManager来管理网络,因为我们手动配置了网络,network和NetworkManager服务会出现冲突,⽽且NetworkManager通常会⽐较先启动,所以为了防⽌NetworkManager的启动导致我们直接配置的⽹络环境失效,我们需要禁⽤它
使用stop命令,表示暂时关闭防火墙,开机再次启动
使用disable命令,永久关闭,开机不启动,一般选择永不启动
[root@cs001 ~]# systemctl stop NetworkManager.service
[root@cs001 ~]# systemctl disable NetworkManager.service
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
关闭防火墙
检查防火墙状态
Active:active(running) 说明防火墙已打开,需要关闭防火墙
[root@cs001 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2023-07-04 09:05:05 CST; 5h 2min ago
Docs: man:firewalld(1)
Main PID: 703 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
└─703 /usr/bin/python2 -Es /usr/sbin/firewa...
同上,再次查看防火墙状态,Active已经dead
[root@cs001 ~]# systemctl stop firewalld.service
[root@cs001 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cs001 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
关闭SElinux
SeLinux全称Security-Enhanced Linux即安全增强型Linux,它是一个Linux内核模块,也是Linux的一个安全子系统。
SELinux对文件权限和访问控制进行了更加严格的限制,可能阻止一些应用程序的正常运行,选择关闭SElinux
查看selinux状态
[root@cs001 ~]# getenforce
Enforcing
暂时关闭selinux
[root@cs001 ~]# setenforce 0
[root@cs001 ~]# getenforce
Permissive
永久关selinux需要进入/etc/selinux/config文件中,将SELINUX=enforcing改为SELINUX=disabled
[root@cs001 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
这边直接使用命令进行更改,也可以使用vim命令进入文件修改
[root@cs001 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@cs001 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# disabled - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of disabled.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
配置本地yum源
挂载镜像文件
新建一个挂载目录,使用mount命令将镜像文件挂载进目录中
[root@cs001 ~]# mkdir /mnt/cdrom
[root@cs001 ~]# ls /mnt
cdrom
[root@cs001 ~]# mount CentOS-7-x86_64-DVD-2009.iso /mnt/cdrom/
mount: /dev/loop0 写保护,将以只读方式挂载
[root@cs001 ~]# ls /mnt/cdrom/
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
修改原有的yum源信息
yum这个工具,只通过识别/etc/yum.repos .d目录 下 repo的文件来识别到哪下载rpm 包,yum.repos.d里的文件是yum的配置文件,我们在访问公共yum库时都是通过这些文件进行,所以进入该目录,修改yum源信息,方便之后的操作 : 包含转移默认.repo文件和自定义新的yum源文件
进入该目录,创建一个新目录用来备份之前的数据
[root@cs001 ~]# cd /etc/yum.repos.d/
[root@cs001 yum.repos.d]# ls
CentOS-Base.repo CentOS-Media.repo
CentOS-CR.repo CentOS-Sources.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
CentOS-fasttrack.repo CentOS-x86_64-kernel.repo
[root@cs001 yum.repos.d]# mkdir oldrepo
[root@cs001 yum.repos.d]# mv CentOS-* oldrepo
[root@cs001 yum.repos.d]# ls
oldrepo
这里直接从原有文件中复制一份合适的进行修改,将原文件重命名为local.repo,新文件名必须要以repo为后缀
(也可以直接新建一个文件,在里面输入需要配置的内容)
[root@cs001 yum.repos.d]# cd oldrepo/
[root@cs001 oldrepo]# cp CentOS-Media.repo ../
[root@cs001 oldrepo]# cd ../
[root@cs001 yum.repos.d]# ls
CentOS-Media.repo oldrepo
[root@cs001 yum.repos.d]# mv CentOS-Media.repo local.repo
[root@cs001 yum.repos.d]# ls
local.repo oldrepo
[root@cs001 yum.repos.d]# cat local.repo
# CentOS-Media.repo
#......
# 修改以下内容
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@cs001 yum.repos.d]# vim local.repo
# CentOS-Media.repo
#......
# 修改为以下内容
[local]
#修改源名称、仓库名称
name=CentOS local
#将url地址修改为镜像挂载地址
baseurl=file:///mnt/cdrom
#GPG验证是否开启,建议不开启,设为0,关闭检测
gpgcheck=0
#表示开启此yum仓库,1表示生效,0则表示不生效
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
清洗并重新读取yum源信息
yum clean all
删除之前yum的缓存,因为之前指向的是公共仓库,yum靠缓存去寻找,所以要删除
yum makecache
重新建立指向本地yum仓库的缓存
yum repolist
列出所有仓库
[root@cs001 yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: local
Cleaning up list of fastest mirrors
[root@cs001 yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
local | 3.6 kB 00:00
(1/4): local/group_gz | 153 kB 00:00
(2/4): local/primary_db | 3.3 MB 00:00
(3/4): local/other_db | 1.3 MB 00:00
(4/4): local/filelists_db | 3.3 MB 00:00
元数据缓存已建立
[root@cs001 yum.repos.d]# yum repolist
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
源标识 源名称 状态
local CentOS local 4,070
repolist: 4,070
总结
本地镜像中自带的Packages就存在许多rpm软件包,无法连接外网进行下载时,只需要将yum源默认指向的位置改为镜像包所在位置即可