Friday, February 19, 2010

Setting Up Postfix As A Backup MX

SkyHi @ Friday, February 19, 2010

In this tutorial I will show how you can set up a Postfix mailserver as a backup mail exchanger for a domain so that it accepts mails for this domain in case the primary mail exchanger is down or unreachable, and passes the mails on to the primary MX once that one is up again.

I do not issue any guarantee that this will work for you!


1 Preliminary Note

I want to set up a backup MX for the domain In this example the primary MX for is called (IP address, so I call the backup MX (IP address

I have created MX records for that look like this:               86400   IN      MX      10               86400   IN      MX      20

It's important that the primary MX has a lower number (10) and therefore a higher priority than the backup MX (20).

I'm assuming that the Postfix on is already installed and working.


2 Configuring Postfix On

To make a backup MX for the domain, all we have to do is change/add three lines to /etc/postfix/

vi /etc/postfix/

First make sure that smtpd_recipient_restrictions contains permit_mynetworks and reject_unauth_destination, so something like this would be ok:

smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination

Then we must add to the relay_domains paramater; if there's no relay_domains paramater yet in /etc/postfix/, the following will do:

[...]<br />relay_domains = $mydestination,<br />[...]

And finally we add an empty relay_recipient_maps parameter to /etc/postfix/

[...]<br />relay_recipient_maps =<br />[...]

(That way we don't have to specify a list of valid email addresses to back up, which might be a daunting task if you have to manage hundreds of email accounts.)

There's one important thing I have to add: You must not list in the following parameters in /etc/postfix/

  • mydestination
  • virtual_alias_domains
  • virtual_mailbox_domains

That's it already. All we have to do now is restart Postfix:

/etc/init.d/postfix restart


3 Testing

To test the new backup MX, we take down the MTA (Postfix, Sendmail, Exim, etc.) on and send an email from some remote server to an account (e.g.

If you have access to the mail log on the remote (sending) server, you should now find something like this in it:

Jun 6 18:29:16 mail postfix/smtp[17746]: AF814144146: to=<>,[], delay=1, status=sent (250 2.0.0 Ok: queued as DCA5A1BF40F)

As you see, the mail has been sent to instead of because is unreachable. Now, let's take a look at the mail log of

Jun 6 18:29:16 mx2 postfix/qmgr[3049]: DCA5A1BF40F: from=<falko@blabla.tld>, size=892, nrcpt=1 (queue active)
Jun 6 18:29:16 mx2 postfix/smtpd[3051]: disconnect from mail.blabla.tld[]
Jun 6 18:29:16 mx2 postfix/smtp[3057]: connect to[]: Connection refused (port 25)
Jun 6 18:29:16 mx2 postfix/smtp[3057]: DCA5A1BF40F: to=<>, relay=none, delay=0.07, delays=0.03/0.02/0.01/0, dsn=4.4.1, status=deferred (connect to[]: Connection refused) has accepted the mail and tried to connect to to deliver it to the primary MX. Because the primary MX is down, cannot deliver the mail and keeps it in the mailqueue until is available again.

Now we start the MTA on again. The backup MX will not immediately deliver the queued mail, but after some minutes you should see something like this in the mail log of

Jun 6 18:56:44 mx2 postfix/qmgr[3080]: DCA5A1BF40F: from=<falko@blabla.tld>, size=892, nrcpt=1 (queue active)
Jun 6 18:56:45 mx2 postfix/smtp[3083]: DCA5A1BF40F: to=<>,[]:25, delay=1648, delays=1648/0.09/0.4/0.12, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 167995B0109)

The mail has been delivered to the primary MX where you can see this in the mail log:

Jun 6 18:56:45 mx1 postfix/local[4963]: 167995B0109: to=<>, orig_to=<>, relay=local, delay=0.54, delays=0.08/0.02/0/0.43, dsn=2.0.0, status=sent (delivered to command: /usr/bin/procmail -f-)

So no mails were lost while was down, and users can continue to retrieve their mails from