Skip to content

How to configure Postfix as a smarthost relay on Debian 13 and AlmaLinux 9

Send a server's mail through a relay that has the reputation and the PTR record: Postfix smarthost configuration with SASL authentication, mandatory TLS and sender rewriting, on Debian 13 and AlmaLinux 9 side by side.

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 -f must return something like server.example.com, not localhost. Postfix uses it in the HELO line and in Message-ID, and some relays reject a bare name.
  • Outbound access to the relay port. 587 (STARTTLS) and 465 (SMTPS) are the usual ones; check with nc -vz smtp.example.com 587 or telnet smtp.example.com 587.
  • A valid MX/SPF setup 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:

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:

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:

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:

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:

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:

The square brackets and the port are part of the key. A line without the port only matches a relayhost that also has no port, which is a common cause of “the password is simply ignored”.

Lock the file down, build the indexed version, and remove the source file’s world-readable bits:

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.

AlmaLinux 10 and newer default to lmdb, which produces a .lmdb file instead. Check yours with postconf -h default_database_type if you are not sure.

Now tell Postfix to authenticate. Add these lines to main.cf:

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:

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:

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:

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:

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:

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_maps rewrites the address when the message is received, so it applies to local mail too. Use it for per-user mappings.
  • smtp_generic_maps rewrites 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:

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:

If instead you want each local user to send as a different mailbox, use the canonical form. Create /etc/postfix/sender_canonical:

and index it the same way:

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:

On a satellite system you can also disable local delivery completely, so nothing can pile up in a mailbox nobody reads:

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:

The -v flag prints the SMTP conversation, including the line that tells you which relay answered and whether authentication succeeded. Then watch the log:

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:

If the queue does not move, look at what is waiting and force a retry:

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:

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_passwd holds a working password in plain text. Keep it 600 and owned by root, and delete any copy you made elsewhere.
  • Do not add your own networks to mynetworks to “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_restrictions at 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_delay or 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_passwd format 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.gz documents the smarthost setup end to end, including the sender rewriting. It ships with the package, so it is always at hand.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.