Self-Hosted Mail Server on a Linux VPS: Postfix, Dovecot and DKIM Setup

Running your own mail server gives you full control over email routing, retention, and compliance. This guide walks through a production Postfix + Dovecot setup with SPF, DKIM, DMARC, and TLS on a KVM Linux VPS.

Self-hosting a mail server on a VPS gives you complete control over email routing, storage retention, data jurisdiction, and deliverability configuration. The trade-off is maintenance responsibility: you own the spam reputation, certificate renewals, and security patching. This guide covers a production-grade setup using Postfix (SMTP) and Dovecot (IMAP/POP3) with proper authentication records.

Prerequisites

  • A KVM Linux VPS with a dedicated IPv4 address (not a shared or NAT IP)
  • A domain you control with access to DNS zone records
  • Port 25 (SMTP) not blocked by your hosting provider (confirm before ordering—some providers block port 25 on entry-level plans)
  • Reverse DNS (PTR) record set for your server IP pointing to your mail hostname (e.g. mail.yourdomain.com)

Confirm port 25 availability with StreamData before provisioning, as outbound SMTP is sometimes restricted on shared IP ranges to prevent abuse. A clean, dedicated IP with a PTR record set correctly is the foundation of deliverability.

Install Postfix and Dovecot

apt update
apt install -y postfix dovecot-core dovecot-imapd dovecot-pop3d \
  opendkim opendkim-tools certbot

During Postfix installation, select Internet Site and enter your mail hostname (e.g. mail.yourdomain.com).

Postfix: Basic SMTP Configuration

# /etc/postfix/main.cf (key settings)
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, $mydomain
relayhost =

# TLS for inbound SMTP
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls = yes
smtpd_tls_auth_only = yes
smtp_tls_security_level = may

# Submission (port 587) for authenticated client sends
# Enable in /etc/postfix/master.cf:
# submission inet n - y - - smtpd
#   -o syslog_name=postfix/submission
#   -o smtpd_sasl_auth_enable=yes

Dovecot: IMAP Configuration

# /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir

# /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = yes
auth_mechanisms = plain login

# /etc/dovecot/conf.d/10-ssl.conf
ssl = required
ssl_cert = </etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.yourdomain.com/privkey.pem

Let's Encrypt Certificate

certbot certonly --standalone -d mail.yourdomain.com
# Renewal hook to reload Postfix and Dovecot
echo 'post-hook = systemctl reload postfix dovecot' \
  >> /etc/letsencrypt/renewal/mail.yourdomain.com.conf

SPF, DKIM and DMARC DNS Records

These three DNS records are mandatory for deliverability to Gmail, Outlook, and other major providers.

SPF

yourdomain.com. IN TXT "v=spf1 mx ip4:<your-server-ip> ~all"

SPF tells receiving servers which IPs are authorised to send email from your domain. ~all (softfail) is standard for most setups; switch to -all (hardfail) only after confirming no legitimate email comes from other sources.

DKIM with OpenDKIM

mkdir /etc/opendkim/keys/yourdomain.com
opendkim-genkey -s mail -d yourdomain.com -D /etc/opendkim/keys/yourdomain.com/
cat /etc/opendkim/keys/yourdomain.com/mail.txt  # Copy this TXT record to DNS

DKIM signs outgoing messages with a private key. The receiving server fetches the public key from your DNS TXT record (mail._domainkey.yourdomain.com) and verifies the signature. This cryptographically proves the message was not modified in transit.

DMARC

_dmarc.yourdomain.com. IN TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc@yourdomain.com; pct=100"

DMARC tells receiving servers what to do when SPF and DKIM both fail (p=quarantine moves to spam; p=reject drops the message). The rua address receives aggregate reports so you can monitor alignment.

Deliverability Checklist Before Sending

  • PTR (reverse DNS) record set: dig -x <your-server-ip> returns mail.yourdomain.com
  • Port 25 inbound open (confirm with nc -zv <your-server-ip> 25)
  • SPF TXT record published and tested with dig TXT yourdomain.com
  • DKIM signing active and verified with opendkim-testkey -d yourdomain.com -s mail -vvv
  • DMARC record published
  • MX record pointing to your mail hostname with correct priority
  • Server IP not on any major blocklists (check with MXToolbox Blacklist Check)

StreamData VPS Sizing for Mail Servers

A mail server for a small-to-medium organisation (50–500 users, moderate traffic) runs well on a 2–4 vCPU KVM VPS with 4–8 GB RAM and 50–100 GB RAID NVMe for mail storage. For large organisations with retention requirements, plan storage based on average mailbox size and retention period, then add 50% headroom. StreamData's 1 Gbps and 10 Gbps port options where available ensure outbound delivery throughput is not the bottleneck.

See StreamData VPS plans for full specifications and pricing, and StreamData Company page for infrastructure and upstream details.

Monitoring Your Mail Server

A self-hosted mail server needs active monitoring to catch delivery failures, spam relay abuse, and disk full conditions before they cause outage. Install Prometheus node_exporter on the mail server and add postfix-specific metrics with the postfix_exporter (available at github.com/kumina/postfix_exporter). Key metrics to alert on:

  • Mail queue length: A growing deferred queue indicates downstream delivery failures or local processing backlogs. Alert if postfix_showq_message_age_seconds spikes.
  • Disk space on the mail partition: Full disk stops Dovecot from delivering new messages. Alert at 80% disk utilisation.
  • Fail2ban bans: A spike in SSH or SMTP authentication bans indicates active brute-force activity. Monitor /var/log/fail2ban.log.
  • Certificate expiry: Let's Encrypt certificates expire every 90 days. Confirm auto-renewal is working by running certbot renew --dry-run weekly via cron.

Dovecot logs IMAP connection events to /var/log/mail.log on Ubuntu/Debian. Postfix logs all delivery events to the same file. Forwarding these logs to a centralised logging platform (Loki, Graylog, or a simple remote syslog receiver) provides a full audit trail of who sent and received email, which is useful for compliance and troubleshooting.

Anti-Spam Configuration

A production mail server requires spam filtering to avoid becoming a relay for unwanted mail. Two common approaches:

  • SpamAssassin: Rules-based spam scoring that integrates with Postfix via amavisd-new. Marks spam with a header score and can automatically route high-score messages to a spam folder via Sieve filters in Dovecot.
  • Rspamd: A faster, more modern alternative to SpamAssassin with built-in DKIM signing, DMARC enforcement, greylisting, and Redis-backed statistics. It integrates directly with Postfix as a milter.

Without spam filtering, your mail server will accumulate a poor reputation within days if any account credentials are compromised—outbound spam ruins deliverability for all users on the server.


Sources