1. 程式人生 > >搭建yum倉庫、部署共享型yum源、掛載的兩種方式

搭建yum倉庫、部署共享型yum源、掛載的兩種方式

概述:

上一節講到如何讓新建立的虛擬機器下載官方軟體,那麼難道我們每新建一個虛擬機器都需要配置一次yum源嗎?
所以我們需要部署共享型yum源,讓多臺主機也可以下載軟體。

如果將yum源搭建在虛擬機器中,那麼每次開機都需要開啟這個虛擬機器才能實現yum源的共享,
這樣比較麻煩,所以直接將yum源搭建在真機中,在真機中將yum源共享出去。

1.搭建yum源

(1).下載一個和系統匹配的映象

##檢視系統版本資訊
[[email protected] ~]# hostnamectl 

在這裡插入圖片描述

[[email protected] ~]# cd /home/kiosk/Desktop
[
[email protected]
Desktop]# ls rhel-server-7.2-x86_64-dvd.iso ##將桌面上的映象移動到/iso中,防止誤刪 [[email protected] Desktop]# mkdir /iso [[email protected] Desktop]# mv rhel-server-7.2-x86_64-dvd.iso /iso [[email protected] Desktop]# cd /iso [[email protected] iso]# ls rhel-server-7.2-x86_64-dvd.iso

(2).掛載

之所以先臨時掛載再永久性掛載,是為了掛載能立即生效,並且當系統關機再開機(重啟)後掛載仍然存在
[[email protected] iso]# mkdir /source7.2
#臨時掛載
[[email protected] iso]# mount rhel-server-7.2-x86_64-dvd.iso /source7.2
mount: /dev/loop0 is write-protected, mounting read-only
[[email protected] iso]# df

在這裡插入圖片描述

##永久性掛載;/etc/rc.d/rc.local 為開機自動執行指令碼
[
[email protected]
iso]# vim /etc/rc.d/rc.local #################### mount rhel-server-7.2-x86_64-dvd.iso /source7.2 ##給指令碼一個可執行許可權 [[email protected] iso]# chmod +x /etc/rc.d/rc.local [[email protected] iso]# ll /etc/rc.d/rc.local

在這裡插入圖片描述
(3)設定yum源指向

[[email protected] iso]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# ls
redhat.repo  
[[email protected] yum.repos.d]# vim rhel7.2.repo
############################
[rhel7.2]                      #名字(任意)
name=rhel7.2                   #說明(任意)
baseurl=file:///source7.2      #告知系統映象位置;file://表示本地檔案
gpgcheck=0                     #安裝時不檢測

在這裡插入圖片描述
(4)測試

##清除歷史yum源快取資訊
[[email protected] yum.repos.d]# yum clean all

在這裡插入圖片描述

##列出yum源詳細資訊
[[email protected] yum.repos.d]# yum repolist

在這裡插入圖片描述

##測試:安裝dhcp    -y 表示直接安裝不提示
[[email protected] yum.repos.d]# yum install -y dhcp

在這裡插入圖片描述
排錯:當有多個.repo檔案同時被讀取,生效的是最原始的那個檔案,列出yum源資訊時便會報錯

[[email protected] yum.repos.d]# pwd
/etc/yum.repos.d

[[email protected] yum.repos.d]# yum clean all
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.
Cleaning repos: rhel-dvd rhel7.2 rht-ext
Cleaning up everything
##報錯,因為多個.repo檔案中的yum源指向均被讀取
[[email protected] yum.repos.d]# yum repolist
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.
http://172.25.254.250/rhel7.0/x86_64/dvd/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 172.25.254.250:80; Connection refused"
Trying other mirror.
http://172.25.254.250/rhel7.0/x86_64/dvd/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 172.25.254.250:80; Connection refused"
Trying other mirror.
[[email protected] yum.repos.d]# ls
redhat.repo  rhel7.2.repo  rhel-dvd.repo  rht-extras.repo
##解決方案:
[[email protected] yum.repos.d]# vim rhel-dvd.repo
###########################
enabled=0  #相當於註釋,讓系統不讀此檔案

[[email protected] yum.repos.d]# vim rht-extras.repo
###########################
enabled=0  #相當於註釋,讓系統不讀此檔案

[[email protected] yum.repos.d]# yum clean all
Loaded plugins: aliases, changelog, kabi, langpacks, ovl, product-id, search-
              : disabled-repos, subscription-manager, tmprepo, verify,
              : versionlock
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Loading support for Red Hat kernel ABI
Cleaning repos: rhel7.2
Cleaning up everything
[[email protected] yum.repos.d]# yum repolist
Loaded plugins: aliases, changelog, kabi, langpacks, ovl, product-id, search-
              : disabled-repos, subscription-manager, tmprepo, verify,
              : versionlock
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Loading support for Red Hat kernel ABI
rhel7.2                                                  | 4.1 kB     00:00     
(1/2): rhel7.2/group_gz                                    | 136 kB   00:00     
(2/2): rhel7.2/primary_db                                  | 3.6 MB   00:00     
repo id                              repo name                            status
rhel7.2                              rhe7.2                               4,620
repolist: 4,620

