How to Automate Postfix Installation on Ubuntu Without Interactive Configuration Prompts


2 views

When automating server provisioning, running apt-get install -y postfix still interrupts the process with configuration dialogs. This occurs because Postfix's Debian package uses debconf for initial configuration.


# This will still prompt for configuration
sudo apt-get install -y postfix

The solution is to pre-seed debconf values before installation. This tells the package manager what answers to use for configuration questions:


# Set debconf values non-interactively
echo "postfix postfix/mailname string example.com" | sudo debconf-set-selections
echo "postfix postfix/main_mailer_type string 'Internet Site'" | sudo debconf-set-selections

# Then install without prompts
sudo apt-get install -y postfix

As you suspected, Postfix won't prompt if it detects existing configuration. You can leverage this by:


# Create minimal configuration directory
sudo mkdir -p /etc/postfix
sudo touch /etc/postfix/main.cf

# Now installation will skip configuration
sudo apt-get install -y postfix

Here's a complete example for automated provisioning:


#!/bin/bash

# Set non-interactive frontend (just in case)
export DEBIAN_FRONTEND=noninteractive

# Pre-seed postfix configuration
debconf-set-selections <<< "postfix postfix/mailname string yourdomain.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"

# Install postfix quietly
apt-get update
apt-get install -y postfix

# Alternatively, create dummy config first
# mkdir -p /etc/postfix
# echo "myhostname = localhost" > /etc/postfix/main.cf
# apt-get install -y postfix

After automated installation, verify Postfix is running:


sudo systemctl status postfix
sudo postconf -n  # Show non-default configuration

For different mail server configurations, adjust the main_mailer_type:

  • 'Internet Site' - Direct SMTP delivery
  • 'Satellite system' - All mail relayed through another server
  • 'Local only' - No external mail delivery

When running apt-get install -y postfix in automated scripts, the installation pauses at the configuration screen, breaking the automation flow. This occurs because Postfix's debconf configuration requires user interaction by default.

The Postfix package uses debconf to set:

  • Mail server configuration type
  • System mail name
  • Root and postmaster mail recipients
  • Other network-related settings

We can use debconf-set-selections to provide answers before installation:

echo "postfix postfix/mailname string yourdomain.com" | sudo debconf-set-selections
echo "postfix postfix/main_mailer_type string 'Internet Site'" | sudo debconf-set-selections

For a complete non-interactive installation:

sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y postfix

For more complex deployments, create a preseed file:

# postfix.preseed
postfix postfix/main_mailer_type select Internet Site
postfix postfix/mailname string mail.example.com

Then apply it during installation:

sudo debconf-set-selections postfix.preseed
sudo apt-get install -y postfix

Check Postfix status after installation:

sudo systemctl status postfix
sudo postconf -n

Postfix won't prompt for configuration if /etc/postfix/main.cf exists. You can:

  1. Pre-create configuration files
  2. Use configuration management tools (Ansible, Puppet)
  3. Template the configuration during deployment

Example Ansible playbook snippet:

- name: Install Postfix non-interactively
  apt:
    name: postfix
    state: present
    debconf_selections:
      - "postfix postfix/main_mailer_type string Internet Site"
      - "postfix postfix/mailname string {{ mail_domain }}"