Restore Ubuntu and derivatives to the default settings

Ubuntu, and to some degree its derivatives, is the most popular Linux distribution available for personal computers and laptops and due to its simplicity of use it is often chosen by the new users who decide to migrate from Microsoft Windows and Apple OSX to the Penguin’s world.

Like with any other new tech toy, a Linux novice may be tempted to experiment with the new system by keep installing and uninstalling software and sometime even configure new repositories to the package manager if a particular program isn’t available on the default ones. While generally Linux can better stand the test of time than Windows, this continuous experimentation can still lead the system to sluggishness and software misbehavior.

On the Windows’s world it is a common procedure to fix this kind of problem with a disk format and a clean install, but on Linux it is possible to restore Ubuntu, and derivatives, to the default settings without such drastic measure.

Restore Ubuntu list of installed packages

Restore Ubuntu and derivatives to the default settings

First a cautious warning: while the following procedure preserve your data, it is still highly recommended that you back it up to an external media. Backups should be something that you already do regularly regardless of the status of your system, but it is even more important when you do a heavy maintenance procedure to your computer.

To restore Ubuntu and its derivatives to the default settings you still need the installation CD or USB pen drive. Locate it and plug it on your computer (on a running instance of Ubuntu you don’t have to boot the computer with it).

Once the installation media is plugged in, you need to open it with Nautilus (or whatever file manager is installed by the derivative you are using, for example Dolphin for Kubuntu), locate the following 2 files and copy them on any location on our computer:

  • casper/filesystem.manifest
  • casper/filesystem.manifest-remove

Once you done it, open a terminal, go to the location where you saved those file and run the following command:
comm -3 < (cat filesystem.manifest | awk '{print $1}' | sort) < (cat filesystem.manifest-remove | sort) > initial_packages

The above command, in conjunction with the files extracted from the installation media, will generate a file with the list of the packages installed by default by the Ubuntu’s installer.

Still from the terminal, run the following command:
dpkg --get-selections | awk '{print $1}' | sort > installed_packages

The above command will generate a file with the list of all the packages currently installed on your computer.

The next command to run is:
diff -u initial_packages installed_packages | grep "^+[^+]" | cut -c 2- > added_packages

The above command will generate a file with the list of all the packages you installed in addition to those installed by default.

Then with this similar command:
diff -u initial_packages installed_packages | grep "^-[^-]" | cut -c 2- > removed_packages
you will generate a file with the list of all the packages that the Ubuntu’s installer installed but you removed later on.

Still from the terminal, remove all the packages you have additionally installed:
sudo apt-get purge $(cat added_packages)

Leaving the terminal open (there are few more commands later on), open the package manager and remove any additional repository you have added; the way to do it is similar to the one you used to add them, but if you are unsure on how to do it you can follow the second part of this guide on how to configure new repositories to the package manager.

Return to the terminal reinstall the packages you removed with:
sudo apt-get install $ (cat removed_packages)

For the above step it was important to remove the additional repositories first because they may contain a different version of a package present on the default repositories and this difference may lead to misbehavior due to incompatibilities with other packages.

You can now delete the above generated files; to recap:

  • filesystem.manifest
  • filesystem.manifest-remove
  • initial_packages
  • installed_packages
  • added_packages
  • removed_packages

Restore Ubuntu actual settings

If you ever modified a system-wide configuration file (those under the /etc directory and not those under your personal /home/[username] directory) of any packages installed by the Ubuntu’s installer and that you kept (and therefore not touched by the above commands), you can reset them with the following command:
sudo dpkg-reconfigure packageName
Of course change ‘packageName‘ with the specific package name; if you don’t know the package you can use the following command:
dpkg -S name-of-file.conf
For name-of-file.conf obviously put the name of the configuration file you have changed.

The next step to restore Ubuntu and derivatives to the default settings is to reset the user-wide configuration files. You can accomplish it with following command:
rm -r ~/.config/ ~/.cache/

Most Linux applications are standard compliant therefore with the above command you have reset their config without any personal data loss. If you know that some of the installed program isn’t standard compliant regarding where to store its config file, you should manually check (and eventually search online for specific information) under your home directory to find such files are stored and delete them

There is actually a command that delete all configuration files under your home directory, but it will also delete some user information, such as your browser stored login information, your email credentials, save game files, etc., so be mindful if you really want to use it instead of manually hunt and delete the configuration files:
rm -rf ~/.[^.]*

Conclusion

Once you have completed all the above steps, reboot your computer and you will have your Ubuntu installation as good as new.