I have a Kali Linux installed recently, but due to its inflexible mirror selection, all deb packages are downloaded from a mirror that I don't trust. Is it possible to tell apt / dselect to re-download and reinstall all packages, assuming sources.list has been updated to use new mirror?

share|improve this question
2  
apt-get clean && apt-get install --reinstall $(dpkg --get-selections | grep -w 'install$' | cut -f 1)? – Alex Oct 16 '14 at 15:30
up vote 11 down vote accepted

As Alex pointed out:

sudo apt-get clean
sudo apt-get install --reinstall $(dpkg --get-selections | grep -w 'install$' | cut -f 1)

The first one makes sure apt's cache is empty. That way apt-get will need to download the packages from the repositories. The second, first you need a list of all installed packages, which is what the $(...) part is doing, and then you are using apt-get to reinstall them. There's another way with aptitude:

sudo aptitude --reinstall install '~i'

The ~i search for "installed packages".

For both methods you will end without any automatically installed packages, so you must use apt-mark showauto > packages before doing this, and sudo apt-mark auto $(cat packages) to reestablish the list.

share|improve this answer
    
Urgh, actually I have accepted the answer too early. There are lots of dependency cycle problem so that certain packages have to be installed first. Anyway this is a good enough starting point. – Abel Cheung Oct 21 '14 at 23:45
    
For a somewhat full installation, these packages need to be installed first: dpkg, debconf, liblocale-gettext-perl, gawk, foomatic-filters (printing is not part of base system though). Then install all the remaining ones. – Abel Cheung Oct 21 '14 at 23:54
    
Both of these answers cause dependency cycles for me. – mlissner Mar 6 '15 at 7:13
1  
Simply sudo aptitude reinstall '~i' with aptitude 0.7.3 – Richard Ayotte Oct 15 '15 at 0:43
1  
@Braiam The aptitude command fails, cause it cannot download and reinstall the package. Is there a way to workaround that without uninstalling the packages? – Sven Sep 19 '16 at 13:45

The answers here helped me solve the problem, this solution probably not the most efficient but gets the job done.

#!/bin/bash
for package in `dpkg --get-selections | grep -w 'install$' | cut -f 1`; do
    apt-get install --reinstall $package;
done
share|improve this answer
    
Thanks, this method of trying one-at-a-time gets around any errors from packages which can't be downloaded from apt repo. – MrYellow Aug 9 '16 at 2:29

I used this script and was asked from time to time to give a Y(es) Than the script went on. But because it takes quite a while this makes the time to complete much longer. I changed the script with inserting --yes force-yes Recommend forcing automatic def/old config acceptance as well

#!/bin/bash 
for package in `dpkg --get-selections | grep -w 'install$' | cut -f 1`; do
    apt-get --yes --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install --reinstall $package
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.