Configuring Virtual Domains on an Ubuntu Mail Server with Postfix and Dovecot
Virtual domains allow a single mail server to host email addresses for multiple domains (e.g., user1@example.com, user2@test.com). Below is a step-by-step guide to setting up virtual domains using Postfix (MTA) and Dovecot (IMAP/SMTP server) on Ubuntu.
Before starting, ensure your server has:
example.com) with DNS records pointing to your server’s IP.sudo apt update && sudo apt upgrade).Install the core mail server packages:
sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd dovecot-lmtpd
During Postfix installation, select “Internet Site” as the configuration type and enter your domain name (e.g., example.com) when prompted.
Postfix needs to be set up to handle multiple domains. Edit the main configuration file:
sudo nano /etc/postfix/main.cf
Modify or add the following parameters (replace example.com with your primary domain):
myhostname = mail.example.com # Server hostname (FQDN)
mydomain = example.com # Primary domain
myorigin = $mydomain # Default domain for outgoing emails
inet_interfaces = all # Listen on all network interfaces
inet_protocols = ipv4 # Use IPv4 (or "all" for IPv6 support)
mydestination = $myhostname, localhost.$mydomain, localhost # Local domains
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 # Trusted networks
home_mailbox = Maildir/ # Mail storage format (Maildir)
virtual_alias_domains = example.com, test.com # List of virtual domains
virtual_alias_maps = hash:/etc/postfix/virtual # Mapping file for virtual aliases
Define how virtual addresses map to local users. Create/edit the virtual alias file:
sudo nano /etc/postfix/virtual
Add one line per virtual address (replace user1/user2 with local system users):
user1@example.com user1
user2@example.com user2
user3@test.com user2 # Map multiple virtual addresses to one local user
Convert the file to a Postfix-readable hash database:
sudo postmap /etc/postfix/virtual
Apply changes and restart the service:
sudo systemctl restart postfix
Dovecot needs to support virtual users and mail storage. Edit the main configuration file:
sudo nano /etc/dovecot/dovecot.conf
Ensure the following lines are present (enable IMAP/LMTP protocols and set mail location):
protocols = imap lmtp
mail_location = maildir:~/Maildir # Matches Postfix's home_mailbox setting
Edit the authentication file to allow local users (or databases for larger setups):
sudo nano /etc/dovecot/conf.d/10-auth.conf
Modify these parameters:
disable_plaintext_auth = no # Allow plaintext passwords (for testing; disable in production)
auth_mechanisms = plain login # Supported authentication methods
Configure Dovecot to receive mail from Postfix via LMTP. Edit the LMTP settings:
sudo nano /etc/dovecot/conf.d/10-master.conf
Add/uncomment the following block (adjust permissions for Postfix):
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
Apply changes and restart the service:
sudo systemctl restart dovecot
For your virtual domains to work, add the following DNS records (via your domain registrar or DNS provider):
mail.example.com with priority 10).mail.example.com → 192.0.2.1).Verify your setup using command-line tools:
Test SMTP: Connect to your server’s SMTP port (25) to ensure it responds:
telnet mail.example.com 25
You should see a Postfix welcome message.
Test IMAP: Connect to your server’s IMAP port (143) to verify Dovecot is running:
telnet mail.example.com 143
You should see a Dovecot welcome message.
Send Test Emails: Use a mail client (e.g., Thunderbird) or command-line tool (e.g., swaks) to send emails to virtual addresses (e.g., user1@example.com). Check the Maildir folder in the corresponding local user’s home directory (e.g., /home/user1/Maildir/new) for received emails.
ufw or your firewall.By following these steps, your Ubuntu mail server will be able to host email addresses for multiple virtual domains, providing a scalable solution for small to medium-sized email hosting needs.