2.部署共享型yum源

 http 共享方式:
/var/www/html 為apache的預設釋出目錄
注意:我這裡的客戶端虛擬機器版本為7.0 所以需要共享7.0映象資源

在這裡插入圖片描述
(1).安裝apache並開啟服務

[[email protected] ~]# yum install -y httpd
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

(2).關閉防火牆

##1.直接關閉火牆
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
##2.新增防火牆策略
如果真機需要被當作路由器(必須開啟防火牆並新增防火牆策略,設定核心引數為1)使用,此時就不能關閉火牆;解決方案為直接在火牆策略中新增http服務
#模擬環境
[[email protected] ~]# systemctl start firewalld
[[email protected] ~]# systemctl status firewalld

在這裡插入圖片描述

[[email protected] ~]# firewall-cmd --add-masquerade
success
[[email protected] ~]# firewall-cmd --list-all

在這裡插入圖片描述

##新增http服務到防火牆策略中,即允許http服務被訪問
[[email protected] ~]# firewall-cmd --permanent --add-service=http
success
[[email protected] ~]# firewall-cmd --list-all

在這裡插入圖片描述

##必須重新載入才能生效
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# firewall-cmd --list-all

在這裡插入圖片描述

##檢測真機自己是否可以訪問apache
直接輸入真機ip: 172.25.254.66

在這裡插入圖片描述
(3).掛載

將映象掛載到/var/www/html/source7.0上;是為了共享映象資源;必須掛載在/var/www/html/目錄下的子目錄source7.0中,
因為/var/www/html/為apache的預設釋出目錄,而真機火牆已經允許了別人訪問它的apache;這樣才能達到共享的目的
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# mkdir source7.0
[[email protected] html]# cd
##臨時掛載
[[email protected] html]# mount /iso/rhel-server-7.0-x86_64-dvd.iso /var/www/html/source7.0
mount: /dev/loop1 is write-protected, mounting read-only
[[email protected] html]# df

在這裡插入圖片描述

##永久性掛載
[[email protected] ~]# vim /etc/rc.d/rc.local 
###################
mount /iso/rhel-server-7.0-x86_64-dvd.iso /var/www/html/source7.0

在這裡插入圖片描述

##給指令碼一個可執行許可權
[[email protected] ~]# chmod +x /etc/rc.d/rc.local
##測試:
首先是在真機中,檢測自己能否訪問到自己的映象資源
輸入: 172.25.254.66/source7.0/

在這裡插入圖片描述

其次在虛擬機器中,檢測是否能訪問真機的映象資源,即是否達到共享的目錄
輸入: 172.25.254.66/source7.0/

在這裡插入圖片描述
(4).配置客戶端虛擬機器

[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# ls
rhel_dvd.repo
##設定yum源指向
[[email protected] yum.repos.d]# vim rhel7.0.repo
########################
[rhel7.0]
name=rhel7.0
baseurl=http://172.25.254.66/source7.0  # yum源指向;寫的必須是真機共享的掛載目錄(即為/var/www/html目錄下的source7.0子目錄)
gpgcheck=0

在這裡插入圖片描述

##清除yum源歷史快取
[[email protected] yum.repos.d]# yum clean all

在這裡插入圖片描述

##列出yum源的詳細資訊;有報錯
[[email protected] yum.repos.d]# yum repolist

在這裡插入圖片描述

##解決方案:
[[email protected] yum.repos.d]# ls
rhel7.0.repo  rhel_dvd.repo
[[email protected] yum.repos.d]# vim rhel_dvd.repo
########################
enabled=0   #相當於註釋;讓系統不讀取此檔案

在這裡插入圖片描述

##此時便不會報錯
[[email protected] yum.repos.d]# yum clean all

在這裡插入圖片描述

[[email protected] yum.repos.d]# yum repolist

在這裡插入圖片描述

##測試:安裝apache
[[email protected] yum.repos.d]# yum install -y httpd

在這裡插入圖片描述
3.掛載的兩種方式:

(1)臨時性掛載:(重啟後掛載消失)

一般格式:mount 裝置 掛載點
例如:
 mount rhel-server-7.2-x86_64-dvd.iso /source7.2

(2)永久性掛載:(重啟後才會生效)

/etc/rc.d/rc.local 為開機自動執行指令碼;將掛載命令直接寫入此檔案中即可實現永久性掛載
例如:
vim /etc/rc.d/rc.local 
###########
mount rhel-server-7.2-x86_64-dvd.iso /source7.2

##給指令碼一個可執行許可權
chmod +x /etc/rc.d/rc.local
##必須重啟,才能生效
reboot