How to Configure Postfix with Dovecot Lmtp and Virtual Mailbox Domains for Multi-domain Email Hosting on Ubuntu Server
Learning how to configure Postfix with Dovecot LMTP and virtual mailbox domains for multi-domain email hosting on Ubuntu Server gives you full control over your own mail infrastructure. Instead of paying for hosted email services, you can host multiple domains on a single server. This tutorial walks you through the complete setup from package installation to a working multi-domain mail server. You’ll configure Postfix to accept mail for multiple domains, set up Dovecot to deliver messages via LMTP, and create virtual mailbox storage. By the end, you’ll have a production-ready mail server that handles separate mailboxes for different domains. This guide targets Ubuntu 22.04 LTS, but the steps work on Ubuntu 20.04 as well.
Prerequisites for Configuring Postfix with Dovecot LMTP and Virtual Mailboxes
Before you start, make sure you have the following in place.
Required access and software:
- A fresh Ubuntu 22.04 LTS server with root or sudo access
- A fully qualified domain name (FQDN) pointed at your server’s IP
- At least two domain names with DNS MX records configured
- Ports 25, 143, and 587 open in your firewall
- Basic Linux command-line experience
Estimated time: 45–60 minutes
You should be comfortable editing configuration files with a text editor like nano or vim. You don’t need prior Postfix or Dovecot experience. Each step explains what you’re doing and why. Make sure your server’s hostname is set correctly before you begin. Run hostnamectl set-hostname mail.yourdomain.com and update /etc/hosts to match. A correct hostname prevents many common mail server errors later.
For DNS, each domain needs an MX record pointing to your mail server’s hostname. Give DNS changes at least 30 minutes to propagate before testing.
Step-by-Step Guide to Multi-domain Email Hosting with Postfix and Dovecot
See also: How to Containerize a Node.js Web Application with Docker
Step 1: Install Postfix and Dovecot
Update your package list first. Then install both packages together.
sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd dovecot-lmtpd -y
During the Postfix install prompt, select Internet Site. Enter your primary mail domain when asked. This sets the initial myhostname value.
Step 2: Create the Virtual Mailbox User and Directory
You need a dedicated system user to own all mailboxes. This keeps permissions clean and secure.
sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 vmail -d /var/mail/vmail -m
This creates the vmail user with UID and GID 5000. All virtual mailboxes will live under /var/mail/vmail/.
Step 3: Configure Postfix for Virtual Mailbox Domains
Open the main Postfix configuration file.
sudo nano /etc/postfix/main.cf
Add or update these directives at the bottom of the file.
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = localhost
virtual_mailbox_domains = /etc/postfix/virtual_domains
virtual_mailbox_base = /var/mail/vmail
virtual_mailbox_maps = hash:/etc/postfix/virtual_mailbox
virtual_minimum_uid = 100
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
# Deliver via Dovecot LMTP
virtual_transport = lmtp:unix:private/dovecot-lmtp
The virtual_transport line tells Postfix to hand off all incoming mail to Dovecot using the LMTP socket. This is the key integration point between the two services.
Step 4: Create Virtual Domain and Mailbox Map Files
Create the file that lists your hosted domains.
sudo nano /etc/postfix/virtual_domains
Add each domain on its own line.
domain1.com OK
domain2.com OK
Now create the mailbox map file.
sudo nano /etc/postfix/virtual_mailbox
Add your virtual users in this format.
[email protected] domain1.com/user/
[email protected] domain2.com/admin/
Each entry maps an email address to a maildir path under /var/mail/vmail/. Now compile both files into Postfix lookup tables.
sudo postmap /etc/postfix/virtual_domains
sudo postmap /etc/postfix/virtual_mailbox
Step 5: Configure Dovecot for LMTP Delivery
Edit the Dovecot main configuration file to enable LMTP.
sudo nano /etc/dovecot/dovecot.conf
Make sure this line is present.
protocols = imap lmtp
Now configure the mail location. Open the mail configuration file.
sudo nano /etc/dovecot/conf.d/10-mail.conf
Set the mail location to use Maildir format.
mail_location = maildir:/var/mail/vmail/%d/%n/
Here %d is the domain and %n is the username. This organizes mailboxes cleanly by domain.
Step 6: Configure the Dovecot LMTP Socket
Open the master configuration file to define the LMTP service socket.
sudo nano /etc/dovecot/conf.d/10-master.conf
Find the service lmtp block and update it to match this.
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
This creates a Unix socket inside the Postfix chroot directory. Postfix can read from it directly. The permissions ensure only the Postfix process can write to the socket.
Step 7: Configure Dovecot Authentication
Open the authentication configuration file.
sudo nano /etc/dovecot/conf.d/10-auth.conf
Set the authentication mechanism and disable plaintext login over unencrypted connections.
disable_plaintext_auth = yes
auth_mechanisms = plain login
For virtual users, you’ll use a password file. Open the passdb configuration and point it to your password file.
sudo nano /etc/dovecot/conf.d/auth-passwdfile.conf.ext
passdb {
driver = passwd-file
args = scheme=SHA512-CRYPT username_format=%u /etc/dovecot/users
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/mail/vmail/%d/%n
}
Create the users file and add a hashed password for each virtual user.
sudo doveadm pw -s SHA512-CRYPT
This command prompts you for a password and outputs the hash. Add each user to /etc/dovecot/users in this format.
[email protected]:{SHA512-CRYPT}$6$hashedpasswordhere
Step 8: Restart Services and Test
Restart both services to apply all changes.
sudo systemctl restart postfix dovecot
sudo systemctl enable postfix dovecot
Check that both services are running without errors.
sudo systemctl status postfix
sudo systemctl status dovecot
Send a test email using swaks or Telnet. You can also check the official Postfix documentation for advanced delivery testing options.
Troubleshooting Common Issues with Postfix and Dovecot LMTP
Problem: Mail bounces with “User unknown in virtual mailbox table”
Run sudo postmap -q [email protected] hash:/etc/postfix/virtual_mailbox to verify the lookup works. If it returns nothing, recheck your virtual_mailbox file formatting and rerun postmap.
Problem: LMTP socket not found
Check that the socket path in main.cf matches the path in 10-master.conf. Both must reference private/dovecot-lmtp. Verify the socket exists with ls -la /var/spool/postfix/private/.
