Yum and RPM Tricks

来源:百度文库 编辑:神马文学网 时间:2024/04/28 10:33:52
Yum and rpm are excellent package management tools, but they have several lesser known options and features that allow you to do some very interesting things. You probably won‘t need to use these with any frequency, but they come in handy when you need them.
Getting rpm to display architecture
This one is a pretty simple tip, and very useful especially for people using x86_64 systems. Just one line in ~/.rpmmacros will save all sorts of trouble later.
echo "%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}" >> ~/.rpmmacros Reset File Permissions
Have you managed to completely mess up file permissions for a given package? Not a problem, because RPM has you covered.
rpm --setperms View the Changelog
Because CentOS and the upstream vendor have backported security patches, the version numbers can often be misleading when you look for CVE fixes. Checking the changelog of a package is a good way to see if the fix has been implemented. Once again, rpm comes to the rescue.
rpm -q --changelog | lessUsing less isn‘t necessary, but for some packages like the kernel, the changelog can be quite long. Using less helps to make things more readable.
Where‘s the Documentation?
To quickly list documentation relating to a package, you can use the following two options:
rpm -qd This will show you the documentation contained in that rpm, or if you only have a filename you can do:
rpm -qdf /path/to/fileand rpm will show you the documentation in the package that owns the file.
Package Origin
Occasionally it‘s nice to know where you got certain packages, or how many packages you have on your system from a particular repository or vendor. There are a couple of search options that you can use which are not in the rpm man pages to help you out here. While they‘re not 100% perfect, they certainly help out. Most package repositories tag their packages with an identifier in the Release string. For examplerpmforge uses rf as their identifier. You can use this to see what you have of theirs installed with
rpm -qa release="*rf*"or if you want to see just how many packages you have installed built byJohnnyHughes you could use
rpm -qa packager="Johnny*"This trick works for most categories visible in rpm -qi
Extract just one File - If you need to extract just one file from an rpm without reinstalling the whole package, you can do this with rpm2cpio. For example, to extract just the config file from the logrotate rpm you would use the following statement:
rpm2cpio logrotate-1.0-1.i386.rpm |cpio -ivd etc/logrotate.conf Query Package Install Order and Dates - Useful after an upgrade to find old packages that were not upgraded.
rpm -qa --last >~/RPMS_by_Install_DateReview the end of the output file in "less" to find all RPMS older than the install date. Can also grep for specific packages and see when they were installed.
Query Available Packages from a Repo - Find all packages available from a specific repository, e.g. RPMforge.
yum --disable "*" --enable "rpmforge" list available Search yum repossitories for a string - Find packages containing a string in package name or description.
yum search buildrpmtree | less Get set up for rebuilding packages while not being root
Sometimes you just have to rebuild that package - maybe only to use some configuration option which just isn‘t there in the official package. Or because you have found some great package which you really cannot find in the repositories, but the site only gives you RPMs for another distribution. So you have to grab the src.rpm and rebuild it for yourself. But you really do not want to do it as root. So here‘s how to rebuild your packages in your home directory - with your own user account. Assume that you really want to do that in ~/redhat. Sure, you can move that somewhere else.
Method A:
First we setup the directory you want to be working in. Note the astounding similarity to the directory structure in /usr/src/redhat:
[testuser@shutdown ~]$ cd[testuser@shutdown ~]$ mkdir -p redhat/{SRPMS,RPMS,SPECS,BUILD,SOURCES}[testuser@shutdown ~]$ ls redhat/BUILD RPMS SOURCES SPECS SRPMS[testuser@shutdown ~]$Then we fiddle with some rpm macros, so rpmbuild knows about you and where you want to build:
[testuser@shutdown ~]$ echo "%_topdir /home/testuser/redhat" >> .rpmmacros[testuser@shutdown ~]$ echo "%packager Test User " >> .rpmmacros[testuser@shutdown ~]$ cat .rpmmacros%_topdir /home/testuser/redhat%packager Test User [testuser@shutdown ~]$That was it. The next time you issue rpmbuild --rebuild foo.src.rpm, your results will be in ~/redhat/RPMS/i386 (or whichever architecture you just built your package for). Easy, isn‘t it?
Method B for building RPMS as a user:
Configure the kbs-Extras repo (optionally add kbs-Misc) from theRepositories page and "yum install fedora-rpmdevtools" as root using "sudo" or "su -". As the build user (may want to use a special account for this to avoid problems in your normal user home directory) do "fedora-buildrpmtree" and an ~/rpmbuild/... directory tree and ~/.rpmmacros file will be automagically created. (Note "rpmbuild" versus "redhat" in Method A.)
The following addition to the macros may be needed to get proper names for some packages (substitute appropriate distro version for "el4" as required):
[testuser@shutdown ~]$ echo "%dist .el4" >> .rpmmacros
TipsAndTricks/YumAndRPM (2007-03-22 10:47:50由PhilSchaffner编辑)
from: http://wiki.centos.org/TipsAndTricks/YumAndRPM