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'[email protected] [email protected]# 生成哈希表并重载postmap /etc/postfix/sender_accesspostfix 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.comOKlocalhostOKEOF# 哪些发件人属于“仅本地域”策略cat >/etc/postfix/local_senders <<'EOF'@example.comlocal_onlyEOF# 生成哈希表并重载postmap /etc/postfix/local_domains /etc/postfix/local_senderspostfix check && systemctl reload postfixtest.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 REJECTEOFpostmap /etc/postfix/recipient_accesspostfix check && systemctl reload postfixOK实现仅允许列表。按认证与网段控制外发
postconf -e "smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination"postfix check && systemctl reload postfixmynetworks与SASL即可覆盖大多数场景。生效与测试
postfix checksystemctl reload postfixnc -v mail.example.com 25EHLO client.example.comMAIL FROM: <[email protected]>RCPT TO: <[email protected]>观察返回码:被限制时通常返回5.7.1(Access denied)等拒信码。进阶与注意事项
DUNNO可保持策略可组合性。main.cf或映射表后,务必执行postfix check与reload,并保留旧配置以便快速回滚。