hugo-teek is loading...

yum

最后更新于:

yum命令

目录

[toc]

1、查询

案例:列出所有可用软件包 yum list

这会列出系统中所有可用的软件包,包括已安装和未安装的。

1yum list

说明:

1#说明:通过yum list输出可以看出,某个软件包是否已安装在机器上:
2	前面有@代表已安装,无此符号的话就代表没安装;

案例:列出所有可用的软件包及其版本信息 yum list available

这会列出系统中所有可用的软件包及其版本信息,包括已安装和未安装的。

1yum list available

  • 案例:查看可用的rpm包
1# 查看可用的rpm包
2yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

案例:查询本地机器已安装的软件包 yum list installed

1yum list installed

案例:找出某个特定软件包的详细信息 yum list <package_name>

1yum list <package_name>
 1[root@docker ~]#yum list lrzsz
 2已加载插件:fastestmirror, langpacks
 3Loading mirror speeds from cached hostfile
 4 * base: mirrors.aliyun.com
 5 * elrepo: hkg.mirror.rackspace.com
 6 * epel: mirror.nju.edu.cn
 7 * extras: mirrors.aliyun.com
 8 * updates: mirrors.aliyun.com
 9已安装的软件包
10lrzsz.x86_64                                                               0.12.20-36.el7                                                               @base

案例:查看某个包的描述信息 yum info <包名>

 1[root@docker ~]#yum info lrzsz
 2已加载插件:fastestmirror, langpacks
 3Loading mirror speeds from cached hostfile
 4 * base: mirrors.aliyun.com
 5 * elrepo: hkg.mirror.rackspace.com
 6 * epel: mirror.nju.edu.cn
 7 * extras: mirrors.aliyun.com
 8 * updates: mirrors.aliyun.com
 9已安装的软件包
10名称    :lrzsz
11架构    :x86_64
12版本    :0.12.20
13发布    :36.el7
14大小    :181 k
15源    :installed
16来自源:base
17简介    : The lrz and lsz modem communications programs
18网址    :http://www.ohse.de/uwe/software/lrzsz.html
19协议    : GPLv2+
20描述    : Lrzsz (consisting of lrz and lsz) is a cosmetically modified
21         : zmodem/ymodem/xmodem package built from the public-domain version of
22         : the rzsz package. Lrzsz was created to provide a working GNU
23         : copylefted Zmodem solution for Linux systems.
24
25[root@docker ~]#

案例:列出所有可用软件包中包含特定关键字的软件包 yum list *<keyword>*

1yum list *<keyword>*

这会列出包含特定关键字的所有可用软件包。替换 <keyword> 为你要搜索的关键字。

