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 -dshows defaults,postconf -nshows 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
-
Set the hostname and
/etc/hostscleanly before installing. Postfix reads the FQDN at install time and bakes it intomain.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 -
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 -
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 -
Set the identities in
main.cfexplicitly. 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 = nomynetworksis the relay-allow list — leave it as loopback only until you know which subnets need to relay through this host. -
Reload and tail the log. Postfix re-reads config on
reload; a syntax error inmain.cfis caught bypostfix checkbefore you reload:sudo postfix check sudo systemctl reload postfix sudo journalctl -u postfix -f -
Send a test message through
sendmailand 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 -pIf the queue empties within a few seconds, delivery worked. If the message sits in
deferred, runpostqueue -pand inspect the reason field — usually DNS or a missing PTR. -
Lock down before opening port 25 to the world. The default
smtpd_relay_restrictionsblock allows mail frommynetworksand rejects unauthenticated relay attempts, but make it explicit:smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destinationReload, then run a third-party open-relay check (e.g.
mxtoolbox.comorswaks --to test@external --server $(hostname -f)) before declaring the install complete.
Common pitfalls
- Installing Postfix with a hostname that is
localhostorubuntubakes that intomyhostnameand every Received header — set the hostname beforeapt install. - The default
inet_protocols = allcauses IPv6 delivery attempts on hosts without working IPv6, leading to long timeouts indeferred; pin toipv4until v6 is verified end-to-end. - The
mailutilspackage installs amailbinary that calls Postfix’ssendmailwrapper — without it, your smoke test will fail confusingly with “command not found.” - A growing
deferredqueue withConnection timed outerrors almost always means the cloud provider is blocking outbound port 25 — open a ticket; it is not a Postfix problem. - Postfix runs as the
postfixuser 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.