Skip to content

Installing Postfix on Ubuntu the right way

A grown-up Postfix install on Ubuntu — packages, identities, masquerading, hostname/PTR sanity, and the operational checks before the box starts talking to the open internet.

A fresh Ubuntu box and a directive to “make it send mail.” This article is the install procedure we follow before any TLS, DKIM, or relay work begins — get the package on, set the identities cleanly, confirm SMTP listens, and prove the box can deliver a test message without immediately landing in spam. The goal is a Postfix that knows who it is, where its mail spool lives, and what it is allowed to relay.

How to verify

Before installing, audit the network and identity of the machine. Mail servers are unforgiving — a wrong hostname or a missing reverse DNS will leak into every Received header you ever send.

hostname -f
getent hosts $(hostname -f)
dig +short $(hostname -f) A
dig +short -x $(curl -s ifconfig.me)
ss -ltnp | grep -E ':(25|587|465)\b'
dpkg -l | grep -E 'postfix|exim|sendmail'

The forward and reverse must match the FQDN you intend to use as myhostname. If dig -x returns the cloud provider’s default name (ec2-...compute.amazonaws.com), open a ticket with the provider for a custom PTR before you go further — see Bind9 reverse PTR for mail.

What’s happening

Postfix on Ubuntu ships as the postfix package. The install runs a debconf wizard that asks for a “general type of mail configuration” (Internet Site, Internet with smarthost, Satellite, Local only) and a system mail name. The wizard writes /etc/postfix/main.cf with sensible defaults: it sets myhostname, mydestination, inet_interfaces, and creates /etc/mailname. A systemd unit is registered, the master daemon starts listening on port 25, and a small set of helper binaries land in /usr/sbin/ (postqueue, postsuper, postconf, postmap).

The two files you will touch most are:

  • /etc/postfix/main.cf — parameter file. Every option has a default; postconf -d shows defaults, postconf -n shows non-defaults.
  • /etc/postfix/master.cf — service file. Defines the daemons that run inside the master process (smtpd on 25/587/465, pickup, cleanup, qmgr, smtp client, etc.).

Mail spool layout is FHS-standard: /var/spool/postfix/ with subdirectories incoming, active, deferred, bounce, corrupt, defer, hold. A healthy Postfix has a near-empty active and small steady-state deferred. A growing deferred queue is your first signal of a delivery problem.

The procedure

  1. Set the hostname and /etc/hosts cleanly before installing. Postfix reads the FQDN at install time and bakes it into main.cf:

    sudo hostnamectl set-hostname mx1.example.com
    echo "127.0.1.1 mx1.example.com mx1" | sudo tee -a /etc/hosts
    hostname -f   # must return mx1.example.com
  2. Install non-interactively with debconf preseed. Avoid the TUI wizard when you are scripting — set the answers up front:

    sudo debconf-set-selections <<< "postfix postfix/mailname string mx1.example.com"
    sudo debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
    sudo DEBIAN_FRONTEND=noninteractive apt install -y postfix mailutils
  3. Confirm the service is up and listening on 25. The master process forks the smtpd daemon on the configured ports:

    systemctl is-active postfix
    systemctl is-enabled postfix
    ss -ltnp | grep :25
    sudo postfix check
    sudo postconf -n | head -30
  4. Set the identities in main.cf explicitly. Do not rely on defaults — declare them. Edit /etc/postfix/main.cf:

    myhostname = mx1.example.com
    mydomain = example.com
    myorigin = $mydomain
    mydestination = $myhostname, localhost.$mydomain, localhost
    inet_interfaces = all
    inet_protocols = ipv4
    mynetworks = 127.0.0.0/8 [::1]/128
    smtpd_banner = $myhostname ESMTP
    biff = no
    append_dot_mydomain = no

    mynetworks is the relay-allow list — leave it as loopback only until you know which subnets need to relay through this host.

  5. Reload and tail the log. Postfix re-reads config on reload; a syntax error in main.cf is caught by postfix check before you reload:

    sudo postfix check
    sudo systemctl reload postfix
    sudo journalctl -u postfix -f
  6. Send a test message through sendmail and confirm queue behavior. From the box itself, send to an external mailbox you control:

    echo "test from $(hostname -f) at $(date -u +%FT%TZ)" \
      | mail -s "postfix smoke test" you@external-domain.com
    sudo postqueue -p

    If the queue empties within a few seconds, delivery worked. If the message sits in deferred, run postqueue -p and inspect the reason field — usually DNS or a missing PTR.

  7. Lock down before opening port 25 to the world. The default smtpd_relay_restrictions block allows mail from mynetworks and rejects unauthenticated relay attempts, but make it explicit:

    smtpd_relay_restrictions =
        permit_mynetworks
        permit_sasl_authenticated
        defer_unauth_destination

    Reload, then run a third-party open-relay check (e.g. mxtoolbox.com or swaks --to test@external --server $(hostname -f)) before declaring the install complete.

Common pitfalls

  • Installing Postfix with a hostname that is localhost or ubuntu bakes that into myhostname and every Received header — set the hostname before apt install.
  • The default inet_protocols = all causes IPv6 delivery attempts on hosts without working IPv6, leading to long timeouts in deferred; pin to ipv4 until v6 is verified end-to-end.
  • The mailutils package installs a mail binary that calls Postfix’s sendmail wrapper — without it, your smoke test will fail confusingly with “command not found.”
  • A growing deferred queue with Connection timed out errors almost always means the cloud provider is blocking outbound port 25 — open a ticket; it is not a Postfix problem.
  • Postfix runs as the postfix user but the queue directory is chrooted; permission errors on /var/spool/postfix/ after a manual chmod cascade into refused mail.

Stack Harbor stands up Postfix as a baseline mail edge for clients running their own transactional SMTP — install scripted, identity declared, queue under observability, and the box ready for TLS and DKIM in the next pass. If you have a mail server that grew organically and you want it brought under one operating playbook, this is the work covered by our Managed Operations practice.