案例:查询包依赖和被依赖(已解决

1、依赖哪些软件包

1rpm   -qR  postfix      # 查postfix 依赖哪些包,R参数的意思就是requires就是依赖哪些软件包
2或者
3yum deplist postfix   # 查postfix 依赖哪些包

2、被哪些包依赖

1rpm -e --test rpcbind # 通过--test进行测试删除,查看是否有依赖关系,如果有会阻止删除

案例测试:查询包依赖和被依赖

1、我想知道libseccomp软件包被哪些包依赖?

1[root@master1 ~]#rpm -qa | grep libseccomp
2libseccomp-2.3.1-4.el7.x86_64
3[root@master1 ~]#rpm -e --test libseccomp
4error: Failed dependencies:
5	libseccomp.so.2()(64bit) is needed by (installed) chrony-3.4-1.el7.x86_64 

可以看到,libseccomp只有被chrony对其有依赖。

2、我想知道chrony依赖的包有哪些?

 1[root@master1 ~]#rpm -qa|grep chrony
 2chrony-3.4-1.el7.x86_64
 3
 4# 方法1:
 5[root@master1 ~]#rpm   -qR chrony
 6/bin/bash
 7/bin/sh
 8/bin/sh
 9/bin/sh
10/bin/sh
11/bin/sh
12config(chrony) = 3.4-1.el7
13libc.so.6()(64bit)
14libc.so.6(GLIBC_2.12)(64bit)
15libc.so.6(GLIBC_2.14)(64bit)
16libc.so.6(GLIBC_2.15)(64bit)
17libc.so.6(GLIBC_2.17)(64bit)
18libc.so.6(GLIBC_2.2.5)(64bit)
19libc.so.6(GLIBC_2.3)(64bit)
20libc.so.6(GLIBC_2.3.4)(64bit)
21libc.so.6(GLIBC_2.4)(64bit)
22libcap.so.2()(64bit)
23libedit.so.0()(64bit)
24libfreebl3.so()(64bit)
25libfreebl3.so(NSSRAWHASH_3.12.3)(64bit)
26libm.so.6()(64bit)
27libm.so.6(GLIBC_2.2.5)(64bit)
28libpthread.so.0()(64bit)
29libpthread.so.0(GLIBC_2.2.5)(64bit)
30libseccomp.so.2()(64bit)
31rpmlib(CompressedFileNames) <= 3.0.4-1
32rpmlib(FileDigests) <= 4.6.0-1
33rpmlib(PayloadFilesHavePrefix) <= 4.0-1
34rtld(GNU_HASH)
35shadow-utils
36systemd
37systemd
38systemd
39rpmlib(PayloadIsXz) <= 5.2-1
40
41方法2:
42[root@master1 ~]#yum deplist chrony
43Loaded plugins: fastestmirror
44Loading mirror speeds from cached hostfile
45 * base: mirrors.aliyun.com
46 * extras: mirrors.aliyun.com
47 * updates: mirrors.aliyun.com
48package: chrony.x86_64 3.4-1.el7
49  dependency: /bin/bash
50   provider: bash.x86_64 4.2.46-35.el7_9
51  dependency: /bin/sh
52   provider: bash.x86_64 4.2.46-35.el7_9
53  dependency: libc.so.6(GLIBC_2.17)(64bit)
54   provider: glibc.x86_64 2.17-326.el7_9
55  dependency: libcap.so.2()(64bit)
56   provider: libcap.x86_64 2.22-11.el7
57  dependency: libedit.so.0()(64bit)
58   provider: libedit.x86_64 3.0-12.20121213cvs.el7
59  dependency: libfreebl3.so()(64bit)
60   provider: nss-softokn-freebl.x86_64 3.79.0-4.el7_9
61  dependency: libfreebl3.so(NSSRAWHASH_3.12.3)(64bit)
62   provider: nss-softokn-freebl.x86_64 3.79.0-4.el7_9
63  dependency: libm.so.6()(64bit)
64   provider: glibc.x86_64 2.17-326.el7_9
65  dependency: libm.so.6(GLIBC_2.2.5)(64bit)
66   provider: glibc.x86_64 2.17-326.el7_9
67  dependency: libpthread.so.0()(64bit)
68   provider: glibc.x86_64 2.17-326.el7_9
69  dependency: libpthread.so.0(GLIBC_2.2.5)(64bit)
70   provider: glibc.x86_64 2.17-326.el7_9
71  dependency: libseccomp.so.2()(64bit)
72   provider: libseccomp.x86_64 2.3.1-4.el7
73  dependency: rtld(GNU_HASH)
74   provider: glibc.x86_64 2.17-326.el7_9
75   provider: glibc.i686 2.17-326.el7_9
76  dependency: shadow-utils
77   provider: shadow-utils.x86_64 2:4.6-5.el7
78  dependency: systemd
79   provider: systemd.x86_64 219-78.el7_9.7

案例:处理损坏的包依赖关系

有时在安装多个软件包时,某个包的软件依赖关系可能会被另一个包的安装覆盖掉。这叫作损坏的包依赖关系(broken dependency)

1.yum clean all

1如果系统出现了这个问题,先试试下面的命令:
2yum clean all 
3
4然后试着用yum命令的update选项。有时,只要清理了放错位置的文件就可以了。

2.yum deplist package_name 查看某个软件包依赖了哪些包

 1如果这还解决不了问题,试试下面的命令:
 2yum deplist package_name 
 3这个命令显示了某个软件包的all库依赖关系以及什么软件可以提供这些库依赖关系。一旦知道某个包需要的库,你就能安装它们了。下面是确定xterm包依赖关系的例子。
 4
 5# yum deplist xterm 
 6Loaded plugins: fastestmirror, refresh-packagekit, security 
 7Loading mirror speeds from cached hostfile 
 8 * base: mirrors.bluehost.com 
 9 * extras: mirror.5ninesolutions.com 
10 * updates: mirror.san.fastserv.com 
11Finding dependencies: 
12package: xterm.i686 253-1.el6 
13 dependency: libncurses.so.5 
14 provider: ncurses-libs.i686 5.7-3.20090208.el6 
15 dependency: libfontconfig.so.1 
16 provider: fontconfig.i686 2.8.0-3.el6 
17 dependency: libXft.so.2 
18 provider: libXft.i686 2.3.1-2.el6 
19 dependency: libXt.so.6 
20 provider: libXt.i686 1.1.3-1.el6 
21 dependency: libX11.so.6 
22 provider: libX11.i686 1.5.0-4.el6 
23 dependency: rtld(GNU_HASH) 
24 provider: glibc.i686 2.12-1.132.el6 
25 provider: glibc.i686 2.12-1.132.el6_5.1 
26 provider: glibc.i686 2.12-1.132.el6_5.2 
27 dependency: libICE.so.6 
28 provider: libICE.i686 1.0.6-1.el6 
29 dependency: libXaw.so.7
30  provider: libXaw.i686 1.0.11-2.el6 
31 dependency: libtinfo.so.5 
32 provider: ncurses-libs.i686 5.7-3.20090208.el6 
33 dependency: libutempter.so.0 
34 provider: libutempter.i686 1.1.5-4.1.el6 
35 dependency: /bin/sh 
36 provider: bash.i686 4.1.2-15.el6_4 
37 dependency: libc.so.6(GLIBC_2.4) 
38 provider: glibc.i686 2.12-1.132.el6 
39 provider: glibc.i686 2.12-1.132.el6_5.1 
40 provider: glibc.i686 2.12-1.132.el6_5.2 
41 dependency: libXmu.so.6 
42 provider: libXmu.i686 1.1.1-2.el6

3.yum update –skip-broken

1如果这样仍未解决问题,还有最后一招:
2yum update --skip-broken 
3#--skip-broken选项允许你忽略依赖关系损坏的那个包,继续去更新其他软件包。这可能救不了损坏的包,但至少可以更新系统上的其他包。

TS:yum安装软件包失败的案例,因需安装的服务的依赖包版本不一致导致安装服务失败问题

本地yum源没问题:

img

故障原因: 因为本身centos7.4带的httpd-tools版本是67的,而之前应该是源码安装了88版本的,导致httpd安装一直失败。。。。。

img

img

解决办法: 先卸载httpd,再安装httpd [root@ZXL~]# yum remove -y http

img

完美解决。

案例:查找仓库里某个软件包所有的版本 --showduplicates

  • 查找yum源中最新版本号:yum list --showduplicates kubeadm --disableexcludes=kubernetes
11、查找最新版本号
2yum list --showduplicates kubeadm --disableexcludes=kubernetes #--disableexcludes=kubernetes,只使用kubernetes仓库;
3
4#
5  --showduplicates      在 list/search 命令下,显示源里重复的条目

img

案例:需要找出系统上的某个命令属于哪个软件包 yum provides 命令

 1最后,如果需要找出系统上的某个特定文件属于哪个软件包,万能的yum可以做到!只要输入命令:
 2
 3[root@docker ~]#yum provides sz
 4已加载插件:fastestmirror, langpacks
 5Loading mirror speeds from cached hostfile
 6 * base: mirrors.aliyun.com
 7 * elrepo: hkg.mirror.rackspace.com
 8 * epel: mirror.nju.edu.cn
 9 * extras: mirrors.aliyun.com
10 * updates: mirrors.aliyun.com
11lrzsz-0.12.20-36.el7.x86_64 : The lrz and lsz modem communications programs
12源    :base
13匹配来源:
14文件名    :/usr/bin/sz
15
16
17
18lrzsz-0.12.20-36.el7.x86_64 : The lrz and lsz modem communications programs
19源    :@base
20匹配来源:
21文件名    :/usr/bin/sz
22
23
24
25[root@docker ~]#

🔰 扩展

1ifconfig和netstat命令的软件包均是net-tools软件包;

2、安装

yum的优点之一就是它的命令富有逻辑性,而且对用户也友好。

案例:yum安装软件包 yum install -y <软件包>

1下面这个简单的命令会从仓库中安装软件包、所有它需要的库以及依赖的其他包:
2yum install package_name -y

🔰 说明:

备注:-y选项在前在后(前后都可以);软件包是否标注.x86_64(不用标注);可并列写需要安装的软件包名。

 1# -y在前在后都行:
 2[zxl@foryou ~]$ yum -y install httpd
 3[zxl@foryou ~]$ yum install httpd -y
 4
 5
 6# 可以不带.x86_64
 7yum install -y gcc  pam.x86_64
 8或者
 9yum install -y gcc  pam dhcp ntp telnet
10
11
12# yum安装时可以并行写着的:
13yum install -y gcc pam.x86_64 pam-devel.x86_64 krb5-devel.x86_64
14yum -y install gcc gcc-c++ kernel-devel //安装gcc、c++编译器以及内核文件 --可以放在同一行

案例:手动下载rpm安装文件并用yum安装 yum localinstall *.rpm -y

1rpm安装包下载完成后,以下命令在下载的软件包目录下执行,这里不使用rpm -ivh安装时因为使用yum localinstall安装可以解决一些依赖关系,开始安装:
2
3[root@localhost ansible]# yum localinstall *.rpm -y

img

img

img

案例:yum仅下载不安装软件包命令

通常是使用yum来安装解决依赖包关系,如果有一台服务器没法连接外网或yum源没有设置,希望通过另一台服务器将这些RPM包下载下来,然后再去安装。那么怎么使用yum工具来下载RPM包呢?–使用如下命令即可。

1yum install -y ansible --downloadonly --downloaddir=ansible

例子:yum groupinstalll “xxx”命令

 1新安装完机器后,你可能需要安装一些必须的包,比如你要做开发,可能要安装gcc,cmake, glibc之类的;比如说你要做web server,需要安装apache或者httpd,mysql,php之类的。
 2
 3有时候你可能忘了安装一些依赖的包,导致安装失败,不得不先去安装依赖包。
 4
 5yum提供了一个很强大的功能,groupinstall
 6通过这个功能可以让你非常方便的一次过安装你所需要的包,
 7
 8比如你安装必要的开发工具包:
 9yum groupinstalll “Development Tools”
10记得不要丢了双引号。
11
12如果是安装web server必要的包:
13yum groupinstall “Web Server”
14
15如果还想知道还有哪些group可以使用grouplist
16yum grouplist

例子:--disableexcludes选项

1[root@master1 ~]yum makecache fast
2[root@master1 ~]yum install -y kubelet-1.25.4 kubeadm-1.25.4 kubectl-1.25.4 --disableexcludes=kubernetes
3[root@master1 ~]#kubeadm version
4kubeadm version: &version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:35:06Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"linux/amd64"}
5
6
7#可以看到我们这里安装的是 v1.25.4 版本,然后将 master 节点的 kubelet 设置成开机启动:
8[root@master1 ~]#systemctl enable --now kubelet

