Setup Postfix for multiple domains

mail_server_multi-domainIf you have postfix configured as your mail server you have most likely configured it for one particular domain. If you have more then one domain, you can configure postfix to handle those as well to receive emails for them.

In the postfix config you have specified the primary domain already with the “mydomain” setting in the /etc/postfix/ configuration file. For more details about this setup check my post about Setup Postfix with SMTP-AUTH and TLS on CentOS.

Configure the domain map

You can configure postfix for more than one domain via the use of a hash file. This file contains the list of domains postfix will accept for local delivery.

To configure postfix to read the destination domains from a hash file, open the /etc/postfix/ configuration file and add the following setting. The configuration is similar to the “virtual_alias_maps” configuration:

# Define the domain list as hash file or as list in the config file.
virtual_alias_domains = hash:/etc/postfix/virtual_domains

To create the hash file, create the file configured in the /etc/postfix/ and open it with your favourite editor. Add all the domains postfix should accept in this file (one per line):

The postmap(1) program expects the file to have 2 columns. As we do not have a second column in our domain list, you would see messages like this when you create the hash file:

postmap: warning: virtual_domains, line 1: expected format: key whitespace value
postmap: warning: virtual_domains, line 2: expected format: key whitespace value
postmap: warning: virtual_domains, line 3: expected format: key whitespace value
postmap: warning: virtual_domains, line 4: expected format: key whitespace value

Those warning messages do not mean that the hash file was not created or will not work. This is just a warning and it will work as intended, but if you do not want to see those warnings, you can just add a comment to each line as a second parameter. The second parameter can be anything as it is not used in postfix, but it will silence the postmap warnings. I prefer to use the second parameter as a comment:   #domain   #domain    #domain    #domain

After you have added the list of domains to the file, you need to create the hash file out of it. To do this run the following command. It will create a file with the same name but with a .db file extension. This file is the hash file used by postfix.

$ postmap /etc/postfix/virtual_domains

To load the new configuration and the hash file, postfix needs to be reloaded. Once postfix has been reloaded, the changes are active:

$ /etc/init.d/postfix reload
infoWhenever you change the file containing the list of domains, you need to execute the previous two commands in order to activate the changes. postmap will generate the hash file which will be loaded by postfix with the reload command.

Test the changes

To the the configuration changes, check the log file while you send an email to one of the newly configured domains:

tail -f /var/log/maillog

Assuming that the DNS configuration for those domains is already pointing to the server, you should see the email coming in and being accepted by postfix. If the domain is not properly configured you will see a line like this rejecting the email:

postfix/smtpd[123]: NOQUEUE: reject: RCPT from server.domain.tld[]: 554 5.7.1 <>: Relay access denied; from=<user2@domain.tld> to=<> proto=ESMTP helo=<server.domain.tld>

Read more of my posts on my blog at

This entry was posted in Mailserver and tagged , . Bookmark the permalink.