Debian上Postfix邮件发送限制实用配置
适用范围与前置说明
postfix check校验语法,再systemctl reload postfix平滑生效。常见限制与配置示例
按发件人或发件域拒绝外发
smtpd_sender_restrictions阶段使用check_sender_access映射表拒绝指定发件人或域的外发。# 启用检查
postconf -e "smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender_access, permit_sasl_authenticated, permit_mynetworks, reject"
# 编辑映射:deny 表示拒绝外发;OK 表示放行(可用于白名单)
cat >/etc/postfix/sender_access <<'EOF'
student@westos.com REJECT
@example.org REJECT
EOF
# 生成哈希表并重载
postmap /etc/postfix/sender_access
postfix check && systemctl reload postfix
仅允许本地域外发、禁止向指定域外发
restriction_class,对“仅本地域”的发件人,在收件侧限制其只能发给本地域;对外部域统一拒绝。# 定义限制类
postconf -e "smtpd_restriction_classes = local_only"
postconf -e "local_only = check_recipient_access hash:/etc/postfix/local_domains, reject"
# 指定哪些发件人使用该类(示例:只允许 @example.com 域内用户外发)
postconf -e "smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/local_senders, permit_sasl_authenticated, permit_mynetworks, reject"
# 本地域白名单(仅对这些域可外发)
cat >/etc/postfix/local_domains <<'EOF'
example.com OK
localhost OK
EOF
# 哪些发件人属于“仅本地域”策略
cat >/etc/postfix/local_senders <<'EOF'
@example.com local_only
EOF
# 生成哈希表并重载
postmap /etc/postfix/local_domains /etc/postfix/local_senders
postfix check && systemctl reload postfix
test.com用户尝试发给外部域(如163.com)时将被拒绝,返回5.7.1 Access denied。按收件域限制(拒收或仅允许特定域)
smtpd_recipient_restrictions阶段用check_recipient_access按收件域拒绝或放行。postconf -e "smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient_access, permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
cat >/etc/postfix/recipient_access <<'EOF'
example.net REJECT
*.example.org REJECT
EOF
postmap /etc/postfix/recipient_access
postfix check && systemctl reload postfix
OK实现仅允许列表。按认证与网段控制外发
postconf -e "smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination"
postfix check && systemctl reload postfix
mynetworks与SASL即可覆盖大多数场景。生效与测试
postfix check
systemctl reload postfix
nc -v mail.example.com 25
EHLO client.example.com
MAIL FROM: <someone@example.com>
RCPT TO: <target@example.org>
观察返回码:被限制时通常返回5.7.1(Access denied)等拒信码。进阶与注意事项
DUNNO可保持策略可组合性。main.cf或映射表后,务必执行postfix check与reload,并保留旧配置以便快速回滚。