说明:–disableexcludes 禁掉除了kubernetes之外的别的仓库。

🔰 注意:

1yum install -y kubeadm-1.25.4 --disableexcludes=kubernetes
2
3等价于
4
5yum install -y --disablerepo=\* --enablerepo=kubernetes kubeadm-1.25.4 

3、更新

案例:列出所有已安装包的可用更新 yum list updates == yum check-update

1yum list updates == yum check-update

img

案例:更新某个软件包 yum updates package_name

1yum updates package_name

(1)先查看某个软件包是否有新版本?

1[root@GitLab ~]# yum list unzip
2或者
3[root@GitLab ~]# yum list updates|grep unzip
4
5#备注:带有@表示已安装软件包;

img

(2)再利用yum update package_name进行升级安装包

1[root@GitLab ~]# yum update unzip -y

img

(3)验证

img

案例:更新操作系统、内核、软件包

1yum update -y
2yum upgrade -y

yum update和yum upgrade命令有什么区别?

在使用yum这个包管理器时,yum updateyum upgrade命令在大多数情况下是等效的,它们都会更新所有已安装的软件包到最新的可用版本。但是,有一个细微的差别:

  • yum update:这个命令会更新所有已安装的包,并且会处理依赖关系,以确保系统稳定理论上,它不会删除任何包
  • yum upgrade:这个命令除了执行yum update的所有操作外,还会删除过时的包。过时的包指的是那些在仓库中已经被替换掉的包。这意味着某些包可能会被他们的新版本所替代,而旧版本则会被删除。

