yum是rpm包的管理工具,可以自動安裝、升級、刪除軟件包的功能,可以自動解決軟件包之間的依賴關系,使得用戶更方便軟件包的管理。要使用yum必須要進行配置,個人將其分為三類,本地yum源、局域網yum源、第三方yum源,其說明配置如下:
1.本地yum源配置
1.1.上傳iso鏡像文件
連接Linux(Centos 7.6),點擊SFTP圖標按鈕彈出FTP傳輸界面,左邊為Windows系統文件目錄,找到對應版本的iso鏡像文件,雙擊或者拖拽上傳鏡像iso文件,下方可以看到百分比長傳進度。
1.2.掛載iso鏡像文件
先創建一個掛載鏡像的目錄,然后使用mount命令將鏡像掛載到該目錄(注:僅只讀掛載)。
[root@localhost ~]# mkdir /mnt/iso
[root@localhost ~]#
[root@localhost ~]# mount -o loop -t iso9660 CentOS-7-x86_64-Everything-1810.iso /mnt/iso
mount: /dev/loop0 is write-protected, mounting read-only
1.3.本地yum源配置文件配置
1.yum源配置文件在/etc/yum.repos.d/目錄下,存在系統自帶的源,此源需要連接外網,就算連接外網也很有可能不能使用,可備份到目錄,或者可以直接刪除(刪除命令:rm -rf CentOS*)。
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
total 32
-rw-r--r--. 1 root root 1664 Nov 23 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23 2018 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Nov 23 2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Nov 23 2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Nov 23 2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23 2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23 2018 CentOS-Vault.repo
[root@localhost yum.repos.d]# mkdir yumbak
[root@localhost yum.repos.d]# mv CentOS-* yumbak/
[root@localhost yum.repos.d]# ll
total 0
drwxr-xr-x. 2 root root 187 Dec 24 18:55 yumbak
?2.創建新本地yum源配置文件。
[iso]:中括號是一個倉庫開始標識
name:yum源倉庫名稱
baseurl:定義yum源倉庫的URL串,可以是本地(file://),可以是遠程(http://)
gpgkey:用于驗證yum源倉庫中包簽名的GPG密鑰的URL串,在掛載目錄/mnt/iso可以看到
gpgcheck:是否啟動對該倉庫下載的包進行GPG簽名檢查,1啟動,0禁用
enabled:是否啟用該倉庫,1啟動,0禁用
priority:優先級,多個倉庫時優先級配置
failovermethod:該參數設置為priority(默認值),表示從baseurl列表中順序選擇鏡像地址,設置為roundrobin表示在baseurl列出中隨機選擇鏡像地址。
注:一個repo配置文件可以配置多個倉庫,每個倉庫以[倉庫名稱]開始,直到下一個[倉庫名稱]前結束,也可以配置多個repo文件。
[root@localhost yum.repos.d]# vi local.repo # vi一個新文件,文件中內容如下
[iso]
name=iso
baseurl=file:///mnt/iso
gpgkey=file:///mnt/iso/RPM-GPG-KEY-CentOS-7
failovermethod=priority
enabled=1
gpgcheck=0
priority=1
1.4.使得yum源生效
1.清空yum源緩存
[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: iso
Cleaning up list of fastest mirrors
2.重新生成yum源緩存,提示Metadata Cache Created標識成功
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
iso | 3.6 kB 00:00:00
(1/4): iso/group_gz | 166 kB 00:00:00
(2/4): iso/filelists_db | 7.1 MB 00:00:00
(3/4): iso/primary_db | 6.0 MB 00:00:00
(4/4): iso/other_db | 2.6 MB 00:00:00
Metadata Cache Created
1.5.yum源安裝試用
1.檢查yum源是否有tree包
[root@localhost yum.repos.d]# yum search tree
... ...
tree.x86_64 : File system tree viewer
... ...
2.檢查Linux(Centos 7.6)系統是否安裝了tree包(沒有任何輸出則沒有安裝該包)
[root@localhost yum.repos.d]# rpm -qa | grep tree
[root@localhost yum.repos.d]#
2.yum源安裝tree包(中間會提示確認信息,輸入y繼續安裝,也可以直接執行yum install -y tree)
[root@localhost yum.repos.d]# yum install tree
... ...
Is this ok [y/d/N]: y # 此處會提示信息
... ...
Installed:tree.x86_64 0:1.6.0-10.el7 Complete!
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# rpm -qa | grep tree # 安裝后檢查本Linux已經安裝tree包
tree-1.6.0-10.el7.x86_64
[root@localhost yum.repos.d]#
2.局域網yum源配置
如果每個Linux(Centos 7.6)都配置本地yum,那么每臺服務器都需要上傳鏡像文件,且都會占用鏡像文件的磁盤空間。所以就算是不可上網的局域網,我們也可以配置局域網內的遠程的yum源。
配置方法是在本地yum源基礎上,使用httpd服務來實現。
2.1.檢查安裝httpd服務
1.查看httpd服務沒有安裝,檢查本地yum源里面是否有httpd包,檢查結果有。
[root@localhost ~]# rpm -qa | grep httpd
[root@localhost ~]#
[root@localhost ~]# yum search httpd
... ...
httpd.x86_64 : Apache HTTP Server
... ...
?2.安裝httpd軟件包
[root@localhost ~]# yum install -y httpd
... ...
Installed:httpd.x86_64 0:2.4.6-88.el7.centos Dependency Installed:apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-88.el7.centos mailcap.noarch 0:2.1.41-2.el7 Complete!
[root@localhost ~]#
?2.2.鏡像掛載后的目錄內容拷貝httpd服務目錄
1.httpd服務的目錄默認是/var/www/html/,查看為空,然后在/var/www/html/目錄下創建iso目錄。
[root@localhost ~]# ll /var/www/html/
total 0
[root@localhost ~]# mkdir /var/www/html/iso
[root@localhost ~]#
2. 將鏡像掛載后的目錄內容拷貝httpd服務目錄下的iso目錄,然后查看/var/www/html/iso/目錄內容
[root@localhost ~]# cp -r /mnt/iso/* /var/www/html/iso
[root@localhost ~]#
[root@localhost ~]# ll /var/www/html/iso/
total 728
-rw-r--r--. 1 root root 14 Dec 24 20:40 CentOS_BuildTag
drwxr-xr-x. 3 root root 35 Dec 24 20:40 EFI
-rw-r--r--. 1 root root 227 Dec 24 20:40 EULA
-rw-r--r--. 1 root root 18009 Dec 24 20:40 GPL
drwxr-xr-x. 3 root root 57 Dec 24 20:40 images
drwxr-xr-x. 2 root root 198 Dec 24 20:40 isolinux
drwxr-xr-x. 2 root root 43 Dec 24 20:40 LiveOS
drwxr-xr-x. 2 root root 557056 Dec 24 20:40 Packages
drwxr-xr-x. 2 root root 4096 Dec 24 20:40 repodata
-rw-r--r--. 1 root root 1690 Dec 24 20:40 RPM-GPG-KEY-CentOS-7
-rw-r--r--. 1 root root 1690 Dec 24 20:40 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 Dec 24 20:40 TRANS.TBL
2.3.啟動httpd服務
[root@localhost ~]# systemctl start httpd
[root@localhost ~]#
2.4.瀏覽器訪問httpd
1.使用能與服務器網絡連通的Windows訪問httpd(http://192.168.118.201/iso/),內容如下
2.或者使用curl 訪問httpd,查看href后的值,可以看出能正常訪問httpd服務里面的目錄及文件。
[root@localhost ~]# curl http://192.168.118.201/iso/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html><head><title>Index of /iso</title></head><body>
<h1>Index of /iso</h1><table><tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[PARENTDIR]"></td><td><a href="/">Parent Directory</a> </td><td> </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[ ]"></td><td><a href="CentOS_BuildTag">CentOS_BuildTag</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> 14 </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="EFI/">EFI/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="EULA">EULA</a> </td><td align="right">2024-12-24 20:40 </td><td align="right">227 </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="GPL">GPL</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> 18K</td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="LiveOS/">LiveOS/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="Packages/">Packages/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[ ]"></td><td><a href="RPM-GPG-KEY-CentOS-7">RPM-GPG-KEY-CentOS-7</a> </td><td align="right">2024-12-24 20:40 </td><td align="right">1.7K</td><td> </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[ ]"></td><td><a href="RPM-GPG-KEY-CentOS-Testing-7">RPM-GPG-KEY-CentOS-T..></a></td><td align="right">2024-12-24 20:40 </td><td align="right">1.7K</td><td> </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[ ]"></td><td><a href="TRANS.TBL">TRANS.TBL</a> </td><td align="right">2024-12-24 20:40 </td><td align="right">2.8K</td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="images/">images/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="isolinux/">isolinux/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="repodata/">repodata/</a> </td><td align="right">2024-12-24 20:40 </td><td align="right"> - </td><td> </td></tr><tr><th colspan="5"><hr></th></tr>
</table>
</body></html>
2.5.修改repo配置文件
1.備份本地yum源配置文件
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 139 Dec 24 19:16 local.repo
drwxr-xr-x. 2 root root 187 Dec 24 18:55 yumbak
[root@localhost yum.repos.d]# mv local.repo yumbak/
[root@localhost yum.repos.d]#
2.編寫http服務的yum源配置
[root@localhost yum.repos.d]# vi netyum.repo # vi一個新文件,文件中內容如下
[iso]
name=iso
baseurl=http://192.168.118.201/iso/
gpgkey=http://192.168.118.201/iso/RPM-GPG-KEY-CentOS-7
failovermethod=priority
enabled=1
gpgcheck=0
priority=1
2.6.局域網yum生效及其使用
1.參考本文1.4和1.5章節
2.同局域網下的其他服務器,怎么使用該yum源呢?
(1).將netyum.repo文件傳到其他同局域網下的服務器的/etc/yum.repos.d目錄。
[root@localhost yum.repos.d]# scp netyum.repo 192.168.118.130:/etc/yum.repos.d
root@192.168.118.130's password:
netyum.repo 100% 162 115.3KB/s 00:00
[root@localhost yum.repos.d]#
(2).同局域網的其他服務器備份其他的yum配置文件
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
total 36
-rw-r--r--. 1 root root 1664 Nov 23 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23 2018 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Nov 23 2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Nov 23 2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Nov 23 2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23 2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23 2018 CentOS-Vault.repo
-rw-r--r--. 1 root root 162 Dec 24 13:10 netyum.repo
[root@localhost yum.repos.d]# mkdir yumbak
[root@localhost yum.repos.d]# mv CentOS-* yumbak/
[root@localhost yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 162 Dec 24 13:10 netyum.repo
drwxr-xr-x. 2 root root 187 Dec 24 13:12 yumbak
[root@localhost yum.repos.d]#
?(3).同局域網的其他服務器就可以使用該源了
[root@localhost yum.repos.d]# yum clean all # 清除緩存
Loaded plugins: fastestmirror, langpacks
Cleaning repos: iso
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# yum makecache # 生成新緩存
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
iso | 3.6 kB 00:00:00
(1/4): iso/group_gz | 166 kB 00:00:00
(2/4): iso/primary_db | 6.0 MB 00:00:00
(3/4): iso/other_db | 2.6 MB 00:00:00
(4/4): iso/filelists_db | 7.1 MB 00:00:00
Metadata Cache Created
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# yum search tree # 查看此源是否有tree包
... ...
tree.x86_64 : File system tree viewer
... ...
3.第三方yum源配置
所謂第三方yum源,是指在外網(Internet網絡)上有一些大公司或者教育學校配置的yum源,只需要將他們的URL鏈接添加到我們的yum配置文件里面,或者將其repo文件下載后放到/etc/yum.repos.d下,然后就可以使用。
注:該方式需要我們的服務器能連接外網(Internet網絡),可以通過ping www.baidu.com來檢查是否可連接外網。
3.1.備份repo配置文件,檢查外網是否可達
[root@localhost yum.repos.d]# mv netyum.repo yumbak/
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# ping www.baidu.com
PING www.a.shifen.com (183.2.172.185) 56(84) bytes of data.
64 bytes from 183.2.172.185 (183.2.172.185): icmp_seq=1 ttl=128 time=54.8 ms
64 bytes from 183.2.172.185 (183.2.172.185): icmp_seq=2 ttl=128 time=44.5 ms
64 bytes from 183.2.172.185 (183.2.172.185): icmp_seq=3 ttl=128 time=63.8 ms
64 bytes from 183.2.172.185 (183.2.172.185): icmp_seq=4 ttl=128 time=69.3 ms
64 bytes from 183.2.172.185 (183.2.172.185): icmp_seq=5 ttl=128 time=63.8 ms
3.2.下載第三方yum源repo配置文件
下載后的配置文件為CentOS-Base.repo
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2024-12-24 13:33:06-- https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 171.220.246.253, 118.112.232.80, 117.24.169.183, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|171.220.246.253|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’100%[=====================================================================>] 2,523 --.-K/s in 0s 2024-12-24 13:33:11 (191 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523][root@localhost yum.repos.d]# ll
total 4
-rw-r--r--. 1 root root 2523 Aug 4 2022 CentOS-Base.repo
drwxr-xr-x. 2 root root 206 Dec 24 13:27 yumbak
[root@localhost yum.repos.d]#
3.3.第三方yum源使用
1.清除緩存,并生成新的緩存
[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Other repos take up 112 M of disk space (use --verbose for details)
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/10): base/7/x86_64/group_gz | 153 kB 00:00:04
(2/10): extras/7/x86_64/filelists_db | 305 kB 00:00:05
(3/10): extras/7/x86_64/other_db | 154 kB 00:00:00
(4/10): base/7/x86_64/other_db | 2.6 MB 00:00:05
(5/10): updates/7/x86_64/filelists_db | 15 MB 00:00:05
(6/10): updates/7/x86_64/other_db | 1.6 MB 00:00:00
base/7/x86_64/filelists_db FAILED
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/d6d94c7d406fe7ad4902a97104b39a0d8299451832a97f31d71653ba982c955b-filelists.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
base/7/x86_64/primary_db FAILED
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused"
Trying other mirror.
(7/10): base/7/x86_64/filelists_db | 7.2 MB 00:00:03
extras/7/x86_64/primary_db FAILED 854 kB/s | 27 MB 00:00:40 ETA
http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/e12dbf10e94bc2b33b1f45e026559bc8685728b139dddae0654d96bc624c5602-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
(8/10): extras/7/x86_64/primary_db | 253 kB 00:00:00
updates/7/x86_64/primary_db FAILED 947 kB/s | 28 MB 00:00:35 ETA
http://mirrors.aliyuncs.com/centos/7/updates/x86_64/repodata/f19044932626155f0cd849e88972b84875fc85e3308b4d622844a911c4ef54d0-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
(9/10): base/7/x86_64/primary_db | 6.1 MB 00:00:09
(10/10): updates/7/x86_64/primary_db | 27 MB 00:00:12
Metadata Cache Created
2.安裝tree軟件包
[root@localhost yum.repos.d]# rpm -qa | grep tree
[root@localhost yum.repos.d]# yum install -y tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency ResolutionDependencies Resolved===============================================================================================================Package Arch Version Repository Size
===============================================================================================================
Installing:tree x86_64 1.6.0-10.el7 base 46 kTransaction Summary
===============================================================================================================
Install 1 PackageTotal download size: 46 k
Installed size: 87 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/tree-1.6.0-10.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for tree-1.6.0-10.el7.x86_64.rpm is not installed
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:04
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5From : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : tree-1.6.0-10.el7.x86_64 1/1 Verifying : tree-1.6.0-10.el7.x86_64 1/1 Installed:tree.x86_64 0:1.6.0-10.el7 Complete!
[root@localhost yum.repos.d]# rpm -qa | grep tree
tree-1.6.0-10.el7.x86_64
[root@localhost yum.repos.d]#