2020年8月18日 星期二

Postfix + SPF + DKIM

現在自架的 Mail Server 若沒有簽章,寄出的信件有很大的機會被擋或歸類在垃圾信
所以 SPF + DKIM 已經是必要的設定
環境 CentOS + Postfix,文中的域名均以 yourdomain.com 作為範例

安裝所需套件
yum -y install pypolicyd-spf opendkim

在你的域名的 DNS 設定加一筆 TXT 指到 "v=spf1 a mx ~all"
這個設定已經可以符合大部份的狀況,若需要進階的設定,可以搜尋 SPF record generator

產生 DKIM Key
opendkim-genkey -t -b 2048 -d yourdomain.com

執行完後,會在產生兩個檔
default.private 私鑰
default.txt 需設定到域名 TXT 的資訊
把這兩個檔搬到 /etc/opendkim/keys 目錄
mv default.private default.txt /etc/opendkim/keys

在域名的 DNS 設定,新增一筆 default._domainkey TXT 內容是上面那個 default.txt 括號裡面的內容

編輯 /etc/opendkim.conf

Mode    sv

Uncomment 這幾行
KeyTable      /etc/opendkim/KeyTable
SigningTable  refile:/etc/opendkim/SigningTable
ExternalIgnoreList    refile:/etc/opendkim/TrustedHosts
InternalHosts refile:/etc/opendkim/TrustedHosts

編輯 /etc/opendkim/KeyTable
default._domainkey.yourdomain.com yourdomain.com:default:/etc/opendkim/keys/default.private

編輯 /etc/opendkim/SigningTable
*@yourdomain.com default._domainkey.yourdomain.com

編輯 /etc/opendkim/TrustedHosts 預設的 localhost IPv4、IPv6 需保留,再填上主機的 Public IP 及 Hostname
127.0.0.1
::1
Your_Public_IP
yourdomain.com

建立 policyd-spf user
useradd policyd-spf -s /bin/false

最後調整 postfix 設定
/etc/postfix/master.cf 加入
policyd-spf  unix  -       n       n       -       0       spawn
    user=policyd-spf argv=/usr/libexec/postfix/policyd-spf

/etc/postfix/main.cf 加入
policyd-spf_time_limit = 3600
smtpd_recipient_restrictions =
   permit_mynetworks,
   permit_sasl_authenticated,
   reject_unauth_destination,
   check_policy_service unix:private/policyd-spf

# OpenDKIM
milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = $smtpd_milters

寄一封測試信到 Gmail,若沒有問題的話,收件方的 Mail 可以看到類似以下 Header
Authentication-Results: yourdomain.com;
        dkim=pass (2048-bit key) header.d=yourdomain.com header.i=@yourdomain.com header.b="WsyNs1qw"
        spf=pass (google.com: domain of root@yourdomain.com designates 167.xx.xx.xx as permitted sender) smtp.mailfrom=root@yourdomain.com
Received-SPF: pass
DKIM-Filter: OpenDKIM Filter
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yourdomain.com;

1 則留言:

Unknown 提到...

感謝 您的分享, 對我產生莫大幫助!

RHEL 9 install xrdp

xrdp 是 Linux 版的 RDP server (遠端桌面) 它運作時需要 Xvnc 的 libvnc.so, 所以安裝時需要連同 VNC 一起安裝(只需 minimal) dnf -y install xrdp tigervnc-server-minimal system...