If your server cannot send mail directly to the internet, or if your messages keep landing in spam, the usual answer is a smarthost: a relay that accepts your outgoing mail and delivers it with its own reputation, PTR record and SPF alignment. This article configures Postfix as a relay client on Debian 13 and on AlmaLinux 9, with SASL authentication, mandatory TLS, and the sender rewriting that most providers require.
What a smarthost relay does
By default Postfix delivers mail itself. It looks up the recipient’s MX record and opens an SMTP connection to that server on port 25. That works only when the server is reachable from the internet and looks trustworthy to the receiving side. In practice many servers cannot do that, and a relay is the way out:
- the provider blocks outbound port 25 (most residential ISPs, and many cloud providers);
- the server’s IP has no reverse DNS record, or the PTR points at a generic name;
- the IP sits on a block list because a previous tenant sent spam;
- you want one place to see every message the server sends, with one set of credentials to rotate.
With a smarthost, Postfix hands every outgoing message to the relay over an authenticated, encrypted connection. The relay then does the delivery. The setup has four parts: the relay host, the credentials, TLS, and the sender address. The first three are the same on every distribution; the sender address is the part that catches people out, so it gets its own step below.
What you need before you start
- root or sudo on the server, and a working shell.
- An account on the relay: hostname, port, username and password. For Gmail, that means an app password (enable 2FA first) and the address you send from. For providers such as SendGrid, Mailgun, Brevo or your hosting company’s own SMTP, it means the credentials from their dashboard, plus the sender domain you verified there.
- The FQDN of the server set correctly:
hostname -fmust return something likeserver.example.com, notlocalhost. Postfix uses it in theHELOline and inMessage-ID, and some relays reject a bare name. - Outbound access to the relay port.
587(STARTTLS) and465(SMTPS) are the usual ones; check withnc -vz smtp.example.com 587ortelnet smtp.example.com 587. - A valid
MX/SPFsetup for the domain you will send as, if the relay checks it. Gmail rewrites the sender to your account, most transactional providers do not.
Step 1: install Postfix
On Debian 13 the package asks its questions during the install, so run it with a terminal you can answer:
|
1 2 |
apt update apt install postfix |
Pick Satellite system if this machine only sends mail, or Internet with smarthost if it also receives mail directly. Both ask for the system mail name and, later, for the relay host. The full list of choices the package offers is: No configuration, Internet Site, Internet with smarthost, Satellite system and Local only. You can change your mind later:
|
1 |
dpkg-reconfigure postfix |
That dialog sets the relay host, but it knows nothing about authentication, so you still edit main.cf by hand in the next steps.
On AlmaLinux 9 the distribution usually ships Postfix already. If yours does not, or if Sendmail is the active MTA:
|
1 2 3 |
dnf install postfix alternatives --config mta systemctl enable --now postfix |
There are no install-time questions on AlmaLinux: every setting lives in /etc/postfix/main.cf, which you edit yourself.
Step 2: point Postfix at the relay
The parameter is relayhost. Enclose the hostname in square brackets to stop Postfix from looking up its MX record, and give the port:
|
1 |
relayhost = [smtp.example.com]:587 |
Use port 587 for STARTTLS and 465 for SMTPS – the TLS mode has to match the port, see step 4. Postfix also accepts the service name instead of the number, so relayhost = [smtp.example.com]:submission is the same thing.
Two related settings are worth fixing at the same time, because they decide which address the relay sees:
|
1 2 3 |
myhostname = server.example.com mydomain = example.com myorigin = $mydomain |
With the default myorigin = $myhostname, mail sent from root leaves as [email protected]. Setting myorigin = $mydomain makes it [email protected], which is what most relays expect to see. The change is cosmetic for the delivery itself, but it avoids a whole class of “sender rejected” errors.
Step 3: add the relay credentials
Postfix reads the username and password from a lookup table, not from main.cf. Create /etc/postfix/sasl_passwd with one line per relay, in the form [host]:port username:password:
|
1 |
[smtp.example.com]:587 your-relay-user@example.com:your-secret-password |
Lock the file down, build the indexed version, and remove the source file’s world-readable bits:
|
1 2 3 |
chmod 600 /etc/postfix/sasl_passwd postmap /etc/postfix/sasl_passwd ls -l /etc/postfix/sasl_passwd* |
postmap creates /etc/postfix/sasl_passwd.db. The hash: prefix below matches that file, and Debian and AlmaLinux 9 both default to the hash format.
Now tell Postfix to authenticate. Add these lines to main.cf:
|
1 2 3 |
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous |
The last line matters. The built-in default is noplaintext, noanonymous, and every mainstream relay offers only PLAIN and LOGIN – the two mechanisms that send the password as text. With the default you get a login failure that looks like a wrong password:
|
1 |
SASL authentication failed; cannot authenticate to server smtp.example.com[192.0.2.10]: no mechanism available |
Setting smtp_sasl_security_options = noanonymous allows those mechanisms. Use it only together with mandatory TLS, as configured in the next step: on a plaintext connection the password would travel in the clear.
The PLAIN and LOGIN modules ship in a separate package:
- Debian 13:
apt install libsasl2-modules(without it you get No worthy mechs found in the log). - AlmaLinux 9:
dnf install cyrus-sasl-plain.
Step 4: require TLS
Out of the box Postfix is happy to talk in the clear, and even to fall back to plaintext when the relay’s STARTTLS offer is stripped by something in the middle. Make encryption mandatory:
|
1 |
smtp_tls_security_level = encrypt |
That guarantees an encrypted session, but it does not check who is on the other side. To also verify the relay’s certificate against the system CA store:
|
1 2 |
smtp_tls_security_level = verify smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt |
The CA bundle path is the one difference that trips people up between the two systems: Debian keeps it at /etc/ssl/certs/ca-certificates.crt, AlmaLinux at /etc/pki/tls/certs/ca-bundle.crt. If you use encrypt instead of verify, you can leave the CA file out entirely.
For port 465 the TLS mode is different: SMTPS wraps the whole session in TLS from the first byte, instead of upgrading with a STARTTLS command. Add the wrapper mode, which needs smtp_tls_security_level = encrypt (or stronger) to have any effect:
|
1 |
smtp_tls_wrappermode = yes |
Port 587 and port 465 are not interchangeable – a relay on 465 with STARTTLS settings will hang until it times out, and the other way round produces an immediate protocol error. When in doubt, use 587 with smtp_tls_security_level = encrypt and no wrappermode, which is what most providers document today.
If the connection fails with a certificate error, turn on the client TLS log and read the reason:
|
1 |
smtp_tls_loglevel = 1 |
Step 5: rewrite the sender address
This step decides whether the relay accepts your mail. Most relays check the envelope sender against the account: Gmail rewrites it for you, but a transactional provider rejects anything that is not on the verified domain with 550 5.7.1 sender address rejected. Postfix can rewrite the sender in two places, and the choice matters:
sender_canonical_mapsrewrites the address when the message is received, so it applies to local mail too. Use it for per-user mappings.smtp_generic_mapsrewrites the address when the message is delivered over SMTP. Use it when every message should leave with the same domain – which is the normal case for a relay client.
For a server that sends as one domain, create /etc/postfix/generic:
|
1 2 |
@server.example.com your-relay-user@example.com root@server.example.com your-relay-user@example.com |
The first line matches every address in that domain, including root@ and any address generated by cron. Then index it and point Postfix at it:
|
1 2 |
postmap /etc/postfix/generic postconf -e 'smtp_generic_maps = hash:/etc/postfix/generic' |
If instead you want each local user to send as a different mailbox, use the canonical form. Create /etc/postfix/sender_canonical:
|
1 2 |
root admin@example.com bogdan bogdan@example.com |
and index it the same way:
|
1 2 |
postmap /etc/postfix/sender_canonical postconf -e 'sender_canonical_maps = hash:/etc/postfix/sender_canonical' |
postconf -e is a convenient way to change one parameter without opening an editor, and it keeps the rest of main.cf untouched. It works the same on both distributions.
Step 6: send local mail through the relay too
The relay only sees mail that leaves the machine, and Postfix delivers mail for root locally by default. That means cron output and system warnings never reach you. Point those addresses at a real mailbox and rebuild the alias database:
|
1 2 |
newaliases |
On a satellite system you can also disable local delivery completely, so nothing can pile up in a mailbox nobody reads:
|
1 2 |
mydestination = local_transport = error: local delivery disabled |
That is the null client setup, and it is what most monitoring servers should run. On Debian, dpkg-reconfigure postfix sets the equivalent values for you when you choose Satellite system.
Step 7: reload and test
Check the configuration for syntax errors, apply it, and send a real message:
|
1 2 3 |
postfix check systemctl restart postfix echo 'relay test' | sendmail -v your-address@example.com |
The -v flag prints the SMTP conversation, including the line that tells you which relay answered and whether authentication succeeded. Then watch the log:
|
1 2 |
tail -f /var/log/mail.log # Debian tail -f /var/log/maillog # AlmaLinux |
A successful relayed message looks like this – the important part is relay=smtp.example.com, with no dsn=5.x.x and no status=deferred:
|
1 2 |
postfix/smtp[1234]: 4Bh...: to=<your-address@example.com>, relay=smtp.example.com[192.0.2.10]:587, delay=1.2, delays=0.1/0.02/0.4/0.7, dsn=2.0.0, status=sent (250 2.0.0 OK) |
If the queue does not move, look at what is waiting and force a retry:
|
1 2 3 |
postqueue -p # or: mailq postqueue -f # retry everything now postsuper -d ALL # delete the whole queue - only for test messages |
Troubleshooting the most common failures
| Symptom in the log | Cause | Fix |
|---|---|---|
no mechanism available or No worthy mechs found |
The default noplaintext blocks PLAIN and LOGIN, or the SASL modules are missing |
Set smtp_sasl_security_options = noanonymous and install libsasl2-modules (Debian) or cyrus-sasl-plain (AlmaLinux) |
535 5.7.8 Username and Password not accepted |
Wrong password, or a normal account password where the provider wants an app password | Re-issue the credential at the provider, then postmap /etc/postfix/sasl_passwd again |
550 5.7.1 sender address rejected |
The relay does not recognise the envelope sender domain | Add the sender rewriting from step 5 |
Connection timed out to the relay, or lost connection after CONNECT |
Port blocked outbound, wrong port, or STARTTLS settings on an SMTPS port | Test with nc -vz host 587, and match the port to the TLS mode |
certificate verification failed |
Wrong CA bundle path, or the server clock is off | Use the bundle for your distribution, and check timedatectl |
status=deferred for every message |
The relay is unreachable or rejecting the connection | postqueue -p for the reason, then the relay’s own status page |
On AlmaLinux 9, a denial that never reaches the network can also come from SELinux. Files under /etc/postfix inherit the right context automatically, but a password file kept somewhere else does not:
|
1 2 |
ausearch -m avc -ts recent restorecon -v /etc/postfix/sasl_passwd |
Debian 13 and AlmaLinux 9: the differences at a glance
The configuration itself is the same on both systems – the same parameters, the same files, the same commands. What differs is the packaging around it:
| Debian 13 (trixie) | AlmaLinux 9 | |
|---|---|---|
| Postfix version in the distribution | 3.10.13 | 3.5.25 |
| Install | apt install postfix, with debconf questions |
dnf install postfix, no questions |
| Selecting the MTA | not needed | alternatives --config mta |
| Default listening interface | inet_interfaces = all (Internet Site) or loopback-only (Satellite system) |
inet_interfaces = localhost |
| Lookup table format | hash: – creates .db |
hash: on EL9 – creates .db (lmdb: from EL10) |
| PLAIN and LOGIN modules | libsasl2-modules |
cyrus-sasl-plain |
CA bundle for verify |
/etc/ssl/certs/ca-certificates.crt |
/etc/pki/tls/certs/ca-bundle.crt |
| Mail log | /var/log/mail.log |
/var/log/maillog |
| Mandatory access control | AppArmor, nothing to do for this setup | SELinux enforcing; /etc/postfix carries the right label |
| Firewall | none by default | firewalld; no rule needed for outbound mail |
Steps 2 to 6 use the same parameters on both systems, so you can copy one block straight between them.
Keep the credentials and the relay safe
/etc/postfix/sasl_passwdholds a working password in plain text. Keep it600and owned by root, and delete any copy you made elsewhere.- Do not add your own networks to
mynetworksto “make it work”. That parameter decides who may relay through you, and a wrong entry turns the server into an open relay. - Leave
smtpd_relay_restrictionsat its default (permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination). It already refuses to forward mail from strangers. - Use a credential that is limited to sending, not an account that can read mail, and rotate it when someone leaves.
- If the relay has a daily sending limit, add
smtp_destination_rate_delayor check the queue rather than discovering the limit from bounce messages.
References
- Postfix on the Debian wiki – installing and configuring Postfix on Debian, TLS on the submission and SMTPS ports, and a Debian-specific section.
- postconf(5) – the reference for every parameter used here, including the TLS security levels and the SASL options.
- Postfix SASL Howto – the client-side section is where the
sasl_passwdformat and the SASL security options come from. - Postfix TLS Support – the security levels, the CA configuration and the wrapper mode in full.
- Postfix Address Rewriting – canonical against generic mappings, in detail.
- Postfix Standard Configuration Examples – the null client and “route all mail through your provider” recipes.
- Deploying and configuring a Postfix SMTP server – Red Hat’s RHEL 9 guide, which covers the AlmaLinux side.
- On a Debian system,
/usr/share/doc/postfix/README.Debian.gzdocuments the smarthost setup end to end, including the sender rewriting. It ships with the package, so it is always at hand.
