温馨提示×

如何设置Ubuntu邮件服务器

小樊
40
2025-12-26 16:37:18
栏目: 云计算

在 Ubuntu 22.04/24.04 上搭建可用的邮件服务器

一 准备与规划

  • 组件选型:使用 Postfix 作为 MTA(负责邮件传输与投递策略),Dovecot 作为 MDA/LDA 并提供 IMAP/POP3 访问。
  • 前提条件:
    • 拥有可管理的域名(如:example.com)与服务器的 静态公网 IPv4
    • 设置服务器主机名为 FQDN(如:mail.example.com),并确保命令 hostname -f 能正确返回该值。
    • 在云厂商控制台与服务器防火墙放行必要端口(见下文)。
  • 关键 DNS 记录(示例):
    • A 记录mail.example.com → 服务器公网 IP
    • MX 记录example.com → 优先级 10 mail.example.com.
    • SPF 记录example.com"v=spf1 mx -all"
    • 建议后续补充 DKIMDMARC 以提升到达率与防伪能力。
  • 端口策略(入站):
    • 25/TCP(MTA 间通信,服务器↔服务器)
    • 587/TCP(Submission,客户端发信,推荐 STARTTLS)
    • 143/TCP(IMAP,STARTTLS)与 993/TCP(IMAPS)
    • (可选)110/TCP(POP3)与 995/TCP(POP3S)
    • 注意:许多云厂商默认限制或封禁 25/TCP 出站,需工单申请开通;客户端发信请优先使用 587

二 安装与基础配置 Postfix

  • 安装与基础交互式配置:
    • 安装:sudo apt update && sudo apt install postfix -y
    • 安装时选择:Internet Site;系统邮件名称填 example.com(而非 mail.example.com);Root/postmaster 收件人填你的管理员账户(如 sammy)。
  • 关键参数(/etc/postfix/main.cf):
    • myhostname = mail.example.com
    • mydomain = example.com
    • myorigin = $mydomain
    • inet_interfaces = all
    • inet_protocols = ipv4(或 all)
    • mydestination = $myhostname, localhost.$mydomain, $mydomain
    • mynetworks = 127.0.0.0/8 [::1]/128
    • home_mailbox = Maildir/
  • 应用与验证:
    • 使配置生效:sudo systemctl restart postfix && sudo systemctl enable postfix
    • 本地 SMTP 测试(示例):
      • telnet localhost 25 → 输入 EHLO example.com,应看到 250-STARTTLS 等扩展
      • 简单收发测试(在本地用户间)可验证投递是否正常。

三 安装与配置 Dovecot(IMAP/POP3 与投递)

  • 安装组件:
    • sudo apt install dovecot-core dovecot-imapd dovecot-lmtpd -y
  • 邮件存储与协议:
    • /etc/dovecot/conf.d/10-mail.confmail_location = maildir:~/Maildir
    • /etc/dovecot/dovecot.conf(或 10-master.conf 的 protocols 段):启用 imap lmtp
  • SASL 与 Postfix 对接(Dovecot 提供 SASL):
    • /etc/dovecot/conf.d/10-auth.confauth_mechanisms = plain login
    • /etc/dovecot/conf.d/10-master.conf:在 service auth 中创建 /var/spool/postfix/private/auth 的 unix listener(权限 0666,属主 postfix:postfix
    • Postfix 启用 SASL(/etc/postfix/main.cf):
      • smtpd_sasl_auth_enable = yes
      • smtpd_sasl_type = dovecot
      • smtpd_sasl_path = private/auth
      • smtpd_sasl_security_options = noanonymous
      • smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
  • LMTP 投递(推荐):
    • /etc/postfix/main.cf:virtual_transport = lmtp:unix:private/dovecot-lmtp
    • /etc/dovecot/conf.d/10-master.conf:在 service lmtp 中创建 /var/spool/postfix/private/dovecot-lmtp(权限 0600,属主 postfix:postfix
  • 应用与验证:
    • sudo systemctl restart postfix dovecot && sudo systemctl enable dovecot
    • 本地 IMAP 测试:telnet localhost 143a login 用户名 密码a LIST "" "*"

四 安全与进阶配置

  • TLS 加密(证书建议用 Let’s Encrypt):
    • Postfix(/etc/postfix/main.cf):
      • smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
      • smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
      • smtpd_use_tls = yes
      • smtp_tls_security_level = encrypt
      • smtp_tls_note_starttls_offer = yes
    • Dovecot(/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
  • 防火墙与云安全组:
    • UFW:sudo ufw allow 25,587,143,993/tcp(按需增减)
    • 云控制台:放行相同端口的入站规则,并申请开通 25/TCP 出站(如需对外发信)。
  • 反垃圾与可用性:
    • 部署 Fail2ban 防止暴力破解
    • 配置 PTR(反向 DNS)与 SPF;生产环境建议增加 DKIM/DMARC 与合适的 MTA-STS/DANE 策略。

五 快速测试与常见问题

  • 发信测试(Submission,端口 587):
    • echo "主题:测试" | s-nail -s "测试邮件" -r from@you.com -S smtp=submission:587 -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user=you@you.com -S smtp-auth-password=密码 recipient@example.com
  • 收信测试(IMAPS,端口 993):
    • s-nail -s "INBOX 测试" -S imaps -S ssl-verify=ignore recipient@example.com
  • 常见问题排查:
    • 域名与主机名不一致、缺少或错误的 MX/SPF、未配置 TLS、云厂商限制 25/TCP、客户端未使用 STARTTLS/SSL、Dovecot 与 Postfix 的 SASL/LMTP 对接参数不匹配等,都会导致投递或认证失败。
  • 生产提醒:
    • 本方案为基础可用配置,面向生产需进一步强化:启用 DKIM/DMARC、配置 灰名单/速率限制、日志与监控、备份与灾备、定期安全更新与合规审计。

0