yum介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1.联网获取软件 2.基于RPM管理 3.自动解决依赖(前提:1.yum仓库中必须有该依赖包 2.版本要一致) 4.命令简单好记 5.生产最佳实践 yum是RedHat以及CentOS中的软件包管理器,能够通过互联网下载以rpm结尾的包,并且安装,并可以自动 处理依赖性关系,无需繁琐的一次次下载安装。 yum仓库 | yum源 yum install -y xx dnf install xxx Base源 - Base源 基础包 - epel源 扩展包
如何配置yum源
1 2 3 4 5 6 各大镜像源: 阿里云:https://opsx.alibaba.com/mirror 清华源:https://mirrors.tuna.tsinghua.edu.cn/ 163源:http://mirrors.163.com/ 华为源:https://mirrors.huaweicloud.com/ 科大源:http://mirrors.ustc.edu.cn/
配置Base源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@lb01 ~]# ll /etc/yum.repos.d/ yum源配置文件 wget -O:指定保存的路径和文件名 https://mirrors.aliyun.com/repo/Centos-7.repo curl -o = wget -O 1)清空/etc/yum.repos.d下面所有源配置文件(mv rm ) [root@localhost ~]# mv /etc/yum.repos.d/*.repo /tmp/ [root@localhost ~]# gzip /etc/yum.repos.d/* 2)下载国内各大镜像站源 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
换源
1 2 3 4 5 6 7 8 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \ -i.bak \ /etc/yum.repos.d/CentOS-*.repo
更换epel源
1 2 3 4 5 [root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo [root@localhost ~]# ll /etc/yum.repos.d/ total 8 -rw-r--r--. 1 root root 1759 Apr 22 18:55 CentOS-Base.repo -rw-r--r--. 1 root root 664 Apr 22 19:08 epel.repo
yum管理命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@yum_repo ~]# yum provides pstree 注意:yum provides命令只能用于已经安装的软件包 [root@yum_repo ~]# yum search pstree [root@localhost ~]# yum list [root@localhost ~]# yum list|grep net-tools [root@localhost ~]# yum info 包名 [root@localhost ~]# yum info tree [root@lb01 yum.repos.d]# yum repolist Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile repo id repo name status base/7/x86_64 CentOS-7 - Base - mirrors.huaweicloud.com 10,072 extras/7/x86_64 CentOS-7 - Extras - mirrors.huaweicloud.com 526 updates/7/x86_64 CentOS-7 - Updates - mirrors.huaweicloud.com 5,802 repolist: 16,400 repolist: 30,679 [root@localhost ~]# yum repolist all
yum安装命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@localhost ~]# yum install 包名 [root@localhost ~]# yum install vim Is this ok [y/d/N]: 输入y:yes 安装 输入d:download 仅下载不安装 输入N:No不安装 [root@localhost ~]# yum install vim -y [root@localhost ~]# yum -y install vim [root@localhost ~]# yum install -y vim [root@localhost ~]# yum install vim [root@localhost Packages]# yum localinstall -y wget-1.14-18.el7.x86_64.rpm [root@localhost Packages]# yum install -y http://test.driverzeng.com/MySQL_plugins/mha4mysql-node-0.56-0.el6.noarch.rpm [root@localhost ~]# yum install --downloadonly --downloaddir=/opt/ nginx
yum重装/更新/删除
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@localhost nginx]# yum reinstall -y nginx [root@localhost nginx]# yum check-update [root@localhost nginx]# yum update acl -y [root@localhost nginx]# yum update -y [root@localhost nginx]# yum remove -y tree [root@localhost nginx]# yum erase -y tree
yum逆向查询/缓存
1 2 3 4 5 6 7 8 [root@localhost nginx]# yum provides */ifconfig [root@localhost nginx]# yum makecache [root@localhost nginx]# yum clean all
爬取zabbix5.0相关所有rpm包
1 https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/?spm=a2c6h.25603864.0.0.239b5d4a bvLbEB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1.指定下载目录 [root@lb01 ~]# curl -s https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/?spm=a2c6h.25603864.0.0.239b5d4a bvLbEB|awk -F '"' '/rpm/{print "mkdir -p /repo/zabbix/;wget -O /repo/zabbix/"$4" https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/"$4}' |bash 2.下载到当前目录 [root@lb01 ~]# curl -s https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64|awk -F '"' '/rpm/{print $4}' |sed 's#zabbix#https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix#g' |xargs wget 1.curl 网站获取网页内容 [root@lb01 ~]# curl -s https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64 2.截取所有rpm包名 [root@lb01 ~]# curl -s https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64|awk -F '"' '/rpm/{print $4}' zabbix-agent-5.0.0-1.el7.x86_64.rpm zabbix-agent-5.0.1-1.el7.x86_64.rpm zabbix-agent-5.0.10-1.el7.x86_64.rpm zabbix-agent-5.0.11-1.el7.x86_64.rpm 3.补全公共下载链接https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/?spm=a2c6h.25603864.0.0.239b5d4a bvLbEB sed 's#zabbix#https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix#g' 4.拼接命令交给wget [root@lb01 ~]# curl -s https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64|awk -F '"' '/rpm/{print $4}' |sed 's#zabbix#https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix#g' |xargs wget
yum程序的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@localhost ~]# cat /etc/yum.conf cachedir=/var/cache/yum/$basearch /$releasever //缓存目录 keepcache=0 //缓存软件包, 1启动 0 关闭 debuglevel=2 //调试级别 logfile=/var/log/yum.log //日志记录位置 exactarch=1 //检查平台是否兼容 obsoletes=1 //检查包是否废弃 gpgcheck=1 //检查来源是否合法,需要有制作者的公钥信息 plugins=1 //是否启用查询 installonly_limit=5 bugtracker_url
yum包组相关命令
1 2 3 4 5 6 7 [root@localhost ~]# yum groups list [root@localhost ~]# yum groups install Development tools Compatibility libraries Base Debugging Tools [root@localhost ~]# yum groups remove -y Base
yum包历史相关命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [root@localhost ~]# yum history Loaded plugins: fastestmirror ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------- 6 | root <root> | 2024-04-23 18:05 | Install | 1 5 | root <root> | 2024-04-23 17:09 | Update | 2 4 | root <root> | 2024-03-26 17:36 | Install | 1 3 | root <root> | 2024-03-26 17:31 | Install | 1 2 | root <root> | 2024-03-22 15:55 | Install | 1 1 | System <unset > | 2024-03-21 20:50 | Install | 308 [root@localhost ~]# yum history info 6 Loaded plugins: fastestmirror Transaction ID : 6 Begin time : Tue Apr 23 18:05:41 2024 Begin rpmdb : 311:bc74a8339d5853ce2035d376bffbbeeb40d44ae3 End time : 18:05:42 2024 (1 seconds) End rpmdb : 312:44280ba9419b586ea77d5fa483785725fe7e9758 User : root <root> Return-Code : Success Command Line : install -y tree Transaction performed with: Installed rpm-4.11.3-35.el7.x86_64 @anaconda Installed yum-3.4.3-161.el7.centos.noarch @anaconda Installed yum-plugin-fastestmirror-1.1.31-50.el7.noarch @anaconda Packages Altered: Install tree-1.6.0-10.el7.x86_64 @base history info[root@localhost ~]# yum history undo 6 -y
制作yum仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 条件: 1.制作仓库,一定要有存放rpm包的目录 2.会配置repo仓库文件 [base] name=CentOS-$releasever - Base - mirrors.aliyun.com baseurl=http://mirrors.aliyun.com/centos/$releasever /os/$basearch / gpgcheck=1 // 0 代表关闭,不检测 1 代表开启,检测 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 enabled=0 // 是否开启该仓库,0代表关闭 1代表开启 钥匙 锁 - http:// 远程协议 - https:// 远程协议 - ftp:// 远程协议 - file:// 本地协议,只能访问本机
制作仓库(本地yum仓库)
1 2 3 4 5 6 7 8 9 10 11 12 [root@localhost ~]# yum install -y createrepo [root@localhost ~]# createrepo /repo/zabbix/ Spawning worker 0 with 475 pkgs [root@localhost yum.repos.d]# vim zls.repo [zls_repo] name=zls's yum repository xxxx baseurl=file:///repo/zabbix/ gpgcheck=0 enabled=1
如何在仓库中新加RPM包
1 2 3 4 5 6 wget cp mv ... [root@localhost repo]# createrepo --update /repo [root@localhost ~]# yum makecache
需求:将/dev/cdrom中的rpm包,拷贝到 /zls/base目录下,然后将该目录制作成本地的yum仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@localhost ~]# mkdir -p /zls/base [root@localhost ~]# mount /dev/cdrom /mnt [root@localhost ~]# cp /mnt/Packages/*.rpm /zls/base/ [root@localhost ~]# createrepo /zls/base/ [root@localhost ~]# vi /etc/yum.repos.d/zls.repo [zls_repo] name=zls's yum repository xxxx baseurl=file:///repo/ gpgcheck=0 enabled=1
制作远程yum仓库
环境准备
主机
ip
角色
yum_repo
10.0.0.101
yum仓库
web01
10.0.0.102
使用yum仓库的机器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 - http:// 远程协议 - https:// 远程协议(别考虑) - ftp:// 远程协议 [root@web01 ~]# hostnamectl set-hostname yum_repo [root@web01 ~]# hostnamectl set-hostname web01 [root@yum_repo ~]# systemctl stop firewalld [root@web01 ~]# systemctl stop firewalld [root@web01 ~]# setenforce 0 [root@web01 ~]# getenforce Permissive
使用ftp制作yum仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 [root@yum_repo ~]# yum install -y vsftpd [root@yum_repo ~]# systemctl start vsftpd [root@yum_repo ~]# systemctl enable vsftpd [root@yum_repo pub]# rpm -ql vsftpd|grep pub /var/ftp/pub [root@yum_repo pub]# mount /dev/cdrom /mnt/ [root@yum_repo pub]# cp /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm /mnt/Packages/wget-1.14-18.el7.x86_64.rpm base/ [root@yum_repo ~]# yum install -y createrepo [root@yum_repo ~]# createrepo /var/ftp/pub/base/ Spawning worker 0 with 2 pkgs Workers Finished Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete [root@yum_repo base]# vim zls_base.repo [zls_base_repo] name=test baseurl=ftp://10.0.0.101/pub/base/ gpgcheck=0 enabled=1 [root@web01 ~]# curl -o /etc/yum.repos.d/zls_base.repo ftp://10.0.0.101/pub/base/zls_base.repo [root@web01 ~]# vim /etc/yum.repos.d/xxx.repo [zls_base_repo] name=test baseurl=ftp://10.0.0.101/pub/base/ gpgcheck=0 enabled=1
使用nginx制作远程yum仓库
环境准备
主机
ip
角色
yum_repo
10.0.0.101
yum仓库
web01
10.0.0.102
使用yum仓库的机器
前期准备
1 2 3 4 5 6 7 8 9 10 11 12 [root@web01 ~]# hostnamectl set-hostname yum_repo [root@web01 ~]# hostnamectl set-hostname web01 [root@yum_repo ~]# systemctl stop firewalld [root@web01 ~]# systemctl stop firewalld [root@web01 ~]# setenforce 0 [root@web01 ~]# getenforce Permissive
使用nginx制作yum仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 [root@yum_repo ~]# yum -y install nginx [root@yum_repo ~]# systemctl start nginx [root@yum_repo ~]# systemctl enable nginx [root@yum_repo yum]# mkdir /etc/repo/yum/{base,zabbix} -p [root@yum_repo yum]# mkdir /etc/repo/yum/base/ -p [root@yum_repo yum]# mkdir /etc/repo/yum/zabbix/ -p [root@web01 ~]# mount /dev/cdrom /mnt/ [root@yum_repo yum]# cp /mnt/Packages/* /etc/repo/yum/base/ 拷贝爬取的zabbix的包到/etc/repo/yum/zabbix下 [root@lb01 nginx]# cp /repo/zabbix/* /etc/repo/yum/zabbix [root@yum_repo ~]# yum install -y createrepo [root@yum_repo yum]# createrepo /etc/repo/yum/base/ [root@yum_repo yum]# createrepo /etc/repo/yum/zabbix/ [root@yum_repo nginx]# vim /etc/nginx/nginx.conf location / { 45 autoindex on; 46 root /etc/repo/yum; 47 } server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; location / { autoindex on; root /etc/repo/yum; } } 这个前提是,nginx.confp配置文件里没有server{ }这个内容,相当于把这段命令写在conf.d配置文件里 server{ listen 80; server_name _; location /{ autoindex on; root /yum_repo; index index.html; } } [root@yum_repo ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@yum_repo ~]# systemctl restart nginx [root@yum_repo base]# vim nginx.repo [nginx_repo] name=test baseurl=10.0.0.101/base/ gpgcheck=0 enabled=1 [nginx_repo] name=test baseurl=10.0.0.101/zabbix/ gpgcheck=0 enabled=1 可以将nginx.repo文件放在 /etc/repo/yum/目录下 下载制作的nginx.repo源 [root@web01 ~]# wget -O /etc/yum.repos.d/ http://10.0.0.101/nginx.repo 删除web01客户端其它源,只留nginx.repo源,使用安装rpm包测试我们制作的yum源