温馨提示×

如何监控ubuntu邮件服务

小樊
52
2026-10-04 23:44:02
栏目: 智能运维

在 Ubuntu 上“监控邮件服务”通常取决于你使用的是哪种邮件服务(如 Postfix、Exim、Sendmail、Dovecot、或云邮箱中继)。下面我按常见场景 + 实用方法给你一个较完整的指南。


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

ps aux | grep -E 'postfix|exim|sendmail|dovecot'

或

systemctl list-units --type=service | grep -E 'mail|postfix|exim|dovecot'

常见组合:

  • 发信:Postfix / Exim / Sendmail
  • 收信:Dovecot (IMAP/POP3)

二、基础监控(命令行)

1. 查看服务状态

systemctl status postfix
systemctl status dovecot

开机自启检查:

systemctl is-enabled postfix

2. 查看邮件队列

mailq
# 或
postqueue -p

清空队列(谨慎):

postsuper -d ALL

3. 实时查看邮件日志

Ubuntu 通常使用 rsyslog + journald

tail -f /var/log/mail.log

或

journalctl -u postfix -f

常见日志位置:

  • /var/log/mail.log
  • /var/log/mail.err

4. 检查端口监听

ss -lntp | grep -E '25|465|587|110|143|993|995'

三、主动监控(告警级别)

方案 1:使用 Monit(推荐,轻量)

安装:

sudo apt install monit

示例 /etc/monit/conf.d/mail:

check process postfix with pidfile /var/spool/postfix/pid/master.pid
    start program = "/usr/sbin/service postfix start"
    stop program  = "/usr/sbin/service postfix stop"
    if failed port 25 protocol smtp then alert

启动:

systemctl restart monit

方案 2:使用 systemd 自带监控

systemctl enable --now postfix

自动重启:

[Service]
Restart=always

方案 3:Zabbix / Prometheus(企业级)

  • Zabbix:使用 SMTP Template
  • Prometheus + Blackbox Exporter:
modules:
  smtp:
    prober: tcp
    tcp:
      query_response:
        - expect: "220"

四、邮件内容 & 发送成功监控

1. 检查是否真的发出去

grep 'status=sent' /var/log/mail.log
grep 'status=bounced' /var/log/mail.log

2. 测试发信

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

五、安全 & 异常监控(很重要)

1. 防止被当成垃圾邮件

  • 配置 SPF / DKIM / DMARC
  • 监控:
grep 'relay' /var/log/mail.log

2. 检测暴力破解(Dovecot)

grep 'auth failed' /var/log/mail.log

可配合 fail2ban:

apt install fail2ban

六、图形化方案(可选)

  • Mailgraph(Postfix 流量图)
apt install mailgraph

访问:

http://server/mailgraph

七、总结推荐组合

需求 推荐
个人/小服务器 tail + mailq + Monit
企业 Zabbix / Prometheus
安全 fail2ban + 日志监控

如果你愿意,可以告诉我:

  • 你用的是 Postfix / Exim / 其他?
  • 是 只发信 / 只收信 / 都有?
  • 是否要 微信/邮件告警?

我可以直接帮你写 监控脚本或配置文件。

0 踩