Getting a List of Packages and Package Sources from Ubuntu Linux

Originally published: 02/2007 by J Wynia

Lately, I'm finding myself setting up the same set of packages on Ubuntu as I build not just lots of virtual machines, but as I'm converting some of my Windows workstations and servers over to Ubuntu as well. To replicate the process, I looked at building a custom installation CD, but that seemd the wrong approach. At least in part because of the fact that each variation would require a new CD ISO and the storage requirements would get out of hand.

However, Debian and Ubuntu have a handy way to get a list of everything that the aptitude package manager is keeping track of. Since that package management system is the main reason *why* many people are using Ubuntu or Debian, that means pretty much everything that's installed. And, since this list is in plain text, it's easy to edit and tweak, with each configuration taking up next to no space.

This article shows a basic version of getting this list into a file and even emailing the file to get it off of the machine in question and into my email. The linked example does this with the SMTP server on the machine in question. However, some of the machines I'm working with don't have any email server installed. And, I don't want to have to install a complete mail server, just to send out a simple set of files here and there.

Fortunately, there's a nice little commandline app for sending email via an SMTP. You just put it in /usr/local/bin as "sendEmail". Then, the package dump script can use it to send out the email.

The linked example also doesn't include the "sources.list", which is the file that points to the list of repositories used to fetch the packages. You actually need that if any of the packages in the list come from non-standard repositories.

So, a robust script would dump the list of packages, grab a copy of the sources.list, bundle them together into a tar.gz tarball and send that file as an attachment. Now, if you look here into my hat, you'll see such a magical script.

This is saved in the home directory of my regular user on all of these servers as package-dump.sh, which I make executable with "chmod +x package-dump.sh". Then, I run it as "./package-dump.sh" and the email gets done. It obviously will ask for the root password if it's not already cached.

Each chunk should be on its own line.


mkdir package-dump


sudo dpkg --get-selections | grep "install" > package-dump/packages.txt


sudo cp /etc/apt/sources.list package-dump/sources.list


tar cfz packages.tar.gz package-dump


sudo rm -r package-dump


sendEmail -f FROMADDRESS -t TOADDRESS -s SMTPSERVER -xu SMTPUSER
-xp SMTPPASSWORD -u "Package List for Server" -m "Attached is a list
of packages and apt-get sources they came from." -a packages.tar.gz


rm packages.tar.gz


To load the list up on a fresh server, put the sources.list in the appropriate place and follow the instructions in the linked article.

Comments

James Ponza on 11/18/2008
It could be a good idea to also include /etc/apt/preferences if it exists - since it may have been used to customise the way the packages sources get used, or even which packages come from which repo...
James Ponza on 11/18/2008
adding this does the trick: sudo test -f /etc/apt/preferences && cp /etc/apt/preferences package-dump/preferences although there's no reason why we shouldn't simply do: sudo cp -a /etc/apt package-dump/apt
blog comments powered by Disqus
Or, browse the archives.
© 2003- 2014 J Wynia. Very Few Rights Reserved. This article is licensed under the terms of the Creative Commons Attribution License. Quoted content or content included from others is not subject to that license and defaults to normal copyright.