yum管理RPM包
yum介绍
yum自动解决依赖关系
Yum(全称为 Yellow dog Updater, Modified)是 一个在Fedora和RedHat以及CentOS中的Shell前端软件 包管理器。基于RPM包管理,能够从指定的服务器自动下载 RPM包并且安装,可以自动处理依赖性关系,并且一次安装 所有依赖的软件包,无须繁琐地一次次下载、安装。
查看yum命令与配置文件
查询已安装的yum软件包
[root@laser ~]# rpm -q yum
yum-3.4.3-168.el7.centos.noarch
查询已安装的yum软件包的配置文件
[root@laser ~]# rpm -qc yum
/etc/logrotate.d/yum
/etc/yum.conf
/etc/yum/version-groups.conf
yum命令常用选项
常用选项说明:
仓库
查询可用仓库 yum repolist
查看包括已启用或禁用的所有仓库状态 yum repolist all
关闭与启用仓库:本质都是在修改 repo文件中的enable的值 0 不启用 1 启用
如果找不到yum-config-manager命令 需要安装
yum -y install yum-utils
yum-config-manager --disable epel #关闭仓库epel
yum-config-manager --enable epel #启用仓库epel
查看
列出可用仓库中所有的软件包 yum list | less
或 yum list
列出可用仓库中的软件组 yum grouplist
查询命令所属的软件包,可以不加路径,只写命令名字 yum provides /usr/sbin/ifconfig
与rpm -qf的区别在于yum provides后可以只跟命名
安装
yum install httpd httpd-tools
加上-y选项可以变成非交互
yum groupinstall "开发工具" -y
安装软件组,一个软件组中包含了多个软件包
yum groups install "开发工具" -y
**卸载 ** remove
卸载以http开头的软件包 yum remove httpd httpd-tools http*
卸载软件组: yum groups remove "开发工具" -y
重装 reinstall
不小心删除了配置文件的时,可以使用reinstall下载回来
测试: 先删除httpd的配置文件, 然后使用reinstall恢复
[root@laser ~]# ls /etc/httpd/conf 查看配置文件位置
httpd.conf magic
[root@laser ~]# rm /etc/httpd/conf/httpd.conf 删除
rm:是否删除普通文件 "/etc/httpd/conf/httpd.conf"?y
[root@laser ~]# ls /etc/httpd/conf 查看已经删除
magic
进行重装
[root@laser ~]# yum reinstall httpd
[root@laser ~]# ls /etc/httpd/conf 再查看就恢复了
httpd.conf magic
更新
检查可以更新的软件包 yum check-update
更新所有软件包,包括内核,通常只在刚装完系统时执行
全部更新; yum update -y
更新某个软件包 yum update httpd -y
缓存
制作元数据缓存 yum makecache
清理元数据缓存 yum clean all
默认软件包下载安装后会自动删除 vim /etc/yum.conf
设置keepcache=1 即开启了软件包缓存
缓存目录为配置文件中指定的cachedir
历史记录
查看执行过的yum命令历史记录 yum history
查看具体某一条yum命令的详细信息 yum history info ID号
撤销执行过的历史命令 yum history undo ID号
关于安装需要注意
无论yum安装的软件来自何方,yum时刻以自己仓库中的 repodata存储的依赖关系为准,如果有多个仓库,就依次 检索
1、yum直接安装公网的rpm包, 会自动查找当前系统上已 有的仓库解决依赖关系
yum install https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/samba-4.10.16-5.el7.x86_64.rpm
2、Yum直接安装本地的rpm包,会自动查找当前系统上已 有的仓库解决依赖关系
yum localinstall -y /mnt/Packages/httpd2.4.6-88.el7.centos.x86_64.rpm
yum localinstall /opt/Packages/screen4.1.0-0.26.20120314git3c2946.el7.x86_64.rpm
要使用yum前,需要准备一个yum源(我们也称为yum仓 库), 这个可以是一个互联网上的仓库,也可以是本地自己 搭建的仓库.仓库里面有什么呢?里面全部都是.rpm的软 件包.一台linux,可以添加N多个yum源, 能搜索的软件包 数量就是N个yum源之和.
系统常见yum源
1.自定义的本地源
2.网络上的源头,如:base基础源、epel扩展源、与服务 相关的源(官网)
镜像文件作为yum源(本地源)
1、要是用,先挂载, 查看上方挂载详情
2、编辑repo文件
[root@laser ~]# cd /etc/yum.repos.d/
文件名自定义,必须以.repo结尾
[root@laser yum.repos.d]# vim local.repo
复制如下值:
[local] # 仓库的实际名字,任意
name=local # 仓库的描述,任意
baseurl=file:///opt # 仓库位置,可以是
http:// https:// ftp:// file:// 多种方式
enabled=1 # 启用仓库,默认就是启用的
gpgcheck=0 # 检查安装的rpm是否是合法的,0表示不检验, md5值
3、检查可用仓库
[root@laser ~]# yum repolist # 查询可用仓库
[root@laser ~]# yum repolist all # 查看所有仓库,包括禁用的
4、安装、查询、卸载
列出可用的软包
[root@laser ~]# yum list
安装
[root@laser ~]# yum install vsftpd -y
查询
[root@laser ~]# yum list vsftpd httpd
卸载
[root@laser ~]# yum remove vsftpd -y
安装多个
[root@laser ~]# yum install glib* httpd -y
查询某个软件(可以是未安装的)是由哪个rpm包提供
[root@laser ~]# yum provides httpd
5、软件包组安装、查询、卸载
[root@laser ~]# yum grouplist
[root@laser ~]# yum groupinstall "GNOME桌面" -y
[root@laser ~]# yum groupinstall "开发工具" -y
[root@laser ~]# yum groupremove "开发工具" -y
使用开源yum源(网络源)
然后下载开源的yum源头:
例如阿里云的Base基础源
先改名原来的原包
mv CentOS-Base.repo CentOSBase.repo_default
下载阿里云包
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos7.repo
可以直接下载
[root@laser ~]# curl -o/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
也可以安装一个软件包来生成
[root@laser ~]# yum install epel-release -y
EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍 生发行版如 CentOS、Scientific Linux 等提供高质 量软件包的项目。装上了 EPEL之后,就相当于添加了一个 第三方源。
RHEL以及他的衍生发行版如CentOS、Scientific Linux为了稳定,官方的rpm repository提供的rpm包 往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这 是服务器版本,安全稳定是重点,官方的rpm repository提供的rpm包也不够丰富,很多时候需要自己 编译那太辛苦了,而EPEL恰恰可以解决这两方面的问题。
例如nginx官网的软件包源
进入nginx官网->download->Pre-BuiltPackages->Stable and mainline->RHEL/CentOS-
>编辑到本地
[root@laser ~]# cd /etc/yum.repos.d/
[root@laser yum.repos.d]# vim nginx.repo
添加如下内容
[nginx-stable] 起名
name=nginx stable repo 别名
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ 下载位置
gpgcheck=1
enabled=1 如果为0则后续两行不用谢
gpgkey=https://nginx.org/keys/nginx_signing.key 校验值,md5值
module_hotfixes=true 是否校验
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
启动,不执行查询不到这个
[root@laser yum.repos.d]# yum-config-manager --enable nginx-mainline
测试下载
[root@laser yum.repos.d]#yum install nginx
将Centos系统默认的源备份
[root@laser ~]# cd /etc/yum.repos.d/
[root@laser yum.repos.d]# mkdir backup
[root@laser yum.repos.d]# mv CentOS-* backup/
自己制作本地yum源
1、下载好一堆rpm包,相关依赖包也都存在,将所有的 rpm包都放置于一个文件夹下,比如/opt
2、安装工具createrepo
[root@laser ~]# yum install createrepo -y
3、制作repodata
[root@laser ~]# createrepo /opt/
4、配置本地repo文件,指向/opt
[root@laser ~]# vim
/etc/yum.repos.d/soft.repo
[soft]
name=soft
baseurl=file:///opt
enabled=1
gpgcheck=0
yum扩展知识
1、yum元数据缓存:指的将yum仓库的元数据缓存下 来,下次安装时省去了检索过程,安装速度可以加快
[root@laser ~]# yum clean all # 清理缓存
[root@laser ~]# yum makecache # 制作缓存
2、yum软件包缓存:开启软件包缓存后,从网络源下 载安装完的软件安装包不再删除,都缓存到了本地,
[root@laser ~]# vim /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever # 指定软件安装包的缓存目录
keepcache=1 # 设置为1
debuglevel=2
# 缓存目录为:ls /var/cache/yum/x86_64/7/
3、基于软件包缓存,可以做成自己的yum源,以此来控制 各个服务的版本,通常的做法为
1、在测试环境中,在每个节点上都开启软件包缓存
2、在测试环境中,安装相同版本的操作系统,安装完毕后统一执行yum update -y
3、在测试环境中,部署完完你的架构,测试完毕
4、把测试环境中每台机器上缓存好的软件包安装包采集到一个文件下
[root@laser ~]# find /var/cache/yum/x86_64/7/ -type f -name "*.rpm" | xargs -I {} mv {} /soft/
5、然后用createrepo命令制作好依赖关系,即repodata数据库
6、然后在线上环境部署时,就用该本地源就好,不要轻易升级,不要滥用yum源,这样可以严格控制好各个服务的版本
自己制作本地yum源共享给别人
在服务端上执行如下操作
1、环境准备
[root@laser ~]# systemctl stop firewalld
[root@laser ~]# systemctl disable firewalld
[root@laser ~]# setenforce 0
[root@laser ~]# sed -i 's#^SELINUX=.*#SELINUX=disabled#g' /etc/selinux/config
2、安装ftp服务
[root@laser ~]# yum install vsftpd -y
[root@laser ~]# systemctl start vsftpd
[root@laser ~]# systemctl status vsftpd # 查看状态,确保开启
[root@laser ~]# systemctl enable vsftpd
3、采集软件包放到一个目录下
可以基于3.3的方式,也可以偷个懒直接用镜像里现成的, 毕竟是实验嘛,但无论何种方式,都一定要解决好依赖关系 才能用来做yum源,本例就用现成的吧
[root@laser ~]# mkdir /var/ftp/centos7
[root@laser ~]# mount /dev/sr0/var/ftp/centos7
4、或者拷贝
[root@laser ~]# mount /dev/sr0 /opt
[root@laser ~]# mkdir /var/ftp/centos7
[root@laser ~]# find /opt/Packages/ -type f-name "*.rpm" |xargs -I {} cp -rp {} /var/ftp/centos7/
在客户端上执行如下操作
1、环境准备
[root@laser ~]# systemctl stop firewalld
[root@laser ~]# systemctl disable firewalld
[root@laser ~]# setenforce 0
[root@laser ~]# sed -i 's#^SELINUX=.*#SELINUX=disabled#g' /etc/selinux/config
[root@laser ~]# ping -c 2 192.168.0.101 #ping一下服务端的ip地址,保证网络畅通
2、在客户端配置网络yum源
[root@laser yum.repos.d]# cd /etc/yum.repos.d/
[root@laser yum.repos.d]# mkdir bak
[root@laser yum.repos.d]# mv *.repo bak/
[root@laser yum.repos.d]# cat >> ftp.repo <<EOF
> [ftp_repo]
> name = This is Ftp Share Repo
> baseurl = ftp://192.168.0.101/centos7
> enabled = 1
> gpgcheck = 0
> EOF
其实可以使用vim方式写入, 这样比较新奇
注意:客户端baseurl指向的文件夹无论是什么,在该文件夹的子目录一级必须有一个repodata数据库
yum的历史记录
yum history # 查看执行过的yum命令历史记录
yum history info ID号 # 查看具体某一条yum命令的详细信息
yum history undo ID号 # 撤销执行过的历史命令
拓展了解:签名检查机制
RPM工具使用签名检查机制
# 导入centos公钥
[root@laser ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@laser ~]# rpm --import /etc/pki/rpm-gpg/*
额外选项:
--nomd5 # 不检验软件包的签名
YUM使用签名检查机制:
方式1
[root@laser yum.repos.d]# vim local.repo
...
gpgcheck=1 # 开启检查软件包的签名
方式2
[root@laser yum.repos.d]# vim local.repo
...
gpgcheck=1 # 开启检查软件包的签名
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEYCentOS-7 # 指定公钥文件
额外选项:
--nogpgcheck //不检查软件包的签名
源码包
1、官网下载,如nginx
浏览器下载 上传
或者执行命令
wget http://nginx.org/download/nginx1.18.0.tar.gz
2、预先安装编译安装依赖的库
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
或者
LANG=C yum -y groupinstall "Development tools"
3、解压、编译、编译安装
[root@laser ~]# tar xvf nginx1.18.0.tar.gz -C /usr/local/ 指定目录解压
[root@laser ~]# cd /usr/local/nginx1.18.0/
执行该命令编译
[root@laser nginx-1.18.0]# ./configure --user=www --group=www -- prefix=/usr/local/nginx --withhttp_stub_status_module --withhttp_ssl_module --with-stream --withhttp_gzip_static_module --withhttp_sub_module
命令解释:
1、--prefix 指定安装的目录,/usr/local/nginx是安装目录
2、带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了
3、http_stub_status_module 状态监控
4、http_ssl_module 配置https
5、stream 配置tcp得转发
6、http_gzip_static_module 压缩
7、http_sub_module 替换请求
安装, 这里有两步
[root@laser nginx-1.18.0]# make && make install
将程序设定一个软连接,便于后续的升级
[root@laser ~]#mkdir /soft #创建一个/soft目录
将/usr/local/nginx/目录链接到/soft/nginx
[root@laser soft]# ln -s /usr/local/nginx/ /soft/nginx
#创建一个禁止登录服务器的用户www
[root@laser ~]#useradd -s /sbin/nologin www
#启动nginx服务
[root@laser ~]# /soft/nginx/sbin/nginx
注意:现在百分之90,能用上的软件,都提供了rpm包. — >使用户可以使用yum直接安装使用.
测试 在本机浏览器上输入 http://虚拟机IP地址 访问虚拟 机NGINX服务。
总结
./configure --prefix=/usr/local/nginx1.18.0
a. 指定安装路径,例如–prefix=DIR
b. 启用或禁用某项功能, 例如 --enable-ssl, --disable-filter --with-http_ssl_module
c. 和其它软件关联,例如–withpcre=/root/pcre-8.31
d. 检查安装环境,例如是否有编译器gcc,是否满足软件的依赖需求
最终生成:Makefile
make: 按Makefile文件编译,可以使 用-j 2指定两颗CPU编译
make install: 安装
ps:如果中途发生错误,多半是因为缺少必要的库支持,可以先清理 make clean
清理掉以前编译后产生的 *.o目 标文件
然后安装好依赖库,重新安装