Where to start First steps track · 15 stages
Linux Labs

Postfix — a mail server

4 min Ubuntu 24.04 · Postfix

Sending and receiving mail — config, the queue, logs, typical delivery debugging.

Cheat sheet

7 commands Printable version
  • postconf -n the effective config
  • mailq what's in the queue
  • postqueue -f force the queue to flush
  • postcat -q <QUEUEID> look at a message in the queue
  • journalctl -u postfix or /var/log/mail.log
  • sudo postmap /etc/postfix/sasl_passwd && sudo systemctl reload postfix
  • echo "test" | mail -s "subject" you@example.com a test message

Overview

Postfix is an MTA (mail transfer agent): it sends, receives and routes email over SMTP. Often installed just for a server's outgoing notifications. The postfix service runs under systemd; the config is /etc/postfix/main.cf.

bash
systemctl status postfix
postconf -n                        # the effective config (only what differs from the default)
ss -tlnp | grep -E ':25|:587'      # is SMTP listening (see ss)

Configuration

bash
postconf -n                        # current settings (the changed ones)
postconf inet_interfaces mynetworks relayhost   # specific parameters
sudo postconf -e 'myhostname=mail.example.com'  # change a parameter
sudo postconf -e 'relayhost=[smtp.provider.com]:587'   # send through a smarthost
sudo systemctl reload postfix

# the mail queue
mailq                              # = postqueue -p — what's stuck and why
postqueue -f                       # force a retry of the whole queue
postqueue -i <QUEUEID>             # one specific item
postcat -q <QUEUEID>               # view a message's contents in the queue
sudo postsuper -d ALL              # delete EVERYTHING from the queue (careful!)
sudo postsuper -d ALL deferred     # only the deferred ones
echo "test" | mail -s "subject" you@example.com   # a test message (the mailutils package)

Recipes

1

Relaying through a smarthost

The typical "the server sends notifications through an external SMTP" setup (main.cf):

relayhost = [smtp.provider.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
bash
echo '[smtp.provider.com]:587 user:pass' | sudo tee /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd      # rebuild the .db (after ANY edit to a map!)
sudo systemctl reload postfix

Troubleshooting

5

Mail isn't going out / it's stuck in the queue

mailq shows the reason next to each message. Logs: journalctl -u postfix or /var/log/mail.log. Common causes: the recipient rejected it, there's no reverse DNS (PTR), or port 25 is blocked by the provider (in which case, send through relayhost:587).

Relay access denied

Postfix doesn't forward mail for strangers (anti-relay). For your own networks, use mynetworks; for authenticated senders, SASL. Don't run an open relay (it leads to spam and blocklisting).

Mail is landing in spam

You need SPF/DKIM/DMARC records in DNS and a correct PTR (rDNS). Without them, large providers filter it out.

You changed a map, but it didn't take effect

After editing sasl_passwd/transport/virtual, always run postmap file (rebuild the .db) and reload.

Connection refused on port 25

It's only listening on localhost (inet_interfaces = loopback-only), or the port is blocked (firewall). Check ss -tlnp | grep :25 and postconf inet_interfaces.

Danger zone

Danger. postsuper -d ALL permanently deletes every message in the queue, including ones that could still be delivered — check mailq first, and delete selectively by ID or scope the command (ALL deferred). An open relay (forwarding other people's mail without restricting mynetworks/SASL) quickly gets the server blocklisted — rebuilding an IP's reputation takes weeks.

See also

  • dns-resolv — How a name becomes an IP — resolv.conf, systemd-resolved, /etc/hosts, dig.
  • journalctl — Reading the systemd journal (journald) — filters by service, time, priority.
  • ss — Who's listening on what ports and which connections are open — the modern netstat.
Loading…