1、rpm升级、降级
- 升级安装
rpm -Uvh kernel-2.6.32-696.el6.x86_64.rpm ---升级安装
rpm -ivh kernel-2.6.32-642.el6.x86_64.rpm --oldpackage ---降级安装
rpm -ivh kernel-2.6.32-642.el6.x86_64.rpm ---重新安装
总结:降级安装和重新安装内核时不会把原有的内核也删掉,系统会并存两个内核,但升级安装时会把原有的内核也删除,建议生产环境中不要升级安装内核。
2、rpm2cpio
- rpm2cpio命令用于将rpm软件包转换为cpio格式的文件
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -ivt ---预览包内文件,可以看到包内的文件写的都是相对路径
-rwxr-xr-x 1 root root 41136 Jan 14 2015 ./usr/bin/tree
drwxr-xr-x 2 root root 0 Jan 14 2015 ./usr/share/doc/tree-1.5.3
-rw-r--r-- 1 root root 18009 Aug 13 2004 ./usr/share/doc/tree-1.5.3/LICENSE
-rw-r--r-- 1 root root 4167 Oct 20 2009 ./usr/share/doc/tree-1.5.3/README
-rw-r--r-- 1 root root 3375 Jan 14 2015 ./usr/share/man/man1/tree.1.gz
132 blocks
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -iv ./usr/bin/tree ---只解包./usr/bin/tree这个文件,但是当前app这个目录下没有usr这个目录
cpio: ./usr/bin/tree: Cannot open: No such file or directory
./usr/bin/tree
132 blocks
[root@centos6 app]#ls
[root@centos6 app]#ls
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -ivd ./usr/bin/tree ---用-d选项可以解包还原时自动生成目录,
./usr/bin/tree
132 blocks
[root@centos6 app]#ls --解包到当前的app目录下usr/bin/tree
usr
[root@centos6 app]#cd usr
[root@centos6 usr]#ls
bin
[root@centos6 usr]#cd bin
[root@centos6 bin]#ls
tree
总结:需要注意的是包内的文件写的都是相对路径,解包时只能解包到前期目录,所以解包时最好先进入指定目录,然后在解包,这样可以把这个文件正好解包到这个目录,并且不用-d选项,否则要在别的目录下创建之后在移动到目标目录。
3、破坏tree程序的一个文件,安装和重新安装的区别
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -ivt ---预览包内文件,可以看到包内的文件写的都是相对路径
-rwxr-xr-x 1 root root 41136 Jan 14 2015 ./usr/bin/tree
drwxr-xr-x 2 root root 0 Jan 14 2015 ./usr/share/doc/tree-1.5.3
-rw-r--r-- 1 root root 18009 Aug 13 2004 ./usr/share/doc/tree-1.5.3/LICENSE
-rw-r--r-- 1 root root 4167 Oct 20 2009 ./usr/share/doc/tree-1.5.3/README
-rw-r--r-- 1 root root 3375 Jan 14 2015 ./usr/share/man/man1/tree.1.gz
132 blocks
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -iv ./usr/bin/tree ---只解包./usr/bin/tree这个文件,但是当前app这个目录下没有usr这个目录
cpio: ./usr/bin/tree: Cannot open: No such file or directory
./usr/bin/tree
132 blocks
[root@centos6 app]#ls
[root@centos6 app]#ls
[root@centos6 app]#rpm2cpio /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm |cpio -ivd ./usr/bin/tree ---用-d选项可以解包还原时自动生成目录,
./usr/bin/tree
132 blocks
[root@centos6 app]#ls --解包到当前的app目录下usr/bin/tree
usr
[root@centos6 app]#cd usr
[root@centos6 usr]#ls
bin
[root@centos6 usr]#cd bin
[root@centos6 bin]#ls
tree
总结:我们只是破坏了tree的一个文件,并没有破坏所有的文件,所有在安装时显示虽然是100%,但并没有重新安装,跟用rpm -e tree卸载不同,卸载相当于把tree所有的文件都卸载了,这个时候不用--force安装就可以,但如果只破坏了某一个文件只能用--force强制重新安装后。
- 如果没有安装autofs服务,使用不了misc,则需要自己挂载光盘进行安装
1、模拟误删除文件
[root@localhost ~]# rm -rf /etc/inittab
[root@localhost ~]# cat /etc/inittab
cat: /etc/inittab: No such file or directory
2、新建该文件,并查看对应包的相关信息
[root@localhost ~]# touch /etc/inittab
[root@localhost ~]# rpm -qf /etc/inittab
initscripts-8.45.19.EL-1
3、确定了包的相关信息,挂载安装光盘,进行查找
[root@localhost ~]# cd /mnt/yum/
[root@localhost yum]# ll
total 0
[root@localhost yum]# cd ~
[root@localhost ~]# mount /dev/cdrom /mnt/yum
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/yum/Server/
[root@localhost Server]# cp initscripts-8.45.30-2.el5.i386.rpm /tmp
cp: overwrite `/tmp/initscripts-8.45.30-2.el5.i386.rpm'? y
[root@localhost Server]# cd /tmp
[root@localhost tmp]# ll
total 1680
drwx------ 2 root root 4096 Feb 17 13:45 bin
drwx------ 9 root root 4096 Feb 17 13:45 etc
drwx------ 3 root root 4096 Feb 17 13:53 gconfd-root
-r--r--r-- 1 root root 1651907 Feb 17 15:41 initscripts-8.45.30-2.el5.i386.rpm
drwx------ 2 root root 4096 Feb 17 13:53 keyring-06YG9F
drwx------ 2 root root 4096 Sep 26 08:44 keyring-elqVM1
drwx------ 2 root root 4096 Sep 26 08:08 keyring-jTNbTb
drwx------ 3 root root 4096 Feb 17 13:45 lib
srwxr-xr-x 1 root root 0 Feb 17 13:54 mapping-root
drwx------ 2 root root 4096 Feb 17 15:38 orbit-root
drwx------ 2 root root 4096 Feb 17 13:45 sbin
drwx------ 2 root root 4096 Feb 17 13:53 ssh-ROxTuK6949
drwx------ 4 root root 4096 Feb 17 13:45 usr
drwx------ 4 root root 4096 Feb 17 13:45 var
drwx------ 2 root root 4096 Feb 17 13:54 virtual-root.2eelUV
drwxrwxrwt 2 root root 4096 Feb 5 12:19 VMwareDnD
drwx------ 2 root root 4096 Feb 17 13:53 vmware-root
4、解压包的信息
[root@localhost tmp]# rpm2cpio initscripts-8.45.30-2.el5.i386.rpm |cpio -imd
10748 blocks
[root@localhost tmp]# ll
total 1680
drwx------ 2 root root 4096 Feb 17 13:45 bin
drwx------ 9 root root 4096 Feb 17 13:45 etc
drwx------ 3 root root 4096 Feb 17 13:53 gconfd-root
-r--r--r-- 1 root root 1651907 Feb 17 15:41 initscripts-8.45.30-2.el5.i386.rpm
drwx------ 2 root root 4096 Feb 17 13:53 keyring-06YG9F
drwx------ 2 root root 4096 Sep 26 08:44 keyring-elqVM1
drwx------ 2 root root 4096 Sep 26 08:08 keyring-jTNbTb
drwx------ 3 root root 4096 Feb 17 13:45 lib
srwxr-xr-x 1 root root 0 Feb 17 13:54 mapping-root
drwx------ 2 root root 4096 Feb 17 15:38 orbit-root
drwx------ 2 root root 4096 Feb 17 13:45 sbin
drwx------ 2 root root 4096 Feb 17 13:53 ssh-ROxTuK6949
drwx------ 4 root root 4096 Feb 17 13:45 usr
drwx------ 4 root root 4096 Feb 17 13:45 var
drwx------ 2 root root 4096 Feb 17 13:54 virtual-root.2eelUV
drwxrwxrwt 2 root root 4096 Feb 5 12:19 VMwareDnD
drwx------ 2 root root 4096 Feb 17 13:53 vmware-root
5、进行文件替换与简单修改
[root@localhost tmp]# cp ./etc/inittab /etc/
cp: overwrite `/etc/inittab'? y
[root@localhost tmp]# cat /etc/inittab
4、rpm的查询
rpm -qp --scripts /misc/cd/Packages/mod_ssl-2.2.15-59.el6.centos.x86_64.rpm ---查询未安装的文件包中包含的脚本
rpm -q --scripts rpm ---查询已安装的包包含哪些脚本
[root@centos6 ~]#rpm -q tree ---查询tree包来源于哪个软件包
tree-1.5.3-3.el6.x86_64
rpm -qf /usr/bin/tree ---如果tree包没有安装,可以-f通过写路径查询
rpm -qi tree ---查询已安装的包的信息,也可以用-p加上路径,查看未安装的包的信息。
[root@centos6 ~]#rpm -ql tree ---查询安装包生成的文件,可以用-p加路径查看未安装的包安装后将生成的文件列表
/usr/bin/tree
/usr/share/doc/tree-1.5.3
/usr/share/doc/tree-1.5.3/LICENSE
/usr/share/doc/tree-1.5.3/README
/usr/share/man/man1/tree.1.gz
[root@centos6 ~]#rpm -qa |grep tre ---查询所有已安装的包,可以用grep过滤一下
gstreamer-python-0.10.16-1.1.el6.x86_64
gstreamer-plugins-bad-free-0.10.19-5.el6_8.x86_64
gstreamer-tools-0.10.29-1.el6.x86_64
libspectre-0.2.4-2.el6.x86_64
gstreamer-0.10.29-1.el6.x86_64
portreserve-0.0.4-11.el6.x86_64
phonon-backend-gstreamer-4.6.2-28.el6_5.x86_64
PackageKit-gstreamer-plugin-0.5.8-26.el6.x86_64
gstreamer-plugins-base-0.10.29-2.el6.x86_64
gstreamer-plugins-good-0.10.23-4.el6_8.x86_64
tree-1.5.3-3.el6.x86_64
5、包校验
[root@centos6 ~]#rpm -V tree
[root@centos6 ~]#rpm -ql tree
/usr/bin/tree
/usr/share/doc/tree-1.5.3
/usr/share/doc/tree-1.5.3/LICENSE
/usr/share/doc/tree-1.5.3/README
/usr/share/man/man1/tree.1.gz
[root@centos6 ~]#ll /usr/bin/tree
-rwxr-xr-x. 1 root root 41136 Jan 14 2015 /usr/bin/tree
[root@centos6 ~]#chown zhang /usr/bin/tree ---改变其中一个文件的所有者
[root@centos6 ~]#ll /usr/bin/tree
-rwxr-xr-x. 1 zhang root 41136 Jan 14 2015 /usr/bin/tree
[root@centos6 ~]#rpm -V tree ---可以检测出来所有者发生改变了
.....U... /usr/bin/tree
总结:检查包一些元数据信息是否改变,不需要导入钥匙也可以校验。
6、检查包的签名和完整性
[root@centos6 Packages]#rpm -ivh tree-1.5.3-3.el6.x86_64.rpm
warning: tree-1.5.3-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY ---有警报
Preparing... ########################################### [100%]
package tree-1.5.3-3.el6.x86_64 is already installed
[root@centos6 Packages]#cd /etc/pki/rpm-gpg/ ---进入这个目录下或者进入光盘/misc/cd
[root@centos6 rpm-gpg]#ls
RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6
RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6
[root@centos6 rpm-gpg]#rpm --import RPM-GPG-KEY-CentOS-6 ---将公钥导入
[root@centos6 rpm-gpg]#cdpack
[root@centos6 Packages]#rpm -ivh tree-1.5.3-3.el6.x86_64.rpm ---再安装已经没有警报了
Preparing... ########################################### [100%]
package tree-1.5.3-3.el6.x86_64 is already installed
[root@centos6 Packages]#rpm -K tree-1.5.3-3.el6.x86_64.rpm
tree-1.5.3-3.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK ---导入钥匙后检查包就ok
[root@centos6 Packages]#rpm -qa "gpg-pubkey" ---查询导入的公钥
gpg-pubkey-c105b9de-4e0fd3a3
[root@centos6 Packages]#rpm -e gpg-pubkey-c105b9de-4e0fd3a3 ---删除公钥
[root@centos6 Packages]#rpm -ivh tree-1.5.3-3.el6.x86_64.rpm
warning: tree-1.5.3-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY ---再次安装时又有警报了
Preparing... ########################################### [100%]
package tree-1.5.3-3.el6.x86_64 is already installed
[root@centos6 Packages]#rpm -K tree-1.5.3-3.el6.x86_64.rpm ---K可以检查包的完整性和签名,如果没有钥匙就无法检查。显示的是notok
tree-1.5.3-3.el6.x86_64.rpm: RSA sha1 ((MD5) PGP) md5 NOT OK (MISSING KEYS: (MD5) PGP#c105b9de) ---没有钥匙
[root@centos6 app]#echo >>tree-1.5.3-3.el6.x86_64.rpm ---更改包的内容加了个空格
[root@centos6 rpm-gpg]#rpm --import RPM-GPG-KEY-CentOS-6 ---将公钥导入
[root@centos6 app]#rpm -K tree-1.5.3-3.el6.x86_64.rpm ---发现文件的完整性不对,所以显示的是notok
tree-1.5.3-3.el6.x86_64.rpm: RSA sha1 (MD5) PGP MD5 NOT OK
总结:用rpm -K 可以检查包的签名和完整性,前提是要先导入公钥。