找到(并重新安装)包含损坏文件的包(不破坏任何内容)

我通常更喜欢修复Linux系统,而不是从头开始重新安装。 我的计算机已经看到许多分发升级和PPA或第三方存储库列表。 APT通常会确保一切顺利进行。 但是,程序包管理器认为所有必需的程序包都已“安装”并不能保证文件系统上存在所有文件。

如果您必须使用dpkg --force-*解决依赖性问题,则可能会出现这种情况。 也可以通过从root用户删除/usr的文件来重现这种情况。

有一种简单的方法可以validation属于已安装软件包的所有文件是否存在?

如果找到这样的问题包, aptitude reinstall解决问题。

debsums手册页:

 apt-get install --reinstall $(dpkg -S $(debsums -c) | cut -d : -f 1 | sort -u) Reinstalls packages with changed files. 

我刚刚在我的系统上运行了这个,因为在我试用测试版操作系统时,磁盘内容被随机破坏了。 这就是我所做的(它似乎运作良好):

首先,我安装了’debsums’并运行它以查看我的系统上是否有任何损坏的文件:

 $ sudo apt-get install debsums $ sudo debsums_init $ sudo debsums -cs /usr/share/bash-completion/completions/ssh /usr/share/icons/hicolor/scalable/actions/cheese-take-photo.svg /usr/share/gnome/help/gnumeric/C/files-textopen.xml /usr/share/dbus-1/services/indicator-sound.service /lib/modules/3.11.0-12-generic/kernel/drivers/mtd/ubi/ubi.ko 

如您所见,我有五个损坏的文件,所以我需要重新安装它们。 这是我发现哪些包包含损坏文件的方式:

 $ sudo debsums -c | xargs -rd '\n' -- dpkg -S | cut -d : -f 1 | sort -u bash-completion cheese-common gnumeric-doc indicator-sound linux-image-extra-3.11.0-12-generic 

然后我通过重新安装损坏的软件包来修复损坏:

 $ xargs -rd '\n' -a <(sudo debsums -c | xargs -rd '\n' -- dpkg -S | cut -d : -f 1 | sort -u) -- sudo apt-get install -f --reinstall -- Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: linux-image-generic Use 'apt-get autoremove' to remove it. 0 upgraded, 0 newly installed, 5 reinstalled, 0 to remove and 0 not upgraded. Need to get 43.9 MB of archives. After this operation, 0 B of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu/ saucy/main bash-completion all 1:2.0-1ubuntu3 [173 kB] Get:2 http://us.archive.ubuntu.com/ubuntu/ saucy/main cheese-common all 3.8.3-0ubuntu1 [2,929 kB] Get:3 http://us.archive.ubuntu.com/ubuntu/ saucy/universe gnumeric-doc all 1.12.6-1 [7,295 kB] Get:4 http://us.archive.ubuntu.com/ubuntu/ saucy/main linux-image-extra-3.11.0-12-generic i386 3.11.0-12.19 [33.5 MB] Get:5 http://us.archive.ubuntu.com/ubuntu/ saucy/main indicator-sound i386 12.10.2+13.10.20131011-0ubuntu1 [55.7 kB] Fetched 43.9 MB in 10min 23s (70.4 kB/s) (Reading database ... 174913 files and directories currently installed.) Preparing to replace bash-completion 1:2.0-1ubuntu3 (using .../bash-completion_1%3a2.0-1ubuntu3_all.deb) ... Unpacking replacement bash-completion ... Preparing to replace cheese-common 3.8.3-0ubuntu1 (using .../cheese-common_3.8.3-0ubuntu1_all.deb) ... Unpacking replacement cheese-common ... Preparing to replace gnumeric-doc 1.12.6-1 (using .../gnumeric-doc_1.12.6-1_all.deb) ... Unpacking replacement gnumeric-doc ... Preparing to replace linux-image-extra-3.11.0-12-generic 3.11.0-12.19 (using .../linux-image-extra-3.11.0-12-generic_3.11.0-12.19_i386.deb) ... Unpacking replacement linux-image-extra-3.11.0-12-generic ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic Preparing to replace indicator-sound 12.10.2+13.10.20131011-0ubuntu1 (using .../indicator-sound_12.10.2+13.10.20131011-0ubuntu1_i386.deb) ... Unpacking replacement indicator-sound ... Processing triggers for man-db ... Processing triggers for libglib2.0-0:i386 ... No such key 'auto-launch' in schema 'com.ubuntu.update-notifier' as specified in override file '/usr/share/glib-2.0/schemas/20_xubuntu-default-settings.gschema.override'; ignoring override for this key. Processing triggers for hicolor-icon-theme ... Setting up bash-completion (1:2.0-1ubuntu3) ... Setting up cheese-common (3.8.3-0ubuntu1) ... Setting up gnumeric-doc (1.12.6-1) ... Setting up linux-image-extra-3.11.0-12-generic (3.11.0-12.19) ... Running depmod. update-initramfs: deferring update (hook will be called later) Not updating initrd symbolic links since we are being updated/reinstalled (3.11.0-12.19 was configured last, according to dpkg) Not updating image symbolic links since we are being updated/reinstalled (3.11.0-12.19 was configured last, according to dpkg) Examining /etc/kernel/postinst.d. run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic run-parts: executing /etc/kernel/postinst.d/dkms 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic update-initramfs: Generating /boot/initrd.img-3.11.0-12-generic run-parts: executing /etc/kernel/postinst.d/pm-utils 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic run-parts: executing /etc/kernel/postinst.d/update-notifier 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic run-parts: executing /etc/kernel/postinst.d/zz-update-grub 3.11.0-12-generic /boot/vmlinuz-3.11.0-12-generic Generating grub.cfg ... Found linux image: /boot/vmlinuz-3.11.0-14-generic Found initrd image: /boot/initrd.img-3.11.0-14-generic Found linux image: /boot/vmlinuz-3.11.0-12-generic Found initrd image: /boot/initrd.img-3.11.0-12-generic Found memtest86+ image: /boot/memtest86+.bin Found Windows 7 (loader) on /dev/sda1 done Setting up indicator-sound (12.10.2+13.10.20131011-0ubuntu1) ... 

