Virtual Hosting with Postfix, part one

Version 1.0

Author: Joe Topjian <joe [at] adminspotting [dot] net>

The virtual domain support in Postfix is actually quite robust. There are three different ways you host virtual domains with Postfix and they're all described here. We'll be looking at the third one: separate domains and non-unix accounts.

Why this one? Because in the end, this options gives us the most flexibility. It's a little more complicated to set up and understand but well worth it when you're hosting several domains.

Our end goal is to have an email server that supports mail delivery to multiple domains. Each email address will be authentic to only that domain. For example, [email protected] and [email protected] are two different accounts that each receive different mail.

To start out, we're going to turn all domains into virtual hosts. Even if you have Postfix set up with a single domain, we're going to make that domain virtual. You don't need to do this, but I do because I think it's more organized. Having Postfix host one real domain and the rest virtual means that you will always need to configure Postfix twice: once for each type of domain. To do that, we'll change our myhostname line in main.cf to read:

myhostname = localhost

Next we're going to add the following virtual domain information to main.cf (all of which will be explained after):

virtual_mailbox_domains = /etc/postfix/vhosts.txt

virtual_mailbox_base = /var/spool/vmail
virtual_mailbox_maps = hash:/etc/postfix/vmaps.txt
virtual_uid_maps = static:1000
virtual_gid_maps = static:1000
virtual_alias_maps = hash:/etc/postfix/valias.txt

In the first line, we're using a text file called vhosts.txt. You can actually name this anything you want. Inside this text file will be a simple one-column list of all the domains you are hosting. For example:

domain1.com
domain2.com
virtual.org



The next line specifies the base directory where we shall store all of our mail. Again, you can choose anything you want.

The third line points to a textfile I called vmaps.txt. This is a two column text file. The first column specifies a virtual email address. The second column specifies that persons mailbox location. Just like with real domain hosting, if you specify a / at the end of the location, it becomes Maildir format. If not, it is mbox. I have hash specified because I'm also turning vmaps.txt into a hash file by running:

postmap vmaps.txt

This results in a file called vmaps.txt.db. Postfix is able to lookup information in hashes faster than a normal text file.

The contents of vmaps.txt looks like this:

[email protected] domain1.com/joe/
[email protected] domain2.com/joe/
[email protected] virtual.org/john/



Take a look at the second column. The value is appended to our virtual_mailbox_base line. So the absolute path of the virtual mailbox becomes, for example, /var/spool/vmail/domain1.com/joe/. Don't forget to actually make the directories domain1.com and joe. Since this mailbox is in maildir format, we'll need 3 subdirectories under this mailbox: new, cur, tmp. There are several scripts around to do this, but basically this works just fine:

mkdir new cur tmp
chmod 700 new cur tmp



The next two lines define an account we'll set up that will have permission to access the mailboxes. Yes, one account will have the ability to read all the virtual email. Yes, this can be considered a security problem. Please do your best to ensure no one can become this user. We'll call the account "virtual". Add it any way you want to the system (eg, useradd) and make note of it's uid and gid.

The final line specifies a text file where we can place aliases for virtual accounts. The contents looks like this:



Finally, you'll need to give ownership to the mailboxes to the virtual user. Running this will take care of it:

chown -R virtual:virtual /var/spool/vmail

And that's it. Just run a "postfix reload" and you are all set. Of course now we need a way to actually retrieve the email. I'll do that in Part Two.

Share this page:

9 Comment(s)