Rebuilding corrupted RPM database on a CentOS/RHEL

One of our clients reported that he is getting an error and RPM database is corrupted. He is using Red Hat Enterprise Linux. Sometime it is possible to fix RPM database errors. I am surprised that many Linux admins do not make back up of RPM DB (/var/lib/rpm). Anyways if you ever messed up with RPM database, here is a quick guide to fix it (you must have rpmdb tools installed). This page shows how to rebuild RPM database on a Red Hat Enterprise Linux system or CentOS Community Enterprise Linux or Oracle Linux system or Scientific Linux.

ADVERTISEMENTS

Typical error message you get from rpm/yum

You might something as follows:

rpm command terminates with “cannot open Packages database in /var/lib/rpm”

OR

rpmdb: Lock table is out of available locker entries

OR

rpmdb: /var/lib/rpm/Packages: unexpected file type or format
error: cannot open Packages index using db3 – Invalid argument (22)

OR

error: rpmdbNextIterator: skipping h# 1601 Header V4 RSA/SHA1 signature: BAD, key ID 2142eef7

Rebuilding corrupted RPM database on a CentOS/RHEL

Rebuilding corrupted RPM database on a CentOS/RHEL
Take system to single user mode to avoid further damage and to make backup/restore process easy:
# init 1 # [ DO NOT RUN on remote system ]

Method # 1

Let us verify integrity of the packages file. Make a backup of /var/lib/rpm/:
# mkdir -vp /backups/rpm/
# cp -avr /var/lib/rpm/* /backups/rpm/

Remove /var/lib/rpm/__db* files to avoid stale locks using rpm command:
# cd /var/lib/rpm/
# rm -vf __db*

Rebuild RPM database:
# rpm --rebuilddb -vv
# rpmdb_verify Packages

Method # 2

If you are still getting errors, then try your luck with following commands:

# mv Packages Packages-BAKUP
# db_dump Packages-BAKUP | db_load Packages
# rpm -qa
# rpm --rebuilddb

Further reading

  • rpm command – rpm is a powerful Package Manager for Linux. Often used to build, install, query, verify, update, and erase individual software packages
  • yum command – yum is an interactive, rpm based, package manager. It can automatically perform system updates, including dependency analysis and obsolete processing based on “repository” metadata. It can also perform installation of new packages, removal of old packages and perform queries on the installed and/or available packages.
  • I recommend all of you visiting Daniel Berrange excellent guide for more information.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
16 comments… add one
  • Paul Blonde Dec 5, 2006 @ 23:25

    Method 2 does not work, as the db_load and db_dump commands were not found. Needless to say if what came before worked I wouldn’t have gotten to 2.

  • 🐧 nixCraft Dec 5, 2006 @ 23:35

    whereis db_dump db_load
    db_dump: /usr/bin/db_dump
    db_load: /usr/bin/db_load

    These utilities get installed by default using db4-utils package. Above instructions are tested on Fedora Core and RHEL 4 only.

  • Tapas Mallick Aug 18, 2008 @ 13:18

    How to recreate /var/lib/rpm/Packages (If original got deleted/corrupted) ?

  • James Cassell Oct 10, 2008 @ 17:46

    Thank You! you saved my day.

  • James Cassell Oct 10, 2008 @ 17:46

    Thank You! you saved my day

  • Tapas Mallick Oct 13, 2008 @ 7:55

    I found the same post/solution before in nixcraft and it is really a great solution but my question is how to recreate /var/lib/Packages if the original got “DELETED”.

  • com4host.com Sep 18, 2009 @ 17:43

    Thank You! you saved my day.

  • michael Nov 8, 2009 @ 15:09

    first command is cd /var/lib/rpm and not /var/lib

  • Anonymous May 17, 2010 @ 18:14

    cd /var/lib/rpm

    not #cd /var/lib/

  • mhelix Oct 18, 2012 @ 14:59

    nixcraft to the rescue!

    • Lyubo Mar 7, 2014 @ 6:48

      For me it worked this one :
      rpm -e –justdb

  • Lubos Aug 27, 2014 @ 7:12

    Hallo and thanks for howto.
    But you have small syntactic error at method1.
    cd /var/lib/rmp instead of cd /var/lib

  • Aleksey Tsalolikhin Aug 12, 2015 @ 16:17

    Thanks for a great site. It’s a fantastic resource for the sysadmin community.
    The code in Method 1 should be “cd /var/lib/rpm” rather than “cd /var/lib”. Please fix? 🙂

  • Mimmus Jan 14, 2016 @ 15:50

    Thank you.
    You solved an infinite loop of RPMs dependencies.

    Please fix typo errors as suggested

  • krish Jun 3, 2016 @ 14:18

    remove that init 1 command from above, it will take the remote machine off network .. and someone from DC or physically reboot the server.

  • raj Nov 28, 2016 @ 5:45

    Thanks..

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.