实际上,在早期的yum版本中,yum upgrade命令是用来表示更彻底的系统升级,它会考虑删除那些已经不再仓库中维护的包。但在后来的版本中,这两个命令的差异已经被最小化了。

为了确保系统稳定,一般推荐使用yum update如果你想进行一个彻底升级并且移除不再需要的包,可以使用yum upgrade。在具体使用之前,查阅特定系统版本的yum文档是一个好习惯,以确认两者之间是否有特殊的差异处理。

个人使用推荐:

一般保持centos mini版本就行,不需要执行yum update命令!

案例:生成yum缓存 yum makecache

1yum makecache

案例:载入elrepo-kernel元数据

1yum --disablerepo=\* --enablerepo=elrepo-kernel repolist

案例:降级某个安装包 downgrade选项

1yum downgrade kubectl-1.20.0-0.x86_64 \
2kubeadm-1.20.0-0.x86_64 \
3kubelet-1.20.0-0.x86_64

image-20230609063541014

image-20230609063709495

image-20230609063723947

4、卸载

注意:yum卸载命令是不需要yum源的;

注意:yum remove/erase 删除一个软件的时候也会删除对该软件具有依赖关系的包。

1方法1:
2yum remove package_name -y #只删除软件包而保留配置文件和数据文件,就用如下命令:
3
4方法2:
5yum erase package_name #要删除软件和它所有的文件,就用erase选项:

🍀 案例测试:yum remove/erase 删除一个软件的时候也会删除对该软件具有依赖关系的包。(符合预期)

1yum -y remove ntp # 删除系统自带ntp
2#进行测试:——验证成功,说法正确。
3#当前虚机环境:centos74

img

这个ntpdate是ntp的依赖包:

img

如果删除了这个依赖包的话,用到这个包的软件均会被删除:

img

img

注意:

1#注意1: rpm -e --nodeps PACKAGE_NAME    #不考虑依赖包单独删除某个包

img

5、搭建yum仓库

1、Centos7配置本地yum源服务

1.挂载本地镜像

1mount /dev/cdrom /mnt  # 物理机或虚机通过cd-rom挂载的iso
2或者
3mount /iso路径  /mnt  # 这种情况是彻底把iso传到系统里去了

2.编写yum配置文件base.repo

 1cd /etc/yum.repos.d/
 2mkdir backup
 3mv * backup/
 4
 5cat > /etc/yum.repos.d/base.repo << EOF          
 6[base]
 7name=local_yum
 8baseurl=file:///mnt
 9enabled=1
10gpgcheck=0                          
11EOF

🍀 说明:

1[name]               #括号中的名称为仓库源名称,通常为字母和数字,必须填写
2name=my new repo     # 对yum的描述,可写可不写
3baseurl=file:///mnt/cdrom    # baseurl表示声明yum可以管理并使用的rpm包路径,必须填写
4enabled=1            # enabled表示当前仓库是否开启,1为开启,0为关闭,此项不写默认为开启
5gpgcheck=0           # gpgcheck表示安装rpm包时,是否基于公私钥对匹配包的安全信息,1表示开启,0表示关闭,此项不写默认为验证

🍀 注意:这里的gpgcheck=0选项

 1cd /etc/yum.repos.d/
 2vi base.repo
 3内容如下:
 4[base]
 5name=local yum
 6baseurl=file:///mac
 7gpgcheck=1 #其实没必要加这个gpgcheck=1的。。。,一般按自己的书写方式就行:
 8gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
 9
10[root@zxl_ecs ~]$ls /etc/pki/rpm-gpg/     #注意下这个校验方式:
11RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Debug-7  RPM-GPG-KEY-CentOS-Testing-7  RPM-GPG-KEY-EPEL-7

3.验证

