Install Postfix noninteractively on Ubuntu
Preseed debconf answers, install Postfix without prompts, and apply a deliberately scoped mail configuration before testing delivery.
Preseed the Postfix package's debconf questions, then run the package installation with DEBIAN_FRONTEND=noninteractive. This avoids the interactive mail-type and mail-name prompts. It does not configure a complete production mail service by itself.
Choose the intended role before installation. Internet Site configures direct SMTP delivery, Local only keeps mail local, and a third-party relay requires the appropriate relayhost, TLS, and authentication configuration.
Shell example for local application submission
The following example uses Internet Site but restricts SMTP listening to loopback. Replace the example hostnames before use. Apply it to a host whose package startup and network exposure you have already controlled; package installation can start the service before later commands run.
#!/bin/bash
set -euo pipefail
printf '%s\n' \
'postfix postfix/main_mailer_type select Internet Site' \
'postfix postfix/mailname string example.com' \
| sudo debconf-set-selections
sudo apt-get update
sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y postfix
sudo postconf -e 'myhostname = mail.example.com'
sudo postconf -e 'mydestination = $myhostname, localhost.$mydomain, localhost'
sudo postconf -e 'inet_interfaces = loopback-only'
sudo postfix check
sudo systemctl enable postfix
sudo systemctl restart postfix
mailname commonly supplies the domain used to qualify local mail through Debian/Ubuntu's /etc/mailname configuration. It need not be identical to the server's myhostname. The quoted $myhostname expressions are Postfix substitutions, not shell variables.
This sample does not declare every address at example.com to be a local UNIX mailbox. Add hosted domains only to the correct Postfix address class. If remote machines must submit, deliberately configure listeners, TLS, authentication or trusted client addresses, and firewall rules for that role.
Changing inet_interfaces requires a stop/start, which is why the example restarts rather than merely enabling an already running service.
Preseed with Ansible
Run these tasks with privilege escalation:
- name: Preseed Postfix package questions
ansible.builtin.debconf:
name: postfix
question: "{{ item.question }}"
value: "{{ item.value }}"
vtype: "{{ item.type }}"
loop:
- { question: 'postfix/main_mailer_type', value: 'Internet Site', type: 'select' }
- { question: 'postfix/mailname', value: 'example.com', type: 'string' }
- name: Install Postfix
ansible.builtin.apt:
name: postfix
state: present
update_cache: true
environment:
DEBIAN_FRONTEND: noninteractive
Manage main.cf, master.cf, credentials, and service handlers in subsequent tasks. Treat the two tasks above as package installation, not a complete role. Use a restart handler for interface changes and the appropriate reload behavior for other settings.
Verify delivery
If a compatible mail utility is installed, send a test to an address you control:
printf 'Postfix installation test\n' | mail -s 'Postfix test' ops@example.com
postqueue -p
Check the configured mail log destination, often /var/log/mail.log, and follow the queue ID through to its final status. A running service or an empty local queue does not prove the recipient received the message.
If direct outbound port 25 is restricted in the hosting environment, use the provider's supported authenticated relay route. Port 587 is submission to such a relay, not a substitute destination port for arbitrary recipient MX servers.
Existing installations
Preseeding is intended to answer package questions; it is not a reliable way to overwrite a previously configured server. Manage existing settings explicitly and preserve a backup. Reinstalling a package normally preserves administrator configuration, whereas purging can remove it. Do not purge a working server just to avoid prompts.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan