温馨提示×

Debian Postman与SMTP服务器如何配置

小樊
39
2025-11-29 00:34:44
栏目: 云计算

Debian 上 Postfix 与 SMTP 服务器配置指南

一 场景与目标

  • Debian 上以 Postfix 作为 SMTP 服务器,满足两类常见需求:
    1. 本机/内网应用调用本地 MTA 发信(本地投递或经外部中继)。
    2. 公网收信(需正确 DNS/MX 与访问控制),可选 Dovecot 提供 IMAP/POP3 收信。
  • 本文给出可直接落地的配置步骤、测试方法与安全要点。

二 安装与最小化配置

  • 安装软件
    • 仅本地发信:sudo apt update && sudo apt install postfix mailutils
    • 需要收信/IMAP/POP3:sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d
  • 基本 main.cf(示例域名与主机名请替换为你的实际值)
    • myhostname = mail.example.com
    • mydomain = example.com
    • myorigin = $mydomain
    • inet_interfaces = all(仅本机测试可先用 loopback-only
    • inet_protocols = ipv4
    • mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
    • mynetworks = 127.0.0.0/8 [::1]/128(仅本机中继;需要局域网发信可追加内网网段)
    • home_mailbox = Maildir/
    • smtpd_banner = $myhostname ESMTP
    • 若需本地用户认证提交:smtpd_sasl_auth_enable = yes;smtpd_sasl_type = dovecot;smtpd_sasl_path = private/auth;smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
  • 启动与开机自启
    • sudo systemctl restart postfix && sudo systemctl enable postfix
    • 若安装 Dovecot:sudo systemctl restart dovecot && sudo systemctl enable dovecot
    • 日志排查:tail -f /var/log/mail.log

三 测试与常见验证

  • 本机命令行发信
    • echo “Test email body” | mail -s “Test Subject” user@localhost
  • 手动 SMTP 会话(端口 25
    • telnet localhost 25
    • 交互示例:EHLO localhost → MAIL FROM: test@localhost → RCPT TO: user@localhost → DATA → Subject: test → 正文 → . → QUIT
  • 外部中继测试(可选,例如 Gmail
    • 在 main.cf 增加:
      • relayhost = [smtp.gmail.com]:587
      • smtp_sasl_auth_enable = yes
      • smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      • smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
      • smtp_use_tls = yes
    • 创建凭据文件 /etc/postfix/sasl_passwd:
      • [smtp.gmail.com]:587 your-email@gmail.com:your-app-password
    • 生成映射并保护权限:
      • sudo postmap /etc/postfix/sasl_passwd
      • sudo chmod 600 /etc/postfix/sasl_passwd*
    • 重启:sudo systemctl restart postfix
  • 端口与连通性
    • 检查监听:ss -tulnp | grep :25
    • 如需从外部访问,请开放防火墙端口(见第四节)并确认云厂商安全组策略。

四 公网发信与收信的 DNS 与安全

  • DNS 记录(示例)
    • A 记录:mail.example.com → 服务器公网IP
    • MX 记录:example.com → 10 mail.example.com
    • 验证:dig MX example.com;dig A mail.example.com
  • 加密与证书
    • 推荐启用 STARTTLS(端口 587)。可使用 Let’s Encrypt 获取证书并配置 Postfix(Certbot 支持 Postfix 插件)。
  • 发信信誉与合规
    • 建议配置 SPF:TXT 记录如 v=spf1 mx a ~all
    • 建议配置 DKIM:使用 opendkim 生成密钥并在 DNS 添加 selector._domainkey 记录
    • 按需启用 DMARC 策略,降低被判垃圾的概率
  • 防火墙与端口
    • 放行常用端口:sudo ufw allow 25/tcp;sudo ufw allow 587/tcp;如需明文 IMAP/POP3 可放行 143/tcp110/tcp(生产环境建议仅启用加密端口 993/995)。

五 常见问题与排错要点

  • 本机可发、外网不通
    • 检查云安全组/服务器防火墙是否放行 25/587;确认 mydestination 包含目标域;查看 /var/log/mail.log 的拒绝原因(如 relay_denied)。
  • 需要局域网/公网客户端提交邮件
    • inet_interfaces 设为 all,并在 mynetworks 中加入 192.168.0.0/16 等内网段;启用 SASL(Dovecot)并限制仅允许认证用户中继。
  • WSL 环境注意事项
    • WSL 默认仅 loopback 监听,需改为 inet_interfaces=all 并获取 WSL IP 后从 Windows 主机连接;WSL 关闭后服务停止,可借助开机自启脚本维持运行。
  • 使用外部 SMTP 中继(如 Gmail)
    • 必须使用 应用专用密码(或开启“安全性较低的应用访问”的旧方式,不推荐);确保 sasl_passwd 权限为 600 并已 postmap;证书链可用 ca-certificates.crt

0