最后,我检查确保没有损坏的文件:

 $ sudo debsums -c 

此命令没有输出,这意味着没有发现错误。 🙂

最后要注意的是:您还应该检查包的配置文件,以确保它们没问题。 这可能更加困难,因为配置文件经常更改,并且更改是合法的,因此您需要手动检查每个更改的配置文件以确定它是否实际损坏。 这是获取已更改配置文件列表的方法:

 $ sudo debsums -as debsums: changed file /etc/gnome/defaults.list (from desktop-file-utils package) debsums: changed file /etc/default/rcS (from initscripts package) debsums: changed file /etc/subuid (from login package) debsums: changed file /etc/subgid (from login package) debsums: changed file /etc/sudoers (from sudo package) 

PeniWize提供的脚本非常适用于损坏的文件,但不会处理丢失文件的包,因为debsums会将它们报告给stderr。 要重新安装包含丢失文件的软件包,这对我有用:

 xargs -rd '\n' -a <(sudo debsums -c 2>&1 | cut -d " " -f 4 | sort -u | xargs -rd '\n' -- dpkg -S | cut -d : -f 1 | sort -u) -- sudo apt-get install -f --reinstall -- 

这个问题已在其他地方得到解答

是否有Ubuntu健全检查? :package debsums可以计算MD5哈希值并与deb包进行比较。

是否有安全的方法通过包管理器重新安装 :是的,但不推荐。

使用dpkg程序以及一些Bash脚本应该能够为您提供此function。 唯一的警告是,如果有人用“恶意”文件替换“干净”版本的文件。 为此,您需要从pristine包中获取有效的MD5校验和。 无论如何,这里是实现你想要的shell代码:

 #!/bin/sh PACKAGE_NAME="xterm" for i in `sudo dpkg -L ${PACKAGE_NAME}` do if ! [ -e $i ]; then echo "$i is a missing file in the $PACKAGE_NAME package." fi done 

只有在包中定义的文件或目录丢失时,脚本才会打印出来。 此外,您还需要将“PACKAGE_NAME”变量替换为您要检查的包。 希望这可以帮助。