Linux用rpm管理软件
主要介绍使用rpm对软件包进行管理。
使用rpm查询软件的信息
使用rpm安装及卸载软件
使用rpm对软件进行更新
使用rpm对软件进行验证
rpm 全称是redhat package manager,后来改成rpm package manager,这是根据源码包编译出来的包。先从光盘中拷贝一个包,并看它是如何命名的。
先挂载光盘,然后拷贝 vsftpd这个包,命令如下。
[root@rh1 ~]# mount /dev/cdrom /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@rh1 ~]# cp /mnt/AppStream/Packages/vsftpd-3.0.3-34.el8.x86_64.rpm
[root@rh1 ~]# cp /mnt/AppStream/Packages/vsftpd-3.0.3-34.el8.x86_64.rpm .
[root@rh1 ~]# ls vsftpd-3.0.3-34.el8.x86_64.rpm
vsftpd-3.0.3-34.el8.x86_64.rpm
这里字段的含义如下。
(1)vsftpd:包的名称。
(2)3.0.3:版本,即 version。
(3)33.el8:小版本号,即release,其中el8 指的是此包适用于RHEL8系统。
(4) x86_64:指的是架构,到底是32位还是64位的包,x86_64表示是64位的。rpm的安
装命令是“rpm -ivh 安装包”。安装rpm,命令如下。
[root@rh1 ~]# rpm -ivh vsftpd-3.0.3-34.el8.x86_64.rpm
警告:vsftpd-3.0.3-34.el8.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:vsftpd-3.0.3-34.el8 ################################# [100%]
[root@rh1 ~]#
这样就把vsftpd-3.0.3-33.el8.x86_64.rpm 安装好了。如果是第一次接触 Linux 会感觉到奇怪,怎么不像 Windows一样让我们通过浏览来指定路径,那么这个包安装到哪里了呢?
相信大家在Windows中都安装过 Chrome浏览器,基本上是秒安装,也没有指定路径,因为这个安装包中已经定义好安装路径了。同理,rpm 安装时也已经指定了安装路径,把这个rpm打开,先拷贝到/opt目录中,命令如下。
[root@rh1 ~]# cp vsftpd-3.0.3-34.el8.x86_64.rpm /opt/
[root@rh1 ~]# cd /opt/
[root@rh1 opt]# ls
vsftpd-3.0.3-34.el8.x86_64.rpm
[root@rh1 opt]#
解压此包,命令如下。
[root@rh1 opt]# rpm2cpio vsftpd-3.0.3-34.el8.x86_64.rpm | cpio -id
714 块
[root@rh1 opt]# ls
etc usr var vsftpd-3.0.3-34.el8.x86_64.rpm
可以看到,生成了3个目录etc、usr、var,看一下它们的结构,如下所示。
[root@rh1 opt]# tree
[root@rh1 opt]# tree
.
├── etc
│ ├── logrotate.d
│ │ └── vsftpd
│ ├── pam.d
│ │ └── vsftpd
│ └── vsftpd
│ ├── ftpusers
│ ├── user_list
│ ├── vsftpd.conf
│ └── vsftpd_conf_migrate.sh
├── usr
│ ├── lib
│ │ └── systemd
│ │ ├── system
│ │ │ ├── vsftpd.service
│ │ │ ├── vsftpd@.service
│ │ │ └── vsftpd.target
│ │ └── system-generators
│ │ └── vsftpd-generator
│ ├── sbin
│ │ └── vsftpd
│ └── share
│ ├── doc
│ │ └── vsftpd
│ │ ├── AUDIT
│ │ ├── BENCHMARKS
│ │ ├── BUGS
│ │ ├── Changelog
│ │ ├── COPYING
│ │ ├── EXAMPLE
│ │ │ ├── INTERNET_SITE
│ │ │ │ ├── README
│ │ │ │ ├── vsftpd.conf
│ │ │ │ └── vsftpd.xinetd
│ │ │ ├── INTERNET_SITE_NOINETD
│ │ │ │ ├── README
│ │ │ │ └── vsftpd.conf
│ │ │ ├── PER_IP_CONFIG
│ │ │ │ ├── hosts.allow
│ │ │ │ └── README
│ │ │ ├── README
│ │ │ ├── VIRTUAL_HOSTS
│ │ │ │ └── README
│ │ │ ├── VIRTUAL_USERS
│ │ │ │ ├── logins.txt
│ │ │ │ ├── README
│ │ │ │ ├── vsftpd.conf
│ │ │ │ └── vsftpd.pam
│ │ │ └── VIRTUAL_USERS_2
│ │ │ └── README
│ │ ├── FAQ
│ │ ├── INSTALL
│ │ ├── LICENSE
│ │ ├── README
│ │ ├── README.security
│ │ ├── REWARD
│ │ ├── SECURITY
│ │ │ ├── DESIGN
│ │ │ ├── IMPLEMENTATION
│ │ │ ├── OVERVIEW
│ │ │ └── TRUST
│ │ ├── SIZE
│ │ ├── SPEED
│ │ ├── TODO
│ │ ├── TUNING
│ │ └── vsftpd.xinetd
│ └── man
│ ├── man5
│ │ └── vsftpd.conf.5.gz
│ └── man8
│ └── vsftpd.8.gz
├── var
│ └── ftp
│ └── pub
└── vsftpd-3.0.3-34.el8.x86_64.rpm
27 directories, 48 files
[root@rh1 opt]# cd
[root@rh1 ~]#
可以看到,当我们安装rpm包时,它就会把包中的内容按照这个结构拷贝到系统,所以安装的路径都是安排好了的 。
1.rpm查询
如果要查询已经安装过的软件包的信息, rpm的第一个选项需要使用-q,表示查询的意思。查询系统安装的所有软件包用 rpm-qa命令,命令如下。
[root@rh1 ~]# rpm -qa
查询系统是否安装了某个软件包用“rpm -qa 包名”命令。例如,要查询是否安装了vsftpd,命令如下。
[root@rh1 ~]# rpm -qa vsftpd
vsftpd-3.0.3-34.el8.x86_64
但是这种用法,在写包名时多一个或少一个字符都查询不出来
所以,更建议用管道和grep进行过滤,如下所示。
[root@rh1 ~]# rpm -qa | grep vsftp
vsftpd-3.0.3-34.el8.x86_64
查询安装某软件包之后所生成的文件用“rpm -ql 包名”命令,这里q后面是字母l。
例如,要查询vsftpd所生成的文件,命令如下。
[root@rh1 ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/vsftpd
这样就可以看到 vsftpd安装到哪里了。
查看软件包生成的配置文件用“rpm -qc包名”命令,命令如下。
[root@rh1 ~]# rpm -qc vsftpd
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list
/etc/vsftpd/vsftpd.conf
查看包的信息用“rpm-qi包名”命令,命令如下。
[root@rh1 ~]# rpm -qi vsftpd
Name : vsftpd
Version : 3.0.3
Release : 34.el8
Architecture: x86_64
Install Date: 2023年12月12日 星期二 11时02分57秒
Group : System Environment/Daemons
Size : 355692
License : GPLv2 with exceptions
Signature : RSA/SHA256, 2021年04月21日 星期三 20时42分12秒, Key ID 199e2f91fd431d51
Source RPM : vsftpd-3.0.3-34.el8.src.rpm
Build Date : 2021年04月20日 星期二 22时28分24秒
Build Host : x86-vm-15.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : https://security.appspot.com/vsftpd.html
Summary : Very Secure Ftp Daemon
Description :
vsftpd is a Very Secure FTP daemon. It was written completely from
当我们安装软件包时会产生许多文件,反过来想查询某个文件是由哪个软件包安装出来的用rpm -qf /path/file命令,命令如下。
[root@rh1 ~]# rpm -qf /etc/vsftpd/vsftpd.conf
vsftpd-3.0.3-34.el8.x86_64
可以看到,/etc/vsfipd/vsfipd.conf是由 vsftpd这个包产生的。
[root@rh1 ~]# rpm -qf /etc/passwd
setup-2.12.2-6.el8.noarch
可以看到,/etc/passwd是由setup这个包生成的。
以上这些都是针对已经安装了的软件包进行查询,如果要查询安装包,则需要加上p选项,命令如下。
[root@rh1 ~]# rpm -qcp vsftpd-3.0.3-34.el8.x86_64.rpm
警告:vsftpd-3.0.3-34.el8.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list
/etc/vsftpd/vsftpd.conf
当然,在RHEL8/CentOS8中不加p选项也可以,之前的系统是不行的。
2.rpm安装及卸载
前面已经介绍了用“rpm -ivh 安装包”命令安装软件,且 vsftpd已经安装完成,如下所示。
卸载软件包的命令是“rpm -e 软件包”,现在要把vsftpd卸载掉,命令如下。
[root@rh1 ~]# rpm -e vsftpd
[root@rh1 ~]# rpm -qa | grep vsftpd
可以看到,现在vsftpd已经不存在了,再次把这个包安装上去。
[root@rh1 ~]# rpm -ivh vsftpd-3.0.3-34.el8.x86_64.rpm
警告:vsftpd-3.0.3-34.el8.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:vsftpd-3.0.3-34.el8 ################################# [100%]
有时需要强制安装软件,例如,某个包已经安装过了,现在想重新安装,此时加上–force选项强制安装即可。
[root@rh1 ~]# rpm -ivh vsftpd-3.0.3-34.el8.x86_64.rpm --force
警告:vsftpd-3.0.3-34.el8.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:vsftpd-3.0.3-34.el8 ################################# [100%]
[root@rh1 ~]#
一般情况下,用于某个文件丢失了,想通过强制重新安装来找回此文件
3.rpm验证
当我们安装了一个软件包之后会产生许多文件,要是想判断这些文件是否被修改过,可以用rpm -V(大写字母V)命令,例如,我们刚刚安装了vsftpd,并没有修改任何配置文件。
[root@rh1 ~]# rpm -V vsftpd
[root@rh1 ~]#
没有任何输出,说明此vsftpd所生成的文件没有被修改。现在用vim 编辑器修改一下/etc/vsftpd/vsftpd.conf,随便增添删减一些内容(最好是修改注释后面的内容,否则影响vsftpd启动),然后再次判断,命令如下。
[root@rh1 ~]# rpm -V vsftpd
S.5....T. c /etc/vsftpd/vsftpd.conf
再次检查可以看到/etc/vsftpd/vsftpd.conf被修改过。
S指的是大小。
5指的是md5值。
T指的是时间。
c指的是此文件是vsftpd 的配置文件。
这样就可以看到哪些文件被修改过了。只修改时间,命令如下。
[root@rh1 ~]# touch /etc/vsftpd/ftpusers
[root@rh1 ~]# rpm -V vsftpd
.......T. c /etc/vsftpd/ftpusers
S.5....T. c /etc/vsftpd/vsftpd.conf
可以看到,/etc/vsftpd/ftpusers的时间发生了改变。