温馨提示×

如何调试ubuntu邮件服务

小樊
47
2026-10-04 23:58:00
栏目: 智能运维

调试 Ubuntu 邮件服务(通常指 Postfix / Sendmail / Exim / Dovecot 等)可以按下面系统化的思路来做。下面以最常见的 Postfix + Dovecot 为例,兼顾通用方法。


一、先确认你用的是哪种邮件服务

systemctl status postfix
systemctl status exim4
systemctl status sendmail
systemctl status dovecot

常见组合:

  • Postfix:MTA(发信)
  • Dovecot:IMAP/POP3(收信)
  • Exim:Debian/Ubuntu 默认有时是 Exim

二、基础检查(必做)

1. 服务是否运行

systemctl status postfix
systemctl status dovecot

没运行就启动:

sudo systemctl start postfix
sudo systemctl enable postfix

2. 查看监听端口

sudo netstat -tunlp | grep -E '25|110|143|465|587|993'

常见端口:

  • 25:SMTP
  • 465:SMTPS
  • 587:Submission
  • 110:POP3
  • 143:IMAP
  • 993:IMAPS

三、查看日志(最重要)

Postfix 日志

sudo tail -f /var/log/mail.log
sudo journalctl -u postfix -f

Dovecot 日志

sudo tail -f /var/log/mail.log
sudo journalctl -u dovecot -f

发一封测试邮件,然后立刻看日志:

echo "test" | mail -s "test" user@example.com

四、测试邮件发送

1. 本地发送

mail -s "hello" user@localhost

2. 命令行 SMTP 测试

telnet localhost 25

示例:

HELO localhost
MAIL FROM:<test@localhost>
RCPT TO:<user@localhost>
DATA
Subject: test
hello
.
QUIT

五、常见故障与排查

1. 邮件发不出去

检查:

  • DNS 是否正常
nslookup example.com
  • 是否被防火墙拦
sudo ufw status
  • 是否缺少 MX 记录(对外发信)

2. 邮件在队列里

mailq
postqueue -p

清空队列(谨慎):

postsuper -d ALL

3. 认证失败(Dovecot)

检查:

sudo dovecot -n

常见错误:

  • 密码文件权限不对
  • 用户名格式错误(是否带域名)

测试登录:

telnet localhost 143

4. 配置文件语法检查

postfix check
postconf -n

六、调试高级技巧

开启详细日志

编辑:

sudo nano /etc/postfix/main.cf

加入:

debug_peer_level = 3

重启:

sudo systemctl restart postfix

使用 swaks 测试

swaks --to user@example.com --from test@localhost --server localhost

七、如果你愿意,我可以更进一步

你可以告诉我:

  1. 你用的是 Postfix / Exim / Sendmail?
  2. 是本机发信还是对外发信?
  3. 报错日志(直接贴 /var/log/mail.log)

我可以帮你逐行分析日志并定位问题。

0 踩