1yum clean all # 清空缓存信息。 
2yum makecache fast # 生成缓存信息
3
4yum repolist all # 验证
5或者:
6yum list | wc -l  统计列出所有包的数量,如果列出了,说明yum源配置完成。

🍀 vmwareworkstation虚机中的光盘图标

img

img

img

🍀 ISO 一次性/永久性挂载:/etc/fastab

一: 一次性挂载,重启完你系统后失效

1mount /dev/sr0  /mnt/cdrom

img

二: 永久挂载

我们的设备的挂载信息在 /etc/fstab 里面,所以我们要想永久挂载,需要修改 fstab 文件。用 vim 打开该文件,我们可以看到有三行信息,是其他分区的挂载信息,所以我们现在要把我们的光盘的挂载信息添加过去。

img

添加如下我们光盘的挂载信息过去

1/dev/sr0     /mnt/cdrom    iso9660  defaults   0   0

img

添加完后,保存退出;

然后执行 mount -a 。这个命令的意思是挂载fstab文件中的所有文件系统。可以看到,我们已经挂载好了;

img

备注:

在虚机系统中,我们的 iso 文件是 /dev/sr0 设备,系统会默认把我们的iso文件挂载到 /run/media/$username/ 目录下。

(也许可能出现在sr1上。物理机情况也相似)

img

代码汇总

 1mount /dev/cdrom /mnt
 2cd /etc/yum.repos.d/
 3mkdir backup
 4mv * backup/
 5
 6cat > /etc/yum.repos.d/base.repo << EOF          
 7[base]
 8name=local_yum
 9baseurl=file:///mnt
10enabled=1
11gpgcheck=0                          
12EOF
13
14yum clean all # 清空缓存信息。 
15yum makecache fast # 生成缓存信息
16
17yum repolist all # 验证

案例:访问misc/cd下面的本地光盘文件

1#这个看起来还是很666的,新鲜的yum方式;
2
3[root@centos76 yum.repos.d]#yum install -y autofs
4[root@centos76 ~]#systemctl enable --now autofs
5然后yum直接配置file:///misc/cd /,就可以挂载本地光盘了。

img

img

img

2、Centos7基于ftp服务的网络yum源的搭建

测试环境

2台vmworkstation Centos7.6虚机。

1.服务器端配置

(1) 安装ftp服务

1[root@ftp-yum-server ~]#yum install -y vsftpd  # 可以利用本地源或者网络yum源进行httpd软件的安装

(2) 配置基础服务环境,开启httpd服务并设置开机启动

 1# 关闭防火墙/NetwokManager/selinux
 2systemctl stop firewalld
 3systemctl disable  firewalld
 4
 5systemctl stop NetworkManager
 6systemctl disable  NetworkManager
 7
 8setenforce 0
 9sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config 
10
11# 开启ftp服务并设置开机启动
12systemctl restart vsftpd  
13systemctl enable vsftpd
14
15# ftp服务启动成功后,可以利用systemctl status vsftpd查看其状态:

img

(3) 创建相应iso镜像文件

1将ISO中的内容拷贝到/var/ftp/pub/目录下。
2注意:vsftpd服务的/是在/var/ftp/pub目录下的。
3
4[root@ftp-yum-server ~]#cd /var/ftp/pub/
5[root@ftp-yum-server pub]#ls
6[root@ftp-yum-server pub]#mkdir centos761810
7[root@ftp-yum-server pub]#cp -rp /mnt/* /var/ftp/pub/centos761810/

img

(4) 此时在宿主机上打开浏览器,就能够看到yum源文件了

ftp://10.10.10.135

注意:vsftpd服务的/是在/var/ftp/pub目录下的。

img

img

2.客户端测试

注意:客户端是不需要安装vsftpd服务的,但要保证客户端和服务端可互访。

1[root@client yum.repos.d]#systemctl status vsftpd
2Unit vsftpd.service could not be found.
3[root@client yum.repos.d]#

客户端配置ftp yum文件:

 1cat >> /etc/yum.repos.d/ftp.repo << EOF
 2[centos-source]
 3name=centos source
 4baseurl=ftp://10.10.10.135/pub/centos761810
 5enabled=1
 6gpgcheck=0
 7EOF
 8
 9yum clean all
10yum makecache
11yum repolist all

img

测试是否可以正常安装软件:

img

可以正常安装软件,实验结束。

3、Centos7基于httpd服务的网络yum源的搭建

测试环境

2台vmworkstation Centos7.6虚机。

1.服务器端配置

(1)安装httpd服务

1[root@http-yum-server ~]#yum install -y httpd  # 可以利用本地源或者网络yum源进行httpd软件的安装

(2)配置基础服务环境,开启httpd服务并设置开机启动

 1# 关闭防火墙/NetwokManager/selinux
 2systemctl stop firewalld
 3systemctl disable  firewalld
 4
 5systemctl stop NetworkManager
 6systemctl disable  NetworkManager
 7
 8setenforce 0
 9sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config 
10
11# 开启httpd服务并设置开机启动
12systemctl restart httpd  # 重启httpd服务可能稍微需要点时间
13systemctl enable httpd
14
15# httpd服务启动成功后,可以利用systemctl status httpd查看其状态:

img

(3)创建相应iso镜像文件

