The Problem
When executing rpm command, the following error is seen:
rpm: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file
The zlib shard libraries are missing:
# ls -l /lib64/libz.so* ls: /lib64/libz.so*: No such file or directory # ls -l /lib/libz.so* ls: /lib/libz.so*: No such file or directory
The Solution
The zlib was broken, normally zlib contains the following files.
On x86_64 platform:
# rpm -ql zlib-1.2.3-27.el6.x86_64 /lib64/libz.so.1 /lib64/libz.so.1.2.3 /usr/share/doc/zlib-1.2.3 /usr/share/doc/zlib-1.2.3/ChangeLog /usr/share/doc/zlib-1.2.3/FAQ /usr/share/doc/zlib-1.2.3/README
On x86 platform:
# rpm -ql zlib-1.2.3-27.el6.i686 /lib/libz.so.1 /lib/libz.so.1.2.3 /usr/share/doc/zlib-1.2.3 /usr/share/doc/zlib-1.2.3/ChangeLog /usr/share/doc/zlib-1.2.3/FAQ /usr/share/doc/zlib-1.2.3/README
While the zlib is broken, the rpm command is broken as well. So we can’t simply use rpm or yum command to reinstall the zlib. What you can do here is:
1. On another CentOS/RHEL server, extract zlib share libraries from a rpm with same version (e.g. zlib-1.2.3-27.el6 here).
2. Decompress the rpm:
# rpm2cpio zlib-1.2.3-27.el6.[arch].rpm | cpio -idvm
3. Subsitute [arch] to x86_64 or i686 depending on your server architecture:
# rpm2cpio zlib-1.2.3-27.el6.x86_64.rpm | cpio -idvm ./lib64/libz.so.1 ./lib64/libz.so.1.2.3 ./usr/share/doc/zlib-1.2.3 ./usr/share/doc/zlib-1.2.3/ChangeLog ./usr/share/doc/zlib-1.2.3/FAQ ./usr/share/doc/zlib-1.2.3/README 300 blocks
4. Move the lib.so.1.2.3 and lib.so.1 to the correct location:
On x86_64 platform:
# mv ./lib64/libz.so.* /lib64
On x86 platform:
# mv ./lib/libz.so.* /lib
5. Rebuild the library cache:
# ldconfig
Now the rpm command should run as normal.
6. Optionally, reinstall the zlib to fix other possible issues:
# yum reinstall zlib
Notes
Similar issue can occur for other packages like mentioned below:
rpm: error while loading shared libraries: libdb-4.7.so: cannot open shared object file: No such file or directory
In order to resolve this, find which package supplies the missing file:
# yum provides */libdb* Loaded plugins: security, ulninfo db4-4.7.25-19.el6_6.i686 : The Berkeley DB database library (version 4) for C Repo : ol6_latest_local Matched from: Filename : /usr/lib/libdb-4.7.so Filename : /lib/libdb-4.7.so
Reinstall the package, or copy the missing file extracting the rpm.