温馨提示×

Ubuntu邮件服务器如何加密

小樊
37
2026-07-05 13:10:04
栏目: 云计算

在 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(可选) 虚拟用户 密码加密存储

二、准备条件

1️⃣ 域名与 DNS(必须)

假设你的邮件服务器域名是:

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 时选择:

  • Internet Site
  • 系统邮件名:example.com

四、使用 Let’s Encrypt 申请 SSL 证书(关键)

1️⃣ 申请证书

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

证书路径:

/etc/letsencrypt/live/mail.example.com/
├── fullchain.pem
└── privkey.pem

五、Postfix 加密配置(SMTP)

1️⃣ 主配置文件 /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

2️⃣ Master 配置 /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

✅ 效果:

  • 25 端口:可加密(STARTTLS)
  • 587 端口:强制加密

六、Dovecot 加密配置(IMAP / POP3)

1️⃣ /etc/dovecot/conf.d/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

2️⃣ /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

✅ 效果:

  • IMAP:143(STARTTLS)
  • IMAPS:993(强制 SSL)
  • POP3:995(SSL)

七、密码与认证加密(非常重要)

1️⃣ 启用 SASL(Postfix + 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

九、自动续期 Let’s Encrypt 证书

sudo certbot renew --dry-run

自动续期脚本(推荐):

sudo systemctl restart postfix dovecot

十、额外安全增强(强烈建议)

✅ DKIM / SPF / DMARC

防止伪造邮件

✅ Fail2ban

防止暴力破解

sudo apt install fail2ban

✅ 禁用不安全的加密算法

smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_protocols = !SSLv2, !SSLv3

十一、验证加密是否成功

1️⃣ 检查端口加密

openssl s_client -connect mail.example.com:993
openssl s_client -starttls smtp -connect mail.example.com:25

2️⃣ 邮件客户端

  • IMAP:993 SSL/TLS
  • SMTP:587 STARTTLS
  • 不使用 25 端口发送认证邮件

十二、总结(重点)

必须做的

  • Let’s Encrypt 免费证书
  • Postfix + Dovecot TLS 加密
  • 587 / 993 / 995 端口加密
  • 禁用明文认证

推荐做的

  • DKIM / SPF / DMARC
  • Fail2ban
  • 定期更新系统

如果你愿意,我可以:

  • ✅ 给你 完整的 postfix + dovecot 配置文件
  • ✅ 帮你 排查邮件被 Gmail 拒收问题
  • ✅ 指导 用 Docker 部署加密邮件服务器
  • ✅ 配置 群晖 / 企业内网邮件系统

你现在的 Ubuntu 版本是 20.04 还是 22.04 / 24.04

0