Linux: reinstall the package containing a command

If you need to reinstall the package containing a command e.g. ldd, you can use the following command:

aptitude reinstall `dpkg -S \`which ldd\` | awk -F: '{print $1;}'`

This will output something like this:
The following packages will be REINSTALLED:
0 packages upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 0 B/749 kB of archives. After unpacking 0 B will be used.
(Reading database ... 106282 files and directories currently installed.)
Preparing to replace libc-bin 2.11.3-3 (using .../libc-bin_2.11.3-3_amd64.deb) ...
Unpacking replacement libc-bin ...
Processing triggers for man-db ...
Setting up libc-bin (2.11.3-3) ...

This basically does the following:

which ldd

Which will return the full path to ldd e.g. /usr/bin/ldd. Then:

dpkg -S /usr/bin/ldd

which finds the package owning the file i.e.:
libc-bin: /usr/bin/ldd

awk -F: '{print $1;}' just splits the string by colons and returns the first part i.e. the package name (libc-bin).


aptitude reinstall libc-bin

which reinstalls this package.

Note that this is useful to restore original commands when a system has been compromised and these commands have been replaced by malicious code BUT even if you think you’ve rolled back all changes made by an intruder, your system will only be safe after a reinstall of the whole system. But if a reinstall is very time consuming and you need to temporary fix until you can afford to reinstall, this may come in handy.

Leave a Reply

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