搭建完整邮件服务器:Ubuntu 22.04 上的 Postfix + Dovecot

Postfix Dovecot 邮件服务器 Ubuntu 生产环境
披露:Email Wiki 上的部分链接为 VPS 服务商(如 Vultr)的联盟链接。通过这些链接购买,我们可能获得佣金,但不会对您产生额外费用。这有助于我们保持网站免费。所有教程均独立撰写,我们仅推荐我们认为适合自建邮件服务器的可靠服务。

本指南从零开始搭建一台完整的生产级自建邮件服务器。完成后你将拥有:

  • Postfix 处理 SMTP(发送和接收)
  • Dovecot 处理 IMAP/POP3(邮件读取)
  • 全程 TLS 加密(Let’s Encrypt)
  • SPF、DKIM、DMARC 完整配置
  • 虚拟邮箱,支持多域名和多用户

架构概览

互联网


Postfix(25 端口——入站 SMTP)
Postfix(587 端口——提交端口,需认证)


Dovecot(投递到 Maildir)


邮件客户端(IMAP 993 端口 / POP3 995 端口)

前提条件

  • 全新的 Ubuntu 22.04 VPS,至少 1 GB 内存
  • 域名:example.com
  • 邮件子域名:mail.example.com,A 记录指向服务器 IP
  • 防火墙已开放 25、587、993、995 端口

第一阶段:系统准备

sudo apt update && sudo apt upgrade -y
sudo hostnamectl set-hostname mail.example.com

编辑 /etc/hosts

你的IP  mail.example.com  mail

安装所有依赖包:

sudo apt install -y postfix postfix-mysql dovecot-core dovecot-imapd \
  dovecot-pop3d dovecot-lmtpd dovecot-mysql opendkim opendkim-tools \
  certbot mailutils

Postfix 对话框出现时选择 No Configuration——我们稍后手动配置。

第二阶段:申请 TLS 证书

sudo certbot certonly --standalone -d mail.example.com

证书位于 /etc/letsencrypt/live/mail.example.com/

设置续期后自动重载服务:

cat > /etc/letsencrypt/renewal-hooks/post/mail.sh << 'EOF'
#!/bin/bash
systemctl reload postfix dovecot
EOF
chmod +x /etc/letsencrypt/renewal-hooks/post/mail.sh

第三阶段:配置 Postfix

main.cf

sudo nano /etc/postfix/main.cf
# 基本设置
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = all

# 入站 TLS(smtpd)
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file  = /etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_tls_security_level = may
smtpd_tls_auth_only = yes

# 出站 TLS(smtp)
smtp_tls_security_level = may
smtp_tls_loglevel = 1

# 通过 Dovecot 进行 SASL 认证
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

# 虚拟邮箱
virtual_mailbox_domains = example.com
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_minimum_uid = 100
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

# 中继限制
smtpd_relay_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination

# DKIM milter
milter_default_action = accept
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891

虚拟邮箱映射

创建 /etc/postfix/vmailbox

user@example.com    example.com/user/
admin@example.com   example.com/admin/

生成索引:

sudo postmap /etc/postfix/vmailbox

master.cf——启用 587 提交端口

编辑 /etc/postfix/master.cf,取消注释或添加 submission 服务:

submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject

创建虚拟邮件目录

sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 vmail -d /var/mail/vhosts
sudo mkdir -p /var/mail/vhosts/example.com
sudo chown -R vmail:vmail /var/mail/vhosts

第四阶段:配置 Dovecot

10-mail.conf

mail_location = maildir:/var/mail/vhosts/%d/%n
mail_uid = vmail
mail_gid = vmail

10-auth.conf

disable_plaintext_auth = yes
auth_mechanisms = plain login

!include auth-static.conf.ext

创建 /etc/dovecot/conf.d/auth-static.conf.ext(测试用静态配置):

passdb {
  driver = static
  args = password=changeme
}

userdb {
  driver = static
  args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}

注意:生产环境请使用数据库认证替换此静态配置。

10-ssl.conf

ssl = required
ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
ssl_key  = </etc/letsencrypt/live/mail.example.com/privkey.pem
ssl_min_protocol = TLSv1.2

10-master.conf——LMTP 和认证套接字

service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0600
    user = postfix
    group = postfix
  }
}

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

第五阶段:配置 DKIM

sudo mkdir -p /etc/opendkim/keys/example.com
sudo opendkim-genkey -b 2048 -d example.com -s mail \
  -D /etc/opendkim/keys/example.com/
sudo chown -R opendkim:opendkim /etc/opendkim/keys/

# 查看需要发布的 DNS 记录
cat /etc/opendkim/keys/example.com/mail.txt

编辑 /etc/opendkim.conf

Mode            sv
Domain          example.com
Selector        mail
KeyFile         /etc/opendkim/keys/example.com/mail.private
Socket          inet:8891@localhost

第六阶段:DNS 记录

在域名 DNS 中添加以下记录:

类型名称
MXexample.commail.example.com(优先级 10)
Amail.example.com服务器 IP
TXTexample.comv=spf1 ip4:服务器IP ~all
TXTmail._domainkey.example.com(来自 mail.txt 的内容)
TXT_dmarc.example.comv=DMARC1; p=none; rua=mailto:admin@example.com

同时在 VPS 服务商控制台配置反向 DNS(PTR 记录)。

第七阶段:启动并测试

sudo systemctl restart opendkim postfix dovecot
sudo systemctl enable opendkim postfix dovecot

发送测试邮件:

echo "测试邮件正文" | mail -s "测试" someone@gmail.com

查看日志:

sudo tail -f /var/log/mail.log

用邮件客户端测试连接:

  • IMAP:mail.example.com:993(SSL/TLS),用户 user@example.com
  • SMTP:mail.example.com:587(STARTTLS),相同凭据

运维清单

  • TLS 证书续期sudo certbot renew --dry-run
  • 黑名单检查:每周用 MXToolbox 检查一次
  • 软件更新:每月 sudo apt upgrade
  • DMARC 策略升级:分析报告后从 p=none 逐步提升到 p=reject

准备好部署你的邮件服务器了吗?

你需要一台 IP 信誉干净的可靠 VPS。立即使用 Vultr——独立 IP、NVMe SSD、全球数据中心,最低 $6/月。