目录
前言
参考文章:搭建本地yum仓库源
在参考文章的基础上,完善搭建本地yum仓库源的一些操作步骤,并分析具体步骤的操作原因。相同的步骤不做任何标注,修改的操作步骤会添加更新的字样。
一、环境准备(更新)
- 准备3台虚拟机,1台作为服务端(安装yum源),主机名为test1,另外2台作为客户端(测试yum源安装结果),主机名分别为test2,test3。
[root@192 ~]# hostnamectl set-hostname test1 #永久修改主机名
[root@192 ~]# hostnamectl test1 #临时设置主机名,关机后就失效了
[root@192 ~]# hostnamectl --help #查看hostnamectl的用法
-
本文使用xshell作为远程软件,将镜像文件传输到作为服务端的虚拟机上。上传的镜像文件不能是最小安装的版本,必须是Everything ISO的版本,这样才能作为本地yum源安装软件。(Everything ISO:包含了所有软件组件,体积庞大,高达9.58G。对完整版安装盘的软件进行补充,集成所有软件。)本文使用的镜像文件是CentOS-7-x86_64-Everything-2207-02.iso。
-
在VMware上,打开3台虚拟机,并用xshell进行内网连接。
-
修改网卡的配置文件,更改网关ip(GATEWAY),营造3台虚拟机都无法连接公网的测试环境。
[root@test1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33 #进入网卡的配置文件
更改GATEWAY
[root@test1 ~]# systemctl restart network #重启网卡
二、服务器初始化(更新)
1.备份yum源的配置文件
3台虚拟机都备份yum源的配置文件
[root@test1 ~]# cd /etc/yum.repos.d/ #进入yum的配置文件目录
[root@test1 yum.repos.d]# mkdir bak
[root@test1 yum.repos.d]# mv *.repo bak/ #工作中,一般会把配置文件放到bak文件夹中
2.关闭防火墙
3台虚拟机都关闭防火墙
- firewalld的设置
[root@test1 ~]# cd /etc/yum.repos.d/ #进入yum的配置文件目录
[root@test1 yum.repos.d]# systemctl stop firewalld #关闭防火墙
[root@test1 yum.repos.d]# systemctl disable firewalld #设置开机不自启
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@test1 yum.repos.d]# systemctl status firewalld #查看防火墙状态
● 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安全策略的设置
[root@test1 yum.repos.d]# setenforce 0 #临时设置selinux策略
[root@test1 yum.repos.d]# vi /etc/selinux/config #进入安全策略的配置文件
将SELINUX=enforcing修改为SELINUX=disabled。安全策略的配置文件是系统级别的,修改后,需要重启服务器。
[root@test1 yum.repos.d]reboot #重启服务器
[root@test1 ~]# getenforce #获取当前系统的selinux安全策略
Disabled
selinux是设置安全策略的。selinux的状态只能设置这3个值:enforcing、permissive、disabled。默认是enforcing,强制不让访问。工作中一定是要设置成 disabled。
setenforce 0中的0代表permissive策略。
如果使用机器允许重启,那只用改配置文件之后,重启服务器reboot;
如果使用不能重启,那就先setenforce 之后,改配置文件,最后重启服务器reboot。
三、配置本地yum源(服务端)
1.创建yum_http.repo文件
配置文件的名字可以随便取,但要以.repo结尾。
[root@test1 ~]# cd /etc/yum.repos.d/ #进入yum的配置文件目录
[root@test1 yum.repos.d]# vi yum_http.repo #创建yum配置文件,不需要touch新建一个文件再进入,vi直接就可以新建一个文件并进入
#yum_http.repo文件内容如下
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
baseurl指明该虚拟机的yum源。file:// 说明yum源是本地目录。
2.挂载镜像文件(更新)
[root@test1 yum.repos.d]# cd #进入到镜像所在的目录
[root@test1 ~]# mkdir /opt/centos #创建镜像文件的挂载目录
[root@test1 ~]# ll
总用量 10046412
-rw-------. 1 root root 1269 12月 12 01:42 anaconda-ks.cfg
-rw-r--r-- 1 root root 10287327232 12月 14 18:18 CentOS-7-x86_64-Everything-2207-02.iso
[root@test1 ~]# mount CentOS-7-x86_64-Everything-2207-02.iso /opt/centos #挂载到/opt/centos目录下
mount: /dev/loop0 写保护,将以只读方式挂载
[root@test1 ~]# ls /opt/centos
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
已将镜像文件挂载到/opt/centos目录下,/opt/centos里的repodata包含所有需要安装的软件。根据yum源的yum_http.repo配置文件可知,yum进行安装的路径是本地的/opt/centos目录。
3.安装httpd服务
服务端test1已经可以用yum install -y httpd,在本地进行软件安装。
[root@test1 ~]# yum install -y httpd #安装httpd服务
已加载插件:fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
(1/2): centos/group_gz | 153 kB 00:00:00
(2/2): centos/primary_db | 6.1 MB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-97.el7.centos.5 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-97.el7.centos.5,它被软件包 httpd-2.4.6-97.el7.centos.5.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-97.el7.centos.5.x86_64 需要
--> 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-97.el7.centos.5.x86_64 需要
--> 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-97.el7.centos.5.x86_64 需要
--> 正在检查事务
---> 软件包 apr.x86_64.0.1.4.8-7.el7 将被 安装
---> 软件包 apr-util.x86_64.0.1.5.2-6.el7 将被 安装
---> 软件包 httpd-tools.x86_64.0.2.4.6-97.el7.centos.5 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================================================================================================
Package 架构 版本 源 大小
======================================================================================================================================================================================
正在安装:
httpd x86_64 2.4.6-97.el7.centos.5 centos 2.7 M
为依赖而安装:
apr x86_64 1.4.8-7.el7 centos 104 k
apr-util x86_64 1.5.2-6.el7 centos 92 k
httpd-tools x86_64 2.4.6-97.el7.centos.5 centos 94 k
mailcap noarch 2.1.41-2.el7 centos 31 k
事务概要
======================================================================================================================================================================================
安装 1 软件包 (+4 依赖软件包)
总下载量:3.0 M
安装大小:10 M
Downloading packages:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 22 MB/s | 3.0 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : apr-1.4.8-7.el7.x86_64 1/5
正在安装 : apr-util-1.5.2-6.el7.x86_64 2/5
正在安装 : httpd-tools-2.4.6-97.el7.centos.5.x86_64 3/5
正在安装 : mailcap-2.1.41-2.el7.noarch 4/5
正在安装 : httpd-2.4.6-97.el7.centos.5.x86_64 5/5
验证中 : apr-1.4.8-7.el7.x86_64 1/5
验证中 : mailcap-2.1.41-2.el7.noarch 2/5
验证中 : httpd-tools-2.4.6-97.el7.centos.5.x86_64 3/5
验证中 : apr-util-1.5.2-6.el7.x86_64 4/5
验证中 : httpd-2.4.6-97.el7.centos.5.x86_64 5/5
已安装:
httpd.x86_64 0:2.4.6-97.el7.centos.5
作为依赖被安装:
apr.x86_64 0:1.4.8-7.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-97.el7.centos.5 mailcap.noarch 0:2.1.41-2.el7
完毕!
httpd软件是做web网页服务的,也可以搞文件服务器。虽然当前环境连接不了公网,但也能实现网页的设计和访问。
- 补充内容:httpd的效果展示
web网页服务:
[root@test1 ~]# cd /var/www/html/ #进入到httpd的默认目录
[root@test1 html]# vi index.html
在edge浏览器中,点击扩展。在获取扩展中,下载sider:Chatgpt侧边栏,让chatgpt编写一个技术博客的标准html文件。然后代码复制下来,粘贴到index.html中。在浏览器中,输入该虚拟机的ip地址,就可以查看网页效果。
文件服务器:
[root@test1 ~]# cd /var/www/html/ #进入到httpd的默认目录
[root@test1 html]# mkdir test
[root@test1 html]# mv /root/redis.tar.gz test #将压缩包移动到test文件夹中
4.配置httpd服务(更新)
[root@test1 ~]# cd /var/www/html/ #进入到httpd的默认目录
[root@test1 html]# mkdir centos #创建一个centos目录
[root@test1 html]# ln -s /opt/centos/ /var/www/html/centos #创建一个快捷方式
[root@test1 html]# ll
总用量 4
lrwxrwxrwx 1 root root 12 12月 14 19:51 centos -> /opt/centos/
-rw-r--r-- 1 root root 1562 12月 14 19:32 index.html
drwxr-xr-x 2 root root 65 12月 14 19:43 test
[root@test1 html]# ls centos #验证快捷方式是否创建成功
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
让http默认访问目录下的centos直接链接到/opt/centos下挂载的文件。这比从/opt/centos下cp复制文件,到/var/www/html/centos,更方便快捷。
创建软连接时,最好用绝对路径。
删除软链接目录不能带/,带了会把源文件的内容删除的。
5.启动httpd服务和设置httpd开机自启
[root@test1 html]# systemctl start httpd && systemctl enable httpd #启动和开机自启httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@test1 html]# ss -ntl #查看开启了哪些端口
[root@test1 html]# ss -ntl|grep 80 #web网页服务一般默认是80端口
LISTEN 0 128 [::]:80 [::]:*
四、测试本地yum源(客户端)
1.创建yum_http.repo文件
配置文件的名字可以随便取,但要以.repo结尾。
baseurl要注意修改
[root@test2 ~]# cd /etc/yum.repos.d/ #进入yum的配置文件目录
[root@test2 yum.repos.d]# vi yum_http.repo #创建yum配置文件,不需要touch新建一个文件再进入,vi直接就可以新建一个文件并进入
#yum_http.repo文件内容如下
[centos]
name=centos
baseurl=http://192.168.149.6/centos
gpgcheck=0
enabled=1
baseurl指明该虚拟机的yum源。http://192.168.149.6 说明yum源是服务端test1的httpd的默认目录下。
2.用yum安装文件
通过yum install -y wget是否能安装成功,判断yum源本地仓是否搭建完成。
[root@test2 ~]# yum -y install wget
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
centos | 3.6 kB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 wget.x86_64.0.1.14-18.el7_6.1 将被 安装
--> 解决依赖关系完成
依赖关系解决
==============================================================================================
Package 架构 版本 源 大小
==============================================================================================
正在安装:
wget x86_64 1.14-18.el7_6.1 centos 547 k
事务概要
==============================================================================================
安装 1 软件包
总下载量:547 k
安装大小:2.0 M
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm | 547 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : wget-1.14-18.el7_6.1.x86_64 1/1
验证中 : wget-1.14-18.el7_6.1.x86_64 1/1
已安装:
wget.x86_64 0:1.14-18.el7_6.1
完毕!
总结
根据上课的内容,整理的学习笔记,方便以后自己回顾所学内容。