在 Ubuntu 上部署加密邮件服务器,核心目标是:
✅ 传输加密(SMTP / IMAP / POP3)
✅ 身份认证加密
✅ 防止明文泄露和中间人攻击
下面按实用部署流程给你一个完整方案(以 Postfix + Dovecot + Let’s Encrypt 为例,这是最常用、最成熟的组合)。
| 组件 | 作用 | 加密方式 |
|---|---|---|
| Postfix | SMTP 发信 / 收信 | STARTTLS(25/587) |
| Dovecot | IMAP / POP3 | SSL/TLS(993/995) |
| Let’s Encrypt | 免费证书 | TLS 加密 |
| MySQL / SQLite(可选) | 虚拟用户 | 密码加密存储 |
假设你的邮件服务器域名是:
mail.example.com
DNS 设置示例:
| 记录类型 | 主机 | 值 |
|---|---|---|
| A | mail.example.com | 服务器公网 IP |
| MX | example.com | mail.example.com |
| TXT | example.com | SPF / DKIM / DMARC(后面讲) |
sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d \
mailutils certbot
安装 Postfix 时选择:
example.comsudo certbot certonly --standalone -d mail.example.com
证书路径:
/etc/letsencrypt/live/mail.example.com/
├── fullchain.pem
└── privkey.pem
/etc/postfix/main.cf# TLS 证书
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
# 启用 TLS
smtpd_use_tls = yes
smtpd_tls_security_level = may
# 强制提交端口加密
smtpd_tls_auth_only = yes
# 日志
smtpd_tls_loglevel = 1
# 端口
smtpd_tls_received_header = yes
/etc/postfix/master.cf启用 587(提交端口):
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
重启:
sudo systemctl restart postfix
✅ 效果:
/etc/dovecot/conf.d/10-ssl.confssl = required
ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem
/etc/dovecot/conf.d/10-master.conf确保监听加密端口:
service imap-login {
inet_listener imap {
port = 143
}
inet_listener imaps {
port = 993
ssl = yes
}
}
重启:
sudo systemctl restart dovecot
✅ 效果:
Dovecot 提供认证:
# /etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain login
Postfix 使用 Dovecot SASL:
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
✅ 禁止明文认证
disable_plaintext_auth = yes
✅ 只在加密通道允许认证
ssl = required
sudo certbot renew --dry-run
自动续期脚本(推荐):
sudo systemctl restart postfix dovecot
防止伪造邮件
防止暴力破解
sudo apt install fail2ban
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_protocols = !SSLv2, !SSLv3
openssl s_client -connect mail.example.com:993
openssl s_client -starttls smtp -connect mail.example.com:25
✅ 必须做的
✅ 推荐做的
如果你愿意,我可以:
你现在的 Ubuntu 版本是 20.04 还是 22.04 / 24.04?