把相应iOS版本镜像内容copy到/var/www/html/centos7yum目录下相应目录(先mkdir出centos7yum目录及相应iso版本目录)。Httpd默认发布目录是/var/www/html/,相当于根目录,所有共享的文件都要放在这个目录里。在/etc/httpd/conf/httpd.conf文件可以查看到。

1[root@http-yum-server ~]#cd /var/www/html/
2[root@http-yum-server html]#ls
3[root@http-yum-server html]#mkdir -p centos7yum/centos761810
4[root@http-yum-server html]#cp -rp /mnt/* /var/www/html/centos7yum/centos761810/

img

1注意:httpd服务的配置文件/etc/httpd/conf/httpd.conf,这里默认不变。
2[root@http-yum-server ~]#vim /etc/httpd/conf/httpd.conf

img

img

注意这边如果要更改httpd服务的端口后的话,可以从这里设置:

1vim /etc/httpd/conf/httpd.conf #修改如下80为8875

img

重启httpd服务:

img

(4)此时在宿主机上打开浏览器,就能够看到yum源文件了 http://10.10.10.135/centos7yum/centos761810

img

img

2.客户端测试

1注意:客户端是不需要安装httpd服务的,但要保证客户端和服务端可互访。
2[root@client yum.repos.d]#systemctl status httpd
3Unit httpd.service could not be found.
4[root@client yum.repos.d]#

客户端配置httpd yum文件:

 1cat >> /etc/yum.repos.d/http.repo << EOF
 2[centos-source]
 3name=centos source
 4baseurl=http://10.10.10.135/centos7yum/centos761810
 5enabled=1
 6gpgcheck=0
 7EOF
 8
 9yum clean all
10yum makecache
11yum repolist all

img

测试是否可以正常安装软件:

img

可以正常安装软件,实验结束。

🍀 总结

利用httpd服务搭建yum其实是很简单的,但httpd知识还需要更深层次去学习;

貌似httpd软件漏扫很多,因此如果利用httpd服务提供yum服务的话,可能后续要利用源码安装httpd软件。

4、实战:Centos7配置公网yum源服务(测试成功)-2022.8.4

CentOS默认的yum源不是国内镜像,导致yum在线安装及更新速度不是很理想。这时候需要将yum源设置为国内镜像站点。国内主要开源的开源镜像站点应该是网易和阿里云了。

1.备份

1把/etc/yum.repos.d/下的所有文件备份起来:
2cd /etc/yum.repos.d/
3mkdir backup
4mv * backup/

