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 Resolution
Dependencies Resolved
===============================================================================================================
Package Arch Version Repository Size
===============================================================================================================
Installing:
tree x86_64 1.6.0-10.el7 base 46 k
Transaction Summary
===============================================================================================================
Install 1 Package
Total 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 0eb5
From : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 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]#