邮件头出现:Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id D7D69A078D
Operating System
Linux
OS Version
debian12
System Architecture
x86_64
Docker Version
Docker version 28.4.0, build d8eb465
Docker Compose Version
No response
Reproducible in Latest Version?
Yes, occurs in the latest stable release
Reproduction Steps
关于 billionmail/postfix 镜像 Received 头出现 localhost 问题的深度分析报告
1. 问题概述 (Summary)
在使用 billionmail/postfix Docker 镜像时,通过 587 端口(ESMTPSA)提交的邮件,其 Received 头部信息被错误地记录为:
Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon)
这个问题导致无法正确追踪邮件的原始客户端 IP,并且服务器标识不正确,可能影响邮件的可信度。
2. 关键排查证据 (Key Evidence)
经过一系列详尽的排查,我们确认了以下核心事实:
-
宿主机和 Docker 网络工作正常:通过
tcpdump在宿主机上抓包,我们已证实,带有真实客户端公网 IP 的网络数据包,被正确无误地、且保留了原始源 IP 的情况下,转发到了postfix-billionmail容器的虚拟网络接口上。这完全排除了宿主机网络、防火墙(UFW)、Docker 网络本身是问题根源的可能性。 -
问题根源在容器内部:既然网络层是完美的,那么问题必定出在容器内部的应用层或容器自身的网络命名空间中。
3. 源代码分析与根本原因 (Source Code Analysis & Root Cause)
通过分析 billionmail/postfix 镜像的 GitHub 源代码 (https://github.com/aaPanel/BillionMail/tree/dev/Dockerfiles/postfix),我们找到了导致此问题的两个独立的、被有意设计的机制,它们的叠加导致了当前的结果。
根本原因一:容器内部的 iptables REDIRECT 规则导致 from [127.0.0.1]
镜像的启动脚本 /run.sh 中包含以下代码:
# 强制走 127.0.0.1
iptables -t nat -A PREROUTING -p tcp --dport 25 -j REDIRECT --to-port 25
iptables -t nat -A PREROUTING -p tcp --dport 465 -j REDIRECT --to-port 465
iptables -t nat -A PREROUTING -p tcp --dport 587 -j REDIRECT --to-port 587
- 行为分析:该脚本在容器启动时,在容器内部添加了
iptables规则。-j REDIRECT的作用是将所有进入容器 25/465/587 端口的流量,强行劫持并重定向到容器自己的127.0.0.1上的对应端口。 - 直接后果:这个重定向过程会不可逆地将网络连接的原始客户端源 IP 地址替换为
127.0.0.1。这就是邮件头中出现from [127.0.0.1] (localhost)的直接原因。
根本原因二:Postfix 服务的 chroot 沙箱模式导致 by localhost
镜像源代码中的 conf/master.cf 文件为 submission (587端口) 服务进行了如下定义:
# service type private unpriv chroot wakeup maxproc command + args
submission inet n - y - - smtpd
- 行为分析:第四列的
y(yes) 表示 `submission
Observed Behavior
Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id D7D69A078D
Expected Behavior
Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id D7D69A078D
Supplemental Information
不是BUG,故意改的,你去查看postfix头信息配置
就是为了不泄露smtp的地址啊
就是为了不泄露smtp的地址啊
Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id D7D69A078D
这段对于用户也没啥关键信息吧,如果你在意你可以手动删掉