Linux 邮件服务器 Postfix 配置方法

1次阅读

postfix 启动失败因无本地接口,需启用 lo 回环、补全 /etc/hosts 映射或设 inet_Interfaces = loopback-only;接收本地邮件需正确配置 mydestination 和 smtpd_recipient_restrictions;smtp 认证失败多因 sasl 服务未运行或路径权限错误;发信慢因 25 端口被封,应改用 tls 中继(如 gmail 587 端口)并执行 postmap 刷新。

Linux 邮件服务器 Postfix 配置方法

Postfix 启动失败,systemctl status postfix 显示 fatal: parameter inet_interfaces: no local interface found for 127.0.0.1

这是 Postfix 在没有可用网络接口时的典型报错,常见于最小化安装的 linux(比如某些云服务器镜像)未启用 lo 回环接口,或 /etc/hosts 缺少 127.0.0.1 localhost 映射。

实操建议:

  • 先运行 ip addr show lo 确认回环接口是否 up;若为 down,执行 sudo ip link set lo up
  • 检查 /etc/hosts 是否含 127.0.0.1 localhost,缺就补上(别用 ::1 冲突 IPv6 配置)
  • 临时绕过:在 /etc/postfix/main.cf 中设 inet_interfaces = loopback-only,避免 Postfix 主动探测所有接口
  • 改完务必运行 sudo postfix check 校验语法,再 sudo systemctl restart postfix

想让 Postfix 接收本机其他程序发来的邮件(比如 cron、logwatch),但被拒收

默认 Postfix 只监听本地回环(inet_interfaces = loopback-only),但“接收”和“投递”是两回事——关键看 mydestinationsmtpd_recipient_restrictions

实操建议:

  • mydestination 必须包含本机域名,例如:mydestination = $myhostname, localhost.$mydomain, localhost, example.com
  • 确保 smtpd_recipient_restrictions 包含 permit_mynetworks(否则本地网段发信会被拦)
  • 如果程序用 sendmail -tmail 命令发信,Postfix 的 sendmail_path 要指向正确路径(通常是 /usr/sbin/sendmail.postfix),检查 alternatives --config sendmail
  • 测试命令:echo "test" | mail -s "local test" root,再查 tail -f /var/log/maillog

配置 SMTP 认证后,telnet localhost 25 能连上,但 swaks --to user@example.com --from me@localhost --server localhost535 5.7.8 Error: authentication failed

Postfix 本身不处理 SASL 认证,必须依赖 saslauthddovecot-sasl。错误不是密码错,而是认证服务没跑、没连上、或机制不匹配。

实操建议:

  • 确认 saslauthd 已启动:sudo systemctl status saslauthd;若用 Dovecot,启 dovecot 并确认 auth_mechanisms = plain login/etc/dovecot/conf.d/10-auth.conf
  • Postfix 的 /etc/postfix/main.cf 中必须有:smtpd_sasl_type = dovecot(或 saslauthd),且 smtpd_sasl_path = private/auth(Dovecot)或 smtpd_sasl_path = smtpd(saslauthd)
  • 权限陷阱:Postfix 进程用户(postfix)必须对 smtpd_sasl_path 指向的 socket 文件有读写权,常见于 /var/spool/postfix/private/auth 所有者不是 postfix:dovecot
  • 调试命令:testsaslauthd -u username -p password(验证底层认证),再 postconf -n | grep sasl 检查配置是否生效

Postfix 发信慢,mailq 里积压多,日志反复出现 connect to gmail-smtp-in.l.google.com[142.250.191.69]:25: Connection timed out

这不是 Postfix 问题,是多数家用宽带和云厂商(阿里云、腾讯云、AWS)默认封禁 25 端口出站——尤其针对新注册 IP。Gmail、outlook 等大厂 SMTP 入口会直接拒绝连接。

实操建议:

  • 不要硬刚 25 端口:改用 smtp_tls_security_level = encrypt + smtp_sasl_auth_enable = yes,走 smtp.gmail.com:587smtp.office365.com:587
  • /etc/postfix/main.cf 加中继配置:relayhost = [smtp.gmail.com]:587,并配好 /etc/postfix/sasl_passwd(格式:[smtp.gmail.com]:587 user@gmail.com:app_password
  • 注意 Gmail 要用「应用专用密码」而非账户密码,且需开启两步验证;Office365 要确认租户允许 SMTP AUTH
  • 强制刷新:运行 sudo postmap /etc/postfix/sasl_passwd,再 sudo systemctl reload postfix

最常被忽略的是:以为配了 relayhost 就万事大吉,却忘了 postmap 没执行,或者 sasl_passwd.db 权限是 644(必须 600)导致 Postfix 拒绝读取。

text=ZqhQzanResources