
Email migrations rarely fail with a bang. They fail quietly. An old MX keeps accepting messages. An IMAP client decides to re-download 30GB. SPF alignment slips, and invoices start landing in spam. This mail server migration tutorial shows a controlled move to a new VPS with a staged cutover, mailbox sync, and pre-DNS checks.
The steps assume a common small-business stack: Postfix for SMTP and Dovecot for IMAP. Mailbox storage is Maildir or mdbox. The approach is provider-agnostic, but the paths and commands are real Linux examples you can copy as-is.
What you’re migrating (and what you can safely leave behind)
Before you copy files, decide what must move and what you should rebuild clean.
- Must move: mailbox data (Maildir/mdbox), user accounts/credentials, TLS certs (or re-issue), DKIM keys, and your DNS records (MX, SPF, DKIM, DMARC, autoconfig/autodiscover if used).
- Usually rebuild: OS packages, Postfix/Dovecot base configs (then re-apply your deltas), spam filtering rules, firewall rules, monitoring.
- Don’t migrate blindly: mail queues, old logs, and “mystery” cron jobs. Moving them tends to move the mess.
If you host multiple domains (or client mailboxes), consider running mail on its own VPS instead of sharing with web hosting. This limits the blast radius and keeps routine maintenance simpler.
If you’d rather have someone handle OS updates and baseline hardening while you focus on mail flow, managed VPS hosting is a practical middle ground for 2026.
Prerequisites checklist (do this before you spin cycles)
- New VPS with enough disk for mailboxes + growth (plan 30–50% headroom).
- Static IPv4 (and IPv6 if you use it) and root access.
- Access to current DNS zone (registrar or DNS provider).
- Admin access to old mail server (SSH preferred).
- A maintenance window (even if you aim for “no downtime”, you still need a cutover moment).
Time sync matters. A drifting clock can break TLS handshakes. It can also produce confusing DKIM validation results. Check NTP first:
timedatectl status
chronyc tracking 2>/dev/null || true
If you see drift warnings, fix them before you do anything else. HostMyCode has a focused walkthrough here: VPS time sync troubleshooting.
Step 1: Reduce DNS TTL ahead of the cutover
Lower TTL so caches expire quickly when you switch MX. Do this at least 12–24 hours before moving mail.
- Find current records: MX, A/AAAA for mail hostnames, SPF TXT, DKIM TXT, DMARC TXT.
- Set TTL to 300 seconds for:
- MX records
- mail.yourdomain.tld A/AAAA
- autodiscover/autoconfig records (if you have them)
If you want a cutover plan that includes rollback, keep this handy: DNS cutover tutorial. It’s written for websites, but the TTL discipline and verification steps apply directly to mail.
Step 2: Build the new mail server baseline (Postfix + Dovecot)
This tutorial targets Ubuntu 24.04 LTS or Debian 12/13-class systems (common picks in 2026). If you’re on AlmaLinux/Rocky, adjust package names accordingly.
Install packages
apt update
apt -y install postfix dovecot-imapd dovecot-pop3d dovecot-lmtpd ca-certificates rsync
During Postfix setup, choose:
- General type: Internet Site (typical for direct delivery) or Satellite (if relaying)
- System mail name: your primary mail hostname (example: mail.example.com)
Confirm service status
systemctl status postfix --no-pager
systemctl status dovecot --no-pager
ss -lntp | egrep ':(25|465|587|143|993)\b' || true
If the expected ports aren’t listening, stop and fix that now.
Migrating data onto a half-working stack just creates a longer outage later.
Step 3: Create mail users and mailbox structure that matches the old server
Your life gets easier if usernames, domains, and mailbox paths stay the same.
- If you use system users with Maildir under
/home/USER/Maildir, recreate the same users on the new server. - If you use virtual users (common in hosted mail), you’ll likely have a user database (SQL/LDAP) or flat files. Recreate the same accounts and password hashes.
Start by confirming what Dovecot is actually doing on the old server:
# Dovecot: where are mailboxes stored?
doveconf -n | egrep -i 'mail_location|namespace|userdb|passdb'
# Common mail roots to inspect
ls -la /var/mail 2>/dev/null || true
ls -la /var/vmail 2>/dev/null || true
Don’t guess the mail location. Verify it before you copy anything.
Step 4: Migrate mailbox data safely (rsync) with permissions intact
Mailbox sync is where “it looks fine” migrations go sideways.
Use rsync flags that preserve ownership, times, and symlinks. This helps Dovecot avoid a permissions mess.
Option A: Maildir copy (common)
Example: virtual mail stored in /var/vmail on the old server.
# On the NEW server
rsync -aHAX --numeric-ids --info=progress2 \
root@OLD_SERVER_IP:/var/vmail/ /var/vmail/
Option B: mdbox copy (Dovecot)
mdbox includes index files you can regenerate, but you still want a consistent data copy.
If you can afford a short pause, stop Dovecot on the old server. Otherwise, plan on a final delta sync close to cutover.
# On the old server (short pause if you can afford it)
systemctl stop dovecot
# On the new server
rsync -aHAX --numeric-ids root@OLD_SERVER_IP:/var/vmail/ /var/vmail/
# Then start dovecot back on the old server
# (only if you are doing a staged cutover)
Run a final delta sync near cutover
Right before the DNS move, run one more rsync pass to catch late-arriving mail:
rsync -aHAX --numeric-ids root@OLD_SERVER_IP:/var/vmail/ /var/vmail/
Pitfall: copying mailboxes without matching UID/GID mapping.
If your mail storage uses a dedicated user (like vmail), make sure it has the same UID/GID on the new server. If you can’t match IDs, keep --numeric-ids and recreate the user with the expected IDs.
Step 5: Move (or re-issue) TLS certificates for IMAP/SMTP
You’ve got two sensible choices:
- Re-issue Let’s Encrypt certs on the new server (usually easiest if you control DNS and can validate quickly).
- Migrate existing certificates and keys if you need continuity during a staged cutover.
If you plan to copy certificates between servers and want to avoid chain mismatches or renewal problems, follow: VPS SSL certificate migration tutorial. The same cert+key handling applies to IMAP and SMTP.
Typical Postfix TLS paths:
/etc/letsencrypt/live/mail.example.com/fullchain.pem/etc/letsencrypt/live/mail.example.com/privkey.pem
Typical Dovecot TLS config lives in:
/etc/dovecot/conf.d/10-ssl.conf
Quick TLS sanity checks
# SMTP STARTTLS (port 587)
openssl s_client -starttls smtp -connect mail.example.com:587 -servername mail.example.com </dev/null | egrep -i 'subject=|issuer=|Verify return code'
# IMAPS (port 993)
openssl s_client -connect mail.example.com:993 -servername mail.example.com </dev/null | egrep -i 'subject=|issuer=|Verify return code'
Step 6: Migrate DKIM keys and align your DNS records
DKIM problems show up immediately in deliverability.
A “new key, old selector” mismatch is a fast way to look suspicious to receivers. If you can, keep the same selector and key through the move.
Find DKIM material on the old server. Common locations:
- OpenDKIM:
/etc/opendkim/keys/ - Rspamd DKIM:
/var/lib/rspamd/dkim/
Copy keys securely, and tighten permissions as they land:
rsync -a --chmod=Du=rwx,Dgo=,Fu=rw,Fgo= \
root@OLD_SERVER_IP:/etc/opendkim/keys/ /etc/opendkim/keys/
Afterwards, confirm the published DKIM TXT record matches the key you’re using.
If you’re rebuilding DKIM end-to-end, use: DKIM setup guide tutorial.
SPF and DMARC: keep alignment during the move
- SPF: include both old and new sending IPs during the transition (temporary). Example:
v=spf1 ip4:OLD ip4:NEW a mx -all - DMARC: keep your policy steady. Don’t switch to
p=rejecton the same day you migrate.
For a deliverability “definition of done” (SPF/DKIM/DMARC/rDNS/TLS), see: Email deliverability setup guide.
Step 7: Prepare a staged cutover (dual-delivery window)
A clean cutover has one goal: new mail should land on the new server quickly.
At the same time, anything that still hits the old box should be pulled across until caches and retries settle.
Plan for a dual-delivery window:
- Lower TTL (done earlier).
- Update MX to point to the new server.
- Keep the old server running for 24–72 hours.
- Continue IMAP sync or mailbox rsync deltas during the window.
If you manage DNS, verify MX from more than one resolver:
dig +short MX example.com
# Check via a public resolver
dig @1.1.1.1 +short MX example.com
dig @8.8.8.8 +short MX example.com
Step 8: Sync mail that arrived during propagation (IMAP sync method)
File-level rsync is fast and reliable. IMAP sync is often safer on live systems.
It follows IMAP semantics and can reduce index-related surprises.
Install imapsync on the new server:
apt -y install imapsync
Example sync for a single mailbox:
imapsync \
--host1 old.example.com --user1 user@example.com --password1 'OLDPASS' \
--host2 new.example.com --user2 user@example.com --password2 'NEWPASS' \
--ssl1 --ssl2 \
--automap --syncinternaldates --nofoldersizes
Tip: run imapsync for your largest mailboxes first.
If throughput, throttling, or disk is going to bite you, you want to learn that early.
Step 9: Lock down auth, relaying, and rate limits (before customers notice)
A fresh mail server with loose auth settings attracts brute force attempts and compromises. Tighten the basics before you cut users over.
- Disable plaintext auth without TLS in Dovecot (
disable_plaintext_auth = yes). - Force submission on 587 with STARTTLS; keep 25 for server-to-server SMTP.
- Set sane per-user/per-IP rate limits (Postfix
smtpd_client_message_rate_limit, policyd, or your filter stack). - Restrict outbound relaying to authenticated users only.
Also put a firewall policy in place that allows only what you need (25/465/587/143/993 + SSH from admin IPs).
If you haven’t built your baseline, use: VPS firewall setup guide.
Step 10: Validate outbound deliverability (fast tests that catch real problems)
Run these checks before you tell anyone “the migration is done.”
They catch slow leaks: spam placement, throttling, and queues that grow quietly overnight.
1) SMTP banner and hostname
postconf -n | egrep '^myhostname|^mydomain|^myorigin'
# See the banner from outside
( echo -e "EHLO test\r\nQUIT\r\n"; sleep 1 ) | nc -w 3 mail.example.com 25
If hostname/EHLO is wrong, fix it. It affects reputation, and some receiving MTAs will throttle or distrust you.
For a clean fix path, follow: Email server hostname setup tutorial.
2) SPF/DKIM/DMARC alignment
- Send a test email to a mailbox you control on a large provider (Gmail/Microsoft/Yahoo).
- Check the message headers for SPF pass, DKIM pass, and DMARC pass.
3) Queue health
mailq
postqueue -p
A growing queue usually points to DNS problems, blocked port 25, or reputation issues.
Treat it as urgent, not “tomorrow’s task.”
Step 11: Update client settings without chaos
Your users don’t care about the cutover plan. They care that mail works.
They also care that devices stop prompting for passwords and sending doesn’t suddenly fail.
- Preferred approach: keep the same hostnames (example:
mail.example.com) and only change DNS A/AAAA to the new IP. Most clients won’t notice. - If hostnames must change: write down the new IMAP/SMTP settings and consider publishing autoconfig/autodiscover records.
If you run multiple domains and want consistent nameserver management (especially useful for resellers), centralizing DNS helps you keep mail hostnames stable through future moves.
For cPanel environments, see: cPanel DNS cluster setup tutorial.
Step 12: Decommission the old server safely (don’t rush this)
Give it a few days of normal traffic before you retire anything. Then shut the old server down in a deliberate order.
- Verify MX points only to the new server and TTLs have normalized.
- Confirm no inbound SMTP hits on the old server (check mail logs and
ss -tn state established). - Run a final IMAP sync or rsync delta.
- Disable outbound SMTP on the old server to prevent accidental sends.
- Take a final snapshot/backup, then power off.
If you want a disciplined “prove it works” process with rollback thinking, do a restore rehearsal on a staging VPS.
HostMyCode’s drill-focused guide is useful even if you’re not doing disaster recovery: VPS restore drill tutorial.
Troubleshooting quick diagnostics (common post-migration surprises)
Mail arrives on the old server after cutover
- Check MX at multiple resolvers (some caching is normal if TTL wasn’t lowered in time).
- Confirm you didn’t leave a higher-priority MX pointing to the old host.
- Keep pulling mail via imapsync during the overlap window.
Users can receive but can’t send
- Submission port (587) blocked by firewall/security group.
- SASL auth misconfigured or wrong password database.
- STARTTLS failing due to wrong certificate path.
ss -lntp | egrep ':(587|465)\b'
journalctl -u postfix -n 200 --no-pager
Mail goes to spam right after migration
- SPF not updated for the new sending IP.
- DKIM selector mismatch or private key permissions wrong.
- New IP reputation not warmed up; rate-limit and avoid blasting newsletters day one.
Summary: your “clean migration” checklist
- TTL lowered a day ahead.
- New server baseline installed and listening on the right ports.
- Mailbox storage copied with correct UID/GID and a final delta sync.
- TLS validated for IMAP and SMTP submission.
- SPF includes old+new IPs during transition; DKIM keys consistent; DMARC stable.
- MX updated, old server kept online briefly, and sync continues during propagation.
- Queue monitored; headers checked for SPF/DKIM/DMARC pass.
If you’re migrating business-critical email and expect heavy use (large mailboxes, lots of concurrent IMAP connections, frequent sends), start with a right-sized VPS.
A HostMyCode VPS gives you dedicated resources and predictable network controls, with room to scale without redesigning your mail setup.
If you’re moving mail as part of a broader hosting upgrade, keep the cutover predictable: stable IPs, fast storage, and support that understands SMTP and DNS side-effects. HostMyCode offers both VPS and managed VPS hosting that fit email + web workloads without locking you into a complicated platform.
FAQ
How long should I keep the old mail server online after switching MX?
Keep it online for 24–72 hours. That window covers late DNS caches and delayed retries from other mail servers. Continue syncing mail during this period.
Should I change IP and hostname at the same time?
Try not to. Keeping the same mail hostname and only changing the A/AAAA record reduces client-side problems and avoids reconfiguring devices.
Is rsync enough, or do I need imapsync?
rsync is fine for a controlled cutover with a final delta sync. imapsync is safer if the old server must stay live and you want to preserve IMAP state with less risk of index issues.
Do I need to update SPF during a staged migration?
Yes. Temporarily allow both old and new sending IPs in SPF until you’re sure nothing sends from the old server. Then remove the old IP to tighten policy.
What’s the fastest way to spot a deliverability problem after the move?
Check a sent message’s headers for SPF/DKIM/DMARC results and watch the outbound queue (mailq). If the queue grows, investigate immediately.