Understanding Postfix Virtual Domain Configuration
Virtual domains let you host email for multiple domains on a single Postfix server. This setup works well for hosting providers managing customer email across different domains. It's also perfect for businesses running multiple brands from one VPS.
Virtual domains need mapping tables that tell Postfix which domains to accept mail for and where to deliver messages. You'll work with three key components: virtual domain maps, virtual alias maps, and virtual mailbox maps.
Our Hostperl VPS plans provide the flexibility needed for complex email configurations like this. You get root access and sufficient resources for multi-domain email hosting.
Prerequisites and System Preparation
Before you configure Postfix virtual domains, ensure your Ubuntu VPS meets these requirements:
- Ubuntu 22.04 or 24.04 with root access
- Postfix already installed and running
- DNS records configured for all domains
- At least 2GB RAM for handling multiple domains
Check your current Postfix configuration:
sudo postconf -n | grep virtual
sudo systemctl status postfix
Verify DNS resolution for your domains:
dig MX example1.com
dig MX example2.com
Creating Virtual Domain Configuration Files
Postfix virtual domains rely on three main configuration files. Create them in /etc/postfix/:
sudo nano /etc/postfix/virtual_domains
Add your domains, one per line:
example1.com OK
example2.com OK
client3.net OK
Create the virtual aliases file:
sudo nano /etc/postfix/virtual_aliases
Define email aliases and forwards:
info@example1.com john@example1.com
sales@example1.com mary@example1.com
support@example2.com tech@example2.com
catchall@client3.net admin@client3.net
Create the virtual mailbox file for actual email accounts:
sudo nano /etc/postfix/virtual_mailboxes
Map email addresses to mailbox locations:
john@example1.com example1.com/john/
mary@example1.com example1.com/mary/
tech@example2.com example2.com/tech/
admin@client3.net client3.net/admin/
Configuring Postfix Main Settings
Edit the main Postfix configuration file:
sudo nano /etc/postfix/main.cf
Add or modify these virtual domain parameters:
# Virtual domain configuration
virtual_mailbox_domains = hash:/etc/postfix/virtual_domains
virtual_alias_maps = hash:/etc/postfix/virtual_aliases
virtual_mailbox_maps = hash:/etc/postfix/virtual_mailboxes
virtual_mailbox_base = /var/mail/virtual
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
The virtual_mailbox_base directory will store all virtual domain emails. Set ownership correctly:
sudo mkdir -p /var/mail/virtual
sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 vmail -d /var/mail/virtual
sudo chown -R vmail:vmail /var/mail/virtual
Building Postfix Database Files
Convert your configuration files to Postfix database format:
sudo postmap /etc/postfix/virtual_domains
sudo postmap /etc/postfix/virtual_aliases
sudo postmap /etc/postfix/virtual_mailboxes
This creates .db files that Postfix uses for fast lookups.
Verify the database files were created:
ls -la /etc/postfix/virtual_*.db
Test your configuration syntax:
sudo postfix check
If no errors appear, reload Postfix:
sudo systemctl reload postfix
Testing Virtual Domain Email Delivery
Test email delivery to your virtual domains using the mail command:
echo "Test message to virtual domain" | mail -s "Test" john@example1.com
Check the mail queue and logs:
sudo mailq
sudo tail -f /var/log/mail.log
Verify mailbox creation:
ls -la /var/mail/virtual/example1.com/
You should see a john/ directory containing the delivered message.
For comprehensive email server monitoring, check our guide on monitoring Postfix mail queues.
Managing Multiple Domain Aliases
Virtual aliases let you create complex forwarding rules across domains. Here are common patterns:
Catch-all aliases:
@example1.com admin@example1.com
Cross-domain forwards:
billing@example1.com accounting@example2.com
Multiple recipients:
alerts@example1.com admin@example1.com,tech@example2.com
After adding aliases, rebuild the database:
sudo postmap /etc/postfix/virtual_aliases
sudo systemctl reload postfix
Dovecot Integration for IMAP Access
To provide IMAP access for virtual domains, configure Dovecot to work with your virtual mailbox structure:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Set the mail location:
mail_location = maildir:/var/mail/virtual/%d/%n/Maildir
Configure virtual users in auth-passwdfile.conf.ext:
sudo nano /etc/dovecot/conf.d/auth-passwdfile.conf.ext
Add:
passdb {
driver = passwd-file
args = scheme=CRYPT username_format=%u /etc/dovecot/users
}
userdb {
driver = passwd-file
args = username_format=%u /etc/dovecot/users
default_fields = uid=vmail gid=vmail home=/var/mail/virtual/%d/%n
}
Create user accounts in the password file:
sudo nano /etc/dovecot/users
For enhanced email security across virtual domains, review our complete email security setup guide.
Troubleshooting Virtual Domain Issues
Here are common problems and their solutions:
Mail rejected for unknown domains:
Check that domains are listed in virtual_domains and the database is current.
Permission denied errors:
Verify vmail user ownership on /var/mail/virtual:
sudo chown -R vmail:vmail /var/mail/virtual
sudo chmod 755 /var/mail/virtual
Aliases not working:
Ensure virtual_aliases.db is up to date:
sudo postmap /etc/postfix/virtual_aliases
Database lookup failures:
Test database queries manually:
postmap -q example1.com hash:/etc/postfix/virtual_domains
Maintenance and Monitoring
Regular maintenance tasks for virtual domain setups:
Monitor disk usage in virtual mailboxes:
du -sh /var/mail/virtual/*
Check for stuck messages:
sudo postqueue -p
Rotate mail logs:
sudo logrotate /etc/logrotate.d/rsyslog
Back up configuration files weekly:
sudo tar czf /backup/postfix-config-$(date +%Y%m%d).tar.gz /etc/postfix/virtual_*
Frequently Asked Questions
How many virtual domains can Postfix handle on a VPS?
Postfix can handle hundreds of virtual domains on a properly sized VPS. Performance depends more on email volume than domain count. A 4GB VPS typically supports 50+ domains with moderate traffic.
Do I need separate SSL certificates for each virtual domain?
Yes, for secure IMAP/SMTP connections, each domain needs its own SSL certificate or a wildcard certificate covering all domains. Let's Encrypt provides free certificates for multiple domains.
Can I mix virtual and local domains on the same server?
Yes, but avoid overlap. Domains in mydestination are local domains, while those in virtual_mailbox_domains are virtual. A domain cannot be in both lists.
How do I add new virtual domains without service disruption?
Add the domain to your configuration files, run postmap to update databases, then reload Postfix with systemctl reload postfix. No restart required.
What's the difference between virtual aliases and virtual mailboxes?
Virtual aliases forward mail to other addresses without storing it. Virtual mailboxes store mail in actual files on the server. Use aliases for forwarding, mailboxes for local storage.

