This guide walks you through configuring Postfix to handle email for multiple domains on a single server. You’ll set up virtual domains and mailboxes so that addresses like user@example.com and user@example.net are delivered to the correct mailboxes, without needing separate server instances.
What You’ll Need
- A Linux server (Debian/Ubuntu, CentOS/RHEL, or similar) with Postfix installed.
- Root or sudo access to edit Postfix configuration files.
- DNS control for the domains you want to host, so you can set MX records.
- A mail client (like Thunderbird or Outlook) for testing, or the ability to send test emails via command line.
Step-by-Step
- Back up your current Postfix configuration.
Before making changes, copy the main configuration file:sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bak. This allows you to revert if something goes wrong. - Create a virtual domains file.
Create a text file, e.g.,/etc/postfix/virtual_domains, and list each domain you want to host on its own line. For example:example.comexample.net
Save the file. - Create a virtual mailboxes file.
Create another text file, e.g.,/etc/postfix/virtual_mailboxes, to map email addresses to local mailboxes. The format isaddress destination. For example:info@example.com example.com/info/support@example.net example.net/support/
You can also use a catch-all like@example.com example.com/catchall/to deliver all unmatched addresses to a single mailbox. Save the file. - Update the main Postfix configuration.
Edit/etc/postfix/main.cfand add or modify these lines:virtual_mailbox_domains = /etc/postfix/virtual_domainsvirtual_mailbox_maps = hash:/etc/postfix/virtual_mailboxes
Also ensure you have a line forvirtual_mailbox_baseto set the base directory for mail storage, e.g.,virtual_mailbox_base = /var/mail/vhosts. And set the local delivery agent, e.g.,virtual_transport = virtual. - Create the mail storage directory.
Create the base directory you specified, e.g.,sudo mkdir -p /var/mail/vhosts. Then, for each domain and mailbox, create the corresponding subdirectories. For example, forinfo@example.com:sudo mkdir -p /var/mail/vhosts/example.com/info. Set appropriate ownership, typically to the user Postfix runs as (oftenvmail). - Build the lookup tables.
Postfix uses hashed database files for performance. Run:sudo postmap /etc/postfix/virtual_mailboxes
This generatesvirtual_mailboxes.db. - Set up DNS MX records.
For each domain, add an MX record pointing to your server’s hostname (e.g.,mail.example.com) with a priority like 10. Ensure the A record for that hostname resolves to your server’s IP. - Restart Postfix.
Apply the changes by restarting the service:sudo systemctl restart postfix(orsudo service postfix restarton older systems). - Test delivery.
Send a test email to one of the configured addresses from an external account or via command line. Check the mailbox directory to confirm delivery, e.g.,ls /var/mail/vhosts/example.com/info.
Common Mistakes
- Forgetting to run
postmap. Changes to the virtual_mailboxes file won’t take effect until you rebuild the database. - Incorrect file permissions. Postfix runs as a non-root user (often
vmail). If the mail directories are not owned by that user, delivery will fail. - Missing MX record. Without proper DNS MX records, other mail servers won’t know where to deliver mail.
- Using the same mailbox for multiple domains without unique paths. If you map
info@example.comandinfo@example.netto the same path, mail will mix.
Tips
- Use a catch-all address for each domain to avoid losing mail to typos, but be aware it can attract spam.
- Set up SPF and DKIM records for each domain to improve deliverability and reduce spam classification.
- Consider using a database (like MySQL or PostgreSQL) for virtual mailboxes if you have many users; Postfix supports this via proxy maps.
- Regularly monitor logs at
/var/log/mail.logor/var/log/maillogto catch issues early.
Troubleshooting
If mail isn’t being delivered, check the following:
- Check Postfix logs. Look for error messages like “bounce” or “mailbox unavailable” in the mail log.
- Verify the virtual domain is recognized. Run
postconf -nto see the effective configuration. Ensurevirtual_mailbox_domainsis set correctly. - Test with
postmap -q. Runpostmap -q info@example.com /etc/postfix/virtual_mailboxesto see if the lookup works. - Check DNS resolution. Use
dig MX example.comto confirm the MX record is present and points to your server. - Check firewall. Ensure port 25 (SMTP) is open and accessible from the internet.
Frequently Asked Questions
Can I host both virtual and local domains?
Yes, you can mix. Local domains (like localhost) are handled by the mydestination setting, while virtual domains are handled by virtual_mailbox_domains. Just add the local domains to mydestination and keep virtual ones separate.
How do I add a new mailbox for an existing domain?
Add a line to virtual_mailboxes with the new address and path, create the directory, run postmap, and restart Postfix.
What is the difference between virtual_alias_maps and virtual_mailbox_maps?
virtual_alias_maps redirects one address to another (like an alias), while virtual_mailbox_maps defines actual mailboxes. Aliases can point to local or virtual addresses.
Can I use a catch-all for multiple domains?
Yes, you can add a catch-all line for each domain, e.g., @example.com example.com/catchall/. This will deliver all unmatched addresses to that mailbox.
How do I set up multiple domains with different mail storage?
Simply use different paths in the virtual_mailboxes file. For example, user@example.com example.com/user/ and user@example.net example.net/user/.
Do I need to configure Dovecot or another IMAP/POP server?
Yes, to let users access their mail via IMAP/POP, you need a separate service like Dovecot. Postfix only handles SMTP delivery. Dovecot can read the same Maildir structure.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Microsoft Surface Duo: The Foldable Phone Story
- Samsung SM-T813 Hard Reset: Step-by-Step Guide
- Mafia Clash Codes: Real Meanings & Usage
- Remove Programs from Mac: Full Uninstall Guide
- Project Sentinel Morning Digest: Data Stack Failure Alerts
- Best KVM VPS Hosting: Top Picks & Guide