Yum and RPM Tricks


Contents
  1. Getting rpm to display architecture
  2. Query packages not from CentOS
  3. Reset File Permissions
  4. View the Changelog
  5. Where's the Documentation?
  6. Package Origin
  7. Extract just one File
  8. Query Package Install Order and Dates
  9. Query Available Packages from a Repo
  10. Search yum repositories for a string
  11. Using yum with a proxy server
  12. Use yum to install a local package, automatically checking/satisfying dependencies
  13. Get set up for rebuilding packages while not being root
    1. Method A
    2. Method B
  14. Display priority scores for all repositories
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.

1. 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

2. Query packages not from CentOS

Want to query all those packages installed from 3rd party repositories, not CentOS?

rpm -qa --qf '%{NAME} %{VENDOR}\n' | grep -v CentOS

3. 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 

4. 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  | less
Using 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.

5. 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/file
  • and rpm will show you the documentation in the package that owns the file.

6. 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 example rpmforge 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 by JohnnyHughes you could use

rpm -qa packager="Johnny*"
This trick works for most categories visible in rpm -qi

7. 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

8. Query Package Install Order and Dates

Useful after an upgrade to find old packages that were not upgraded.

rpm -qa --last >~/RPMS_by_Install_Date
Review 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.

9. Query Available Packages from a Repo

Find all packages available from a specific repository, e.g. RPMforge. This does not show the already installed packages from this repository.

yum --disable "*" --enable "rpmforge" list available

10. Search yum repositories for a string

Find packages containing a string in package name or description.

yum search buildrpmtree | less

11. Using yum with a proxy server

To enable yum operations to use a proxy server you should first add the following parameter to /etc/yum.conf

proxy=http://yourproxy:8080/ 
where yourproxy is the name of the proxy server you want to access and 8080 is the proxy port. If the server requires authentication you can specify the login credentials like:

proxy=http://username:password@yourproxy:8080/
The rpm package manager makes use of the proxy environment variable. This can be set system wide in /etc/profile or user specific in ~/.bash_profile::

export http_proxy=http://yourproxy:8080/
export ftp_proxy=http://yourproxy:8080/
To use wget throug a proxy server add the following lines to /etc/wgetrc

http_proxy = http://yourproxy:8080/
ftp_proxy = http://yourproxy:8080/
In both cases, login information can be set like in the example above.

12. Use yum to install a local package, automatically checking/satisfying dependencies


yum --nogpgcheck localinstall packagename.arch.rpm

13. 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.

13.1. 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?

13.2. Method B

For CentOS-4, configure the kbs-Extras repo (optionally add kbs-Misc) from the Repositories 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.)
For CentOS-5 - A package for rpmdevtools does not seem to be available. The FC6 SRPM rpmdevtools-5.3-1.fc6.src.rpm builds and works.
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

14. Display priority scores for all repositories

You can list all repositories set up on your system by a yum repolist all. However, this does not show priority scores. Here's a one liner for that. If no number is defined, the default is the lowest priority (99).
cat /etc/yum.repos.d/*.repo | sed -n -e "/^\[/h; /priority *=/{ G; s/\n/ /; s/ity=/ity = /; p }"  | sort -k3n

0 comments:

Post a Comment