Postfix 上手教程
关联项目:Postfix 前置要求:一台有固定公网 IP 的 Linux 主机;已配置域名 A/MX 记录和反向 DNS;熟悉 DNS 与 Linux 命令行;理解 SMTP 基本流程。 预计耗时:基础安装约 20 分钟;配通 Postfix + Dovecot 收发与 TLS 约 1–2 小时。 官方仓库:github.com/vdukhovni/postfix 前置要求:一台有固定公网 IP 的 Linux 主机;已配置域名 A/MX 记录和反向 DNS;熟悉 DNS 与 Linux 命令行;理解 SMTP 基本流程。
预计耗时:基础安装约 20 分钟;配通 Postfix + Dovecot 收发与 TLS 约 1–2 小时。
官方仓库:github.com/vdukhovni/postfix
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
Debian/Ubuntu 基础依赖(RHEL 系用 dnf install postfix mailx):
sudo apt update
sudo apt install -y postfix mailutils
安装过程会提示选择站点类型,选「Internet Site」并填入主域名;完整版本与参数以 postfix.org 官方文档为准。DNS 至少应具备:
example.com. IN MX 10 mail.example.com.
mail.example.com. IN A 203.0.113.10
example.com. IN TXT "v=spf1 mx -all"
_dmarc.example.com. IN TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com"
二、安装部署
安装后检查配置与运行参数:
sudo postfix check
postconf -n | grep -E '^(myhostname|mydestination|mynetworks)'
systemctl status postfix --no-pager
预期:postfix check 无输出表示语法通过;postconf -n 打印非默认参数(如 myhostname = mail.example.com);systemctl status 显示 active (running)。若报 fatal: 错误,先检查 /etc/postfix/main.cf。
三、快速上手:跑通第一个示例
最小可用配置:让本机域收发邮件(把 example.com 换成自己的域)。
sudo postconf -e 'myhostname = mail.example.com'
sudo postconf -e 'mydomain = example.com'
sudo postconf -e 'myorigin = $mydomain'
sudo postconf -e 'inet_interfaces = all'
sudo postconf -e 'mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain'
sudo postconf -e 'mynetworks = 127.0.0.0/8 [::1]/128'
sudo systemctl restart postfix
echo "这是一封测试邮件" | mail -s "Postfix 测试" root
关键参数:mydestination 列出本机负责投递的域;mynetworks 定义可信中继网段,绝不要设为 0.0.0.0/0,否则会成为开放中继;inet_interfaces = all 让 25 端口对外监听。用 tail -f /var/log/mail.log 观察投递结果(RHEL 系日志在 /var/log/maillog)。
四、核心工作流
任务:搭建 Postfix + Dovecot 基础邮箱,使客户端可用 587 发信、993 收信
- DNS 准备:按
t_env设置 MX、A、PTR、SPF、DMARC;公钥 DKIM 记录待签名组件生成后再加。预期:dig +short MX example.com返回10 mail.example.com.。 - Postfix 基础配置:执行
t_quickstart的postconf -e命令;预期postfix check无输出。 - 安装 Dovecot 并启用 IMAP/POP3/LMTP:
sudo apt install -y dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd
sudo systemctl enable --now dovecot
/etc/dovecot/conf.d/10-mail.conf 中设置 mail_location = maildir:~/Maildir;10-ssl.conf 中设置 ssl = required 并填入证书路径。预期 ss -tlnp | grep -E ':(993|995)' 有监听。
4. 让 Postfix 用 Dovecot 做 SASL:在 /etc/postfix/main.cf 追加:
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
/etc/dovecot/conf.d/10-master.conf 中开放认证 socket:
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
- 在
/etc/postfix/master.cf启用提交端口:
submission inet n - y - - smtpd
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
smtps inet n - y - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
预期:systemctl reload postfix dovecot 后 ss -tlnp 可见 25、465、587、993、995。
6. 端到端验证:用 openssl s_client -starttls smtp -connect mail.example.com:587 应看到 220 ... ESMTP Postfix;用 openssl s_client -connect mail.example.com:993 应看到 * OK ... Dovecot ready;发一封测试信并查 /var/log/mail.log 与 Maildir。
五、进阶实践
- 虚拟域与虚拟用户:在
main.cf设置virtual_mailbox_domains = hash:/etc/postfix/vdomains、virtual_mailbox_maps = hash:/etc/postfix/vmailbox,编辑后执行sudo postmap /etc/postfix/vdomains /etc/postfix/vmailbox && sudo systemctl reload postfix。 - 限制外发中继:
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination,防止被用作垃圾邮件跳板。 - DKIM 签名:用 OpenDKIM 生成密钥并配置
Selector、KeyFile,在main.cf加smtpd_milters = inet:127.0.0.1:8891与non_smtpd_milters = inet:127.0.0.1:8891;DNS 添加selector1._domainkey.example.com的 TXT 记录(具体字段以 OpenDKIM 官方文档为准)。 - 队列排障:
postqueue -p查看积压,postqueue -f强制刷新,postsuper -d <QUEUE_ID>删除指定邮件;操作前务必确认队列 ID。 - 性能调优:
default_process_limit、smtpd_client_connection_count_limit、message_size_limit按硬件调整,改完用postconf复核。
六、常见问题
Q:GitHub 上的 vdukhovni/postfix 是官方仓库吗?
A:它是官方维护的 Git 镜像,便于社区协作;项目主站、发布公告与权威文档在 postfix.org,提交历史与版本请以主站为准。
Q:服务器成了开放中继怎么办?
A:检查 mynetworks 是否误设为大网段,并确认 smtpd_recipient_restrictions 含 reject_unauth_destination;改完 postfix reload,再用外部主机测试 relay 是否被拒。
Q:发出去的邮件总进垃圾箱? A:依次核对反向 DNS(PTR)、SPF、DKIM 签名、DMARC 对齐四项,任一缺失都会降权;同时确认发信 IP 未被列入黑名单。
Q:云服务器连不上 25 端口?
A:多数云厂商默认封禁 25 出站。申请解封,或把外发配置为经中继主机(relayhost);客户端提交建议走 587(STARTTLS)或 465(SMTPS)。
Q:Dovecot 认证失败,Postfix 日志报 SASL authentication failure?
A:确认 /var/spool/postfix/private/auth socket 存在、属主为 postfix:postfix、权限 0660,且 smtpd_sasl_path = private/auth 路径与之匹配。
七、学习资源
- 文档索引:https://www.postfix.org/documentation.html
- 基础配置 README:https://www.postfix.org/BASIC_CONFIGURATION_README.html
- SASL 与认证:https://www.postfix.org/SASL_README.html
postconf(5)参数手册:https://www.postfix.org/postconf.5.html- 官方 Git 镜像:https://github.com/vdukhovni/postfix