Fully automated ubuntu server setups using preseed
When you are building a server on a cloud like Amazon’s EC2, its quite typical to create a shell script to automate the process. This saves you time when you need to create a test server or god-forbid, your production server dies. But one of the challenges in creating these scripts is that some packages display a UI asking for user input. Two common examples of this are mysql (root password) and postfix (server type, root email, etc).
The solution to this is to use preseeding. This is where you tell the debian installer, in advance, the response to each of the questions it would normally ask when it installs the package. The command-line tools used to preseed are part of the debconf-utils package. So the 1st step is to make sure these are installed.
sudo apt-get -y install debconf-utils
Now you can go ahead and figure out which settings need to be set for each package. Let’s use mysql as an example. The easiest way to do this is to install the package manually and then use the debconfig-get-selections command to query the list of settings.
# sudo apt-get -y install mysql-server
# sudo debconf-get-selections | grep mysql
...
mysql-server-5.1 mysql-server/root_password_again password
mysql-server-5.1 mysql-server/root_password password
mysql-server-5.1 mysql-server/start_on_boot boolean true
So what we need to do now is create a preseed file with the above settings and then pass it to the debian installer using the debconf-set-selections command.
# echo "mysql-server-5.1 mysql-server/root_password password $MYSQL_ROOT_PWD" > mysql.preseed
# echo "mysql-server-5.1 mysql-server/root_password_again password $MYSQL_ROOT_PWD" >> mysql.preseed
# echo "mysql-server-5.1 mysql-server/start_on_boot boolean true" >> mysql.preseed
# cat mysql.preseed | sudo debconf-set-selections
# apt-get -y install mysql-server
Once the values have been set, you can run apt-get to install the package without prompting for any inputs.
One final note, if you have a lot of servers or rebuild them on a regular basis, you should probably be looking at Puppet or Chef.
September 11th, 2010 at 3:07 pm
Hi,
I am considering to try Ubuntu myself. I am getting the iso right now. While getting it i was googling for some info on Ubuntu. That is how i came acros your blog post. Thanks for the information you shared, i will certanly use it when i am going to use Ubuntu.
Thanks!
December 7th, 2010 at 8:44 pm
While reviewing this process today, I notice that the preseed works IF the mysql-server has already been installed and then removed? Unless I am missing somthing, which is entirely likely.
How can I completely clean the debconf db from any/all mysql-server entries so that I can be assured that the debconf-get-selections | grep mysql comes back empty?
December 7th, 2010 at 8:53 pm
sudo apt-get purge mysql-server seems to be doing the job.
TY.
December 8th, 2010 at 8:25 am
Preseed works before a package has been installed. It allows the setup of a new server through an automated script without needing human interaction.
June 22nd, 2011 at 10:23 am
Thanks,
Nice posting spent whole night thinking how to achieve this. Got this link finally.
January 18th, 2012 at 2:53 pm
Automating mysql-server’s installation was the last domino i needed to knock over. Thanks!