Handling mail for multiple virtual domains with postfix
Posted by Steve on Wed 14 Sep 2005 at 22:31
Handling email for multiple virtual domains is a common server task. Here we'll show how easy that is to manage using the postfix mailserver using just a couple of small configuration changes.
Note: The Debian mail server packages all conflict with each other meaning that it is impossible to have more than one installed at the same time. If you are already using postfix this won't be a problem.
However if you are running a different mailserver, such as exim, be warned that installing postfix will remove it. (Your configuration files should be kept safely in place, but taking a backup in advance is worth doing anyway).
To install postfix run, as root:
apt-get install postfix
Once installed you'll be presented with a simple debconf setup wizard. Proceed by answering the questions as follows:
- General Type of Configuration?
- Internet Site
- Where should mail for root go?
- Mail name?
- Your mailserver's primary hostname. (Fully qualified)
- Other destinations to accept mail for?
Now we can move on to the real configuration. What we need to do is two things:
- Tell postfix which domains we will accept mail for.
- (i.e. All the virtual domains we wish to handle).
- Setup the mapping between email addresses and local accounts.
(It is not required that users have local accounts, Postfix offers several alternative virtual hosting approaches - but this is what I use. It is also equivalent to the previous guide on virtual mail handling with exim4)
To tell postfix which domains we will use we will create a text file to list them in, in the directory /etc/postfix/virtual.
(Create that directory by running "mkdir /etc/postfix/virtual").
List all the domains you wish to handle mail for, in the file /etc/postfix/virtual/domains. For example:
example.com example.net example.org
Then to setup the mappings create the file /etc/postfix/virtual/addresses:
example.com DOMAIN firstname.lastname@example.org bob email@example.com steve example.net DOMAIN @example.net steve example.org DOMAIN @example.org chris
This file contains the mappings of email addresses to local accounts. Here we've setup four mappings:
- Mail sent to firstname.lastname@example.org goes to the local user bob.
- Mail sent to email@example.com goes to the local user steve.
- Mail sent to firstname.lastname@example.org goes to the local user steve
- Mail sent to email@example.com goes to the local user chris
(Note that mail sent to other addresses @example.com will be bounced with "no such user".)
To make these settings live we need to do two things:
- Update postfix's main.cf to read these settings.
- Create a hash of the /etc/postfix/virtual/addresses file.
In the file /etc/postfix/main.cf add:
mydestination = $myhostname, /etc/postfix/virtual/domains virtual_maps = hash:/etc/postfix/virtual/addresses
That should be all you need to do. Reload postfix by running:
You can test the setup by attempting to send mail to the server, and watching /var/log/mail.info to see what postfix does with the mail.