When automating server provisioning through bash scripts, package installation via apt-get install
becomes problematic when packages require interactive configuration. Common examples include:
sudo apt-get install -y mysql-server phpmyadmin postfix
The -y
flag only handles the initial installation confirmation, not subsequent configuration questions from debconf.
Debian's configuration management system (debconf) supports preseeding - providing answers to configuration questions before they're asked. We have several approaches:
First identify the configuration keys with:
sudo debconf-show mysql-server
sudo debconf-show phpmyadmin
Then pipe answers to debconf:
echo "mysql-server mysql-server/root_password password mypassword" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password mypassword" | sudo debconf-set-selections
echo "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2" | sudo debconf-set-selections
Create a preseed file (preseed.cfg
):
# MySQL
mysql-server mysql-server/root_password password mypassword
mysql-server mysql-server/root_password_again password mypassword
# phpMyAdmin
phpmyadmin phpmyadmin/dbconfig-install boolean true
phpmyadmin phpmyadmin/mysql/admin-pass password mypassword
Load it before installation:
sudo debconf-set-selections /path/to/preseed.cfg
Some packages support environment variables:
export DEBIAN_FRONTEND=noninteractive
export MYSQL_ROOT_PASSWORD='mypassword'
sudo -E apt-get install -y mysql-server
For sensitive data like passwords:
- Use environment variables instead of files
- Set file permissions (600) on preseed files
- Clean up preseed files after installation
#!/bin/bash
# Set non-interactive frontend
export DEBIAN_FRONTEND=noninteractive
# Preseed MySQL
echo "mysql-server mysql-server/root_password password ${MYSQL_PASS}" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password ${MYSQL_PASS}" | debconf-set-selections
# Install packages
apt-get update
apt-get install -y mysql-server phpmyadmin
# Clean up
unset DEBIAN_FRONTEND
When automating server provisioning, we often encounter interactive configuration prompts during package installation. For critical services like MySQL or phpMyAdmin, these prompts can break the automation flow. Here's how to handle them professionally.
The Debian configuration management system (debconf) provides multiple ways to preset answers:
# First, identify the configuration questions
sudo debconf-get-selections | grep mysql-server
For MySQL installation as an example:
echo "mysql-server-5.7 mysql-server/root_password password yourpassword" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password yourpassword" | sudo debconf-set-selections
sudo apt-get install -y mysql-server-5.7
Create a preseed file and use it during installation:
# mysql.preseed
mysql-server-5.7 mysql-server/root_password password yourpassword
mysql-server-5.7 mysql-server/root_password_again password yourpassword
phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2
phpmyadmin phpmyadmin/dbconfig-install boolean true
Then apply it:
sudo debconf-set-selections < mysql.preseed
sudo apt-get install -y mysql-server phpmyadmin
For more flexibility in scripts:
#!/bin/bash
MYSQL_PASS=$(openssl rand -base64 12)
debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_PASS"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $MYSQL_PASS"
apt-get install -y mysql-server
echo "MySQL root password: $MYSQL_PASS" > /root/mysql_credentials.txt
While convenient, be aware of these security implications:
- Passwords may appear in shell history
- Preseed files with passwords shouldn't be committed to version control
- Consider using configuration management tools (Ansible, Puppet) for production
If preseeding doesn't work:
- Verify package names match exactly what debconf expects
- Check question priorities (only critical and high priority questions can be preseeded)
- Look for typos in the preseeding format