Primary and Secondary mail exchangers

Contents

  1. Introduction
  2. Basic principles
  3. Configuration
    1. Overview
    2. Details
  4. Most common mistakes
    1. MX preferences
    2. Canonical names
    3. Host name

1. Introduction

In some occasions, it might be useful to define a secondary (or backup) mail system for your domain. The secondary system will handle the mail for your domain if the primary is unreachable for some reasons (network down, computer crash, primary system overloaded, etc).

2. Basic principles

When a remote sendmail system tries to send a mail at your domain (say yourdomain.com), it first queries the DNS for the mail exchangers for your domain. There, it will find (normally) one or several hostnames responsible for the handling of your incoming mail. Finally, it will try to connect to one of these hosts on the SMTP port to deliver the mail.

Mail exchangers are specified within the DNS system using MX records (MX - Mail eXchanger). Each MX record specifies a hostname and a priority. Several MX records can be defined per domain, each with different priorities. Suppose the following DNS configuration:

domain.com. MX 10 primary.yourdomain.com.
domain.com. MX 100 secondary.yourdomain.com.

MX records with the lowest priority value get precedence on the others (the primary mail system has always the lowest priority value). According to this configuration, mail systems will first try to deliver mail for your domain to the primary.yourdomain.com host. If this one is unreachable, they will try to contact the seconday, secondary.yourdomain.com.

Suppose your primary mail system is down, the secondary will then receive all your incoming mails. Since it is not the final destination, it will try periodically to contact the primary to forward the mails it has collected during the down-time. It does so by quering the DNS system to find out which host is the primary mail system for your domain... and the story continues.

3. Configuration

3.1. Overview

Primary: Tell SendMail on the primary system to accept mail for your domain by adding yourdomain.com into the /etc/sendmail.cw configuration file.
Secondary: Tell SendMail on the secondary system to accept relaying mail for your domain by adding yourdomain.com into the /etc/mail/relay.allow configuration file.
Dns: Setup the MX records with the correct hostnames and priorities. Remember that the primary mail exchanger must have the lowest priority value to get precedence on the others.
Warning: these hostnames must always be defined using A-record (A) (address records).

3.2. Details

Primary exchanger

The /etc/sendmail.cw file tells SendMail the domain names it should handle. Mails coming for these domains are considered as being local and are treated in the right way. If your domain name doesn't appear in this file, SendMail will issue an error each time another host tries to send it some mail. Typical error message is ...we do not relay.

Sample configuration file:

# /etc/sendmail.cw

yourdomain.com

Secondary exchanger

You have to tell the hosts you want to be secondary mail exchanger to accept mail for your domain even though they are not their final destination. Just add your domain name in the /etc/mail/relay.allow file.

Do not add anything else in the /etc/sendmail.cw file on the secondary host. If you put your domain name in this file, SendMail will not behave the way you want: it will consider incoming mail for your domain as being local and finally act as a primary exchanger instead of a secondary.

Sample configuration file:

# /etc/mail/relay.allow

yourdomain.com

DNS

As stated above, several secondary exchangers may be defined for your domain, each with different priorities. This information is provided by the DNS service. Remember that the primary mail exchanger must have the lowest preference value to get precedence on the others. I will not go more deeply into the DNS configuration details on this page, please refer to the appropriate documentation for this.

Sample configuration file (file name and IP address are informative):

# /var/named/db.yourdomain

;
; Mail Exchangers
;
    IN MX 10   primary.yourdomain.com.
    IN MX 20   secondary.yourdomain.com.

;
; Canonical names
;
primary.yourdomain.com.   IN A 192.168.0.1
secondary.yourdomain.com. IN A 192.168.25.78

4. Most common mistakes

4.1. MX preferences

Check if your primary mail exchanger has the lowest preference value ! Remember, lowest value gets higher preference...

4.2. DNS config

It is very important that MX records are defined using A-record and not to aliases. Otherwise, you may receive the following error when the backup system will try to deliver mail to the primary: config error: mail loops back to me (MX problem?)

Why ? Suppose the following configuration: you setup two mail exchangers for your domain: primary and secondary. SendMail configuration files are setup on both machines as described above. But instead on using canonical names for the secondary, you defined it in your DNS configuration as an alias for another computer, lets say relay.yourdomain.com. A sample WRONG DNS configuration is shown below:

# /var/named/db.yourdomain

;
; Mail Exchangers
;
    IN MX 10   primary.yourdomain.com.
    IN MX 20   secondary.yourdomain.com. [This is WRONG !!!]

;
; Canonical names
;
primary.yourdomain.com.   IN A 192.168.0.1
secondary.yourdomain.com. IN CNAME relay.yourdomain.com.

;
; Aliases
;
relay.yourdomain.com.     IN A 192.168.25.78

After having queued mails for your domain, your secondary will periodically try to flush its queue. As described above, it first query the DNS system looking for a host with a higher priority than it (lowest preference value). In the above example, it will first contact primary.yourdomain.com. If it is still unreachable, it will try the second one, which is secondary.yourdomain.com (alias). Since your secondary system knows itself as being relay.yourdomain.com (canonical name), it will think that secondary.yourdomain.com is actually another host... Unfortunately, this alias points to the same machine and the mail will loop back to itself. Conclusion, you get the infamous: config error: mail loops back to me (MX problem?).

4.3. Host names

Be sure that your machine knows itself with the same name as the one you put in the DNS configuration, especially for a secondary system. Otherwise, you'll end-up to the same problems as above when using aliases instead of canonical names.


Last update: 04 Jan 1999 bertrand.renuart@itma.lu)