2.wegt下载网络repo文件

 1# 备注:centos7 6 5版本的阿里云yum配置文件
 2# 前提是系统有安装wget软件包
 3
 4CentOS7
 5[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 6
 7CentOS6
 8[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
 9
10CentOS5
11[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
12
13#或者利用curl命令:
14curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
15curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
16
17#或者使用163源:
18wget -O CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo

3.验证

1# 清理yum缓存
2yum clean all
3
4# 重建缓存
5yum makecache# -->建立一个缓存,以后用install安装服务的时候就在缓存中搜索,提高了速度
6
7#执行yum repolist all命令查看结果
8yum repolist all

img

FAQ

yum是依赖于python2的

案例:前端前台构建需要依赖python3环境-2024.8.16(已解决)

1、报错现象

  1[root@localhost kuailemao-blog]#     pnpm install
  2Lockfile is up to date, resolution step is skipped
  3Packages: +742
  4+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5
  6   ╭─────────────────────────────────────────────────────────────────╮
  7   │                                                                 │
  8   │                Update available! 8.12.0 → 9.7.1.                │
  9   │   Changelog: https://github.com/pnpm/pnpm/releases/tag/v9.7.1   │
 10   │                Run "pnpm add -g pnpm" to update.                │
 11   │                                                                 │
 12   │     Follow @pnpmjs for updates: https://twitter.com/pnpmjs      │
 13   │                                                                 │
 14   ╰─────────────────────────────────────────────────────────────────╯
 15
 16Downloading mirrors.huaweicloud.com/element-plus/2.7.6: 8.71 MB/8.71 MB, done
 17Downloading mirrors.huaweicloud.com/echarts/5.5.1: 10.36 MB/10.36 MB, done
 18Progress: resolved 742, reused 0, downloaded 742, added 742, done
 19node_modules/.pnpm/vue-demi@0.14.8_vue@3.4.31/node_modules/vue-demi: Running postinstall script, done in 220ms
 20node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild: Running postinstall script, done in 101ms
 21node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass: Running install script, done in 22s
 22node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass: Running postinstall script, failed in 301ms
 23.../node_modules/node-sass postinstall$ node scripts/build.js
 24│ Building: /usr/local/nodejs/bin/node /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= -…
 25│ gyp info it worked if it ends with ok
 26│ gyp verb cli [
 27│ gyp verb cli   '/usr/local/nodejs/bin/node',
 28│ gyp verb cli   '/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js',
 29│ gyp verb cli   'rebuild',
 30│ gyp verb cli   '--verbose',
 31│ gyp verb cli   '--libsass_ext=',
 32│ gyp verb cli   '--libsass_cflags=',
 33│ gyp verb cli   '--libsass_ldflags=',
 34│ gyp verb cli   '--libsass_library='
 35│ gyp verb cli ]
 36│ gyp info using node-gyp@8.4.1
 37│ gyp info using node@16.20.2 | linux | x64
 38│ gyp verb command rebuild []
 39│ gyp verb command clean []
 40│ gyp verb clean removing "build" directory
 41│ gyp verb command configure []
 42│ gyp verb find Python Python is not set from command line or npm configuration
 43│ gyp verb find Python Python is not set from environment variable PYTHON
 44│ gyp verb find Python checking if "python3" can be used
 45│ gyp verb find Python - executing "python3" to get executable path
 46│ gyp verb find Python - "python3" is not in PATH or produced an error
 47│ gyp verb find Python checking if "python" can be used
 48│ gyp verb find Python - executing "python" to get executable path
 49│ gyp verb find Python - executable path is "/usr/bin/python"
 50│ gyp verb find Python - executing "/usr/bin/python" to get version
 51│ gyp verb find Python - version is "2.7.5"
 52│ gyp verb find Python - version is 2.7.5 - should be >=3.6.0
 53│ gyp verb find Python - THIS VERSION OF PYTHON IS NOT SUPPORTED
 54│ gyp ERR! find Python 
 55│ gyp ERR! find Python Python is not set from command line or npm configuration
 56│ gyp ERR! find Python Python is not set from environment variable PYTHON
 57│ gyp ERR! find Python checking if "python3" can be used
 58│ gyp ERR! find Python - "python3" is not in PATH or produced an error
 59│ gyp ERR! find Python checking if "python" can be used
 60│ gyp ERR! find Python - executable path is "/usr/bin/python"
 61│ gyp ERR! find Python - version is "2.7.5"
 62│ gyp ERR! find Python - version is 2.7.5 - should be >=3.6.0
 63│ gyp ERR! find Python - THIS VERSION OF PYTHON IS NOT SUPPORTED
 64│ gyp ERR! find Python 
 65│ gyp ERR! find Python **********************************************************
 66│ gyp ERR! find Python You need to install the latest version of Python.
 67│ gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
 68│ gyp ERR! find Python you can try one of the following options:
 69│ gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
 70│ gyp ERR! find Python   (accepted by both node-gyp and npm)
 71│ gyp ERR! find Python - Set the environment variable PYTHON
 72│ gyp ERR! find Python - Set the npm configuration variable python:
 73│ gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
 74│ gyp ERR! find Python For more information consult the documentation at:
 75│ gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
 76│ gyp ERR! find Python **********************************************************
 77│ gyp ERR! find Python 
 78│ gyp ERR! configure error 
 79│ gyp ERR! stack Error: Could not find any Python installation to use
 80│ gyp ERR! stack     at PythonFinder.fail (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:330:47)
 81│ gyp ERR! stack     at PythonFinder.runChecks (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:159:21)
 82│ gyp ERR! stack     at PythonFinder.<anonymous> (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:266:16)
 83│ gyp ERR! stack     at PythonFinder.execFileCallback (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:297:7)
 84│ gyp ERR! stack     at ChildProcess.exithandler (node:child_process:394:7)
 85│ gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
 86│ gyp ERR! stack     at maybeClose (node:internal/child_process:1100:16)
 87│ gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:304:5)
 88│ gyp ERR! System Linux 3.10.0-957.el7.x86_64
 89│ gyp ERR! command "/usr/local/nodejs/bin/node" "/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "…
 90│ gyp ERR! cwd /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass
 91│ gyp ERR! node -v v16.20.2
 92│ gyp ERR! node-gyp -v v8.4.1
 93│ gyp ERR! not ok 
 94│ Build failed with error code: 1
 95└─ Failed in 301ms at /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass
 96 ELIFECYCLE  Command failed with exit code 1.
 97[root@localhost kuailemao-blog]# 
 98
 99
100这个如何解决呢?

2、解决办法

gpt回答

根据你提供的错误信息,node-gyp 找不到合适的 Python 版本。它找到的 Python 版本是 2.7.5,而 node-gyp 需要 Python 3.6 或更高版本。以下是解决此问题的步骤:

  1. 安装 Python 3

首先,你需要在系统中安装 Python 3。如果你的系统已经安装了 Python 3,请确保它在 PATH 中。

在 CentOS 或 RHEL 系统上,你可以使用以下命令安装 Python 3:

1sudo yum -y install python3 --disablerepo=epel
  1. 设置 PYTHON 环境变量

安装 Python 3 之后,设置 PYTHON 环境变量以指向 Python 3 可执行文件。

1export PYTHON=$(which python3)
  1. 配置 npm 使用 Python 3

你还可以配置 npm 以使用 Python 3:

1npm config set python $(which python3)
2
3pnpm config set python $(which python3)
  1. 重试 pnpm install

现在重新运行 pnpm install

1pnpm install
  1. 验证安装

如果问题仍然存在,确保你的 python3 确实在 PATH 中并可以正确执行:

1python3 --version

你应该看到类似于 Python 3.x.x 的输出(确保版本是 3.6 或更高)。

总结

通过以上步骤,你应该能解决 node-gyp 找不到合适 Python 版本的问题。确保 Python 3 安装并配置正确后,pnpm install 应该能顺利完成。

如果还有问题,可以检查以下几点:

  • 确认 python3 是否在 PATH 中,可以通过 which python3 来验证路径。
  • 确认 npm 配置正确,可以通过 npm config get python 来查看配置。

希望这些步骤能帮助你解决问题!


本yum安装python3方法
1    ##安装python3
2    sudo yum -y install python3 --disablerepo=epel

安装python3后现象:

image-20240818084942011

 1[root@localhost ~]# python
 2python      python2     python2.7   python3     python3.6   python3.6m  
 3[root@localhost ~]# python
 4Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
 5[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
 6Type "help", "copyright", "credits" or "license" for more information.
 7>>> exit()
 8[root@localhost ~]# ll `which python`
 9lrwxrwxrwx. 1 root root 7 Aug 15 12:14 /usr/bin/python -> python2
10[root@localhost ~]# ll `which python2`
11lrwxrwxrwx. 1 root root 9 Aug 15 12:14 /usr/bin/python2 -> python2.7
12[root@localhost ~]# ll `which python2.7`
13-rwxr-xr-x. 1 root root 7216 Oct 31  2018 /usr/bin/python2.7
14[root@localhost ~]# ll `which python3`
15lrwxrwxrwx. 1 root root 9 Aug 17 17:15 /usr/bin/python3 -> python3.6
16[root@localhost ~]# ll `which python3.6`
17-rwxr-xr-x. 2 root root 11336 Nov 15  2023 /usr/bin/python3.6
18[root@localhost ~]# 
ruyu-blog需要python3环境时配置
1pnpm config set python $(which python3)
2python3 --version
扩展:给某个文件做软连接 -s -f -n选项
1sudo ln -sfn /usr/bin/python3.6 /usr/bin/python  #注意,这个命令时有风险的哦!!!

sudo ln -sfn /usr/bin/python3.6 /usr/bin/python 这条命令用于创建一个符号链接,使 /usr/bin/python 指向 /usr/bin/python3.6。以下是每个参数的含义:

  • sudo: 提权命令,意味着需要以超级用户权限执行该命令。
  • ln: 创建链接命令。
  • -s: 创建符号链接(软链接),而不是硬链接。
  • -f: 强制删除已经存在的目标文件(如果 /usr/bin/python 已经存在,会被删除)。
  • -n: 如果目标是一个符号链接,直接覆盖它,而不是在其上创建新的链接。
  • /usr/local/bin/python3.8: 链接的源文件,即要指向的实际文件路径。
  • /usr/bin/python: 链接的目标文件,即符号链接的路径。

这个命令的最终效果是使系统在执行 python 命令时,实际上运行的是 python3.8

:::warning

**这种做法在需要特定版本的 Python 并且不想直接更改系统默认 python 版本的情况下非常有用。**然而,需注意以下几点:

  1. 系统依赖:许多系统工具和脚本依赖于特定版本的 Python(经常是 Python 2.x),将 /usr/bin/python 修改为指向 Python 3.x 可能会导致这些工具和脚本无法正常工作。(例如yum)
  2. 环境管理:使用 pyenv 或者虚拟环境(virtualenvvenv)工具来管理不同版本的 Python 和依赖项通常是更安全和优雅的方法。

如果你确定没有上述问题,或者你已经做好了相应的准备和测试,那么可以使用这条命令来更改默认的 python 版本。

:::


这里我们来测试下:

配置前:

 1[root@localhost ~]# python
 2python      python2     python2.7   python3     python3.6   python3.6m  
 3[root@localhost ~]# python
 4Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
 5[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
 6Type "help", "copyright", "credits" or "license" for more information.
 7>>> exit()
 8[root@localhost ~]# ll `which python`
 9lrwxrwxrwx. 1 root root 7 Aug 15 12:14 /usr/bin/python -> python2
10[root@localhost ~]# ll `which python2`
11lrwxrwxrwx. 1 root root 9 Aug 15 12:14 /usr/bin/python2 -> python2.7
12[root@localhost ~]# ll `which python2.7`
13-rwxr-xr-x. 1 root root 7216 Oct 31  2018 /usr/bin/python2.7
14[root@localhost ~]# ll `which python3`
15lrwxrwxrwx. 1 root root 9 Aug 17 17:15 /usr/bin/python3 -> python3.6
16[root@localhost ~]# ll `which python3.6`
17-rwxr-xr-x. 2 root root 11336 Nov 15  2023 /usr/bin/python3.6
18[root@localhost ~]# 

配置后:

1ln -sfn /usr/bin/python3.6 /usr/bin/python
  • ln: 创建链接命令。
  • -s: 创建符号链接(软链接),而不是硬链接。
  • -f: 强制删除已经存在的目标文件(如果 /usr/bin/python 已经存在,会被删除)。
  • -n: 如果目标是一个符号链接,直接覆盖它,而不是在其上创建新的链接。

image-20240818090830337

如何恢复呢?(已恢复)

1#原来内容
2ln -sfn python2 /usr/bin/python

image-20240818090724252

image-20240818090756082

3、验证效果

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码

x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号

《云原生架构师实战》

image-20230107215126971

🍀 个人博客站点

https://onedayxyy.cn/

🍀 语雀

https://www.yuque.com/xyy-onlyone

🍀 csdn

https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎

https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!

推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航

本页导航