去年好不容易摸索搞定的郵件伺服器,今年Q3又再度發生工作不正常的問題。
MAIL伺服器Dovecot + Postfix + Mailscanner安裝,紀錄&更新 https://jir.idv.tw/wordpress/?p=3124
POSTFIX郵件伺服器,架設Mailscanner可能會遇到OpenDKIM衝突的解決方法 https://jir.idv.tw/wordpress/?p=3146
因為郵件主機配合UBUNTU系統升級,但是陸陸續續發現功能服務有問題,也連帶發現升級後,開始一直收到YAHOO負責任的拒收信件。
今年開始大量利用AI互動來解決我一知半解、或是不熟稔的指令和功能追加。
這次經驗也真的要再來感嘆AI、讚嘆AI了…。
/etc/postfix/master.cf這個檔案。
目前運作方式是這樣:
#1.跑OpenDKIM的PORT: 8891
#2.跑OpenDMARC的.sock
#3.跑MailScanner的PORT: 33333
smtpd_milters = inet:localhost:8891, local:opendmarc/opendmarc.sock, inet:127.0.0.1:33333
non_smtpd_milters = $smtpd_milters
#1.跑OpenDKIM的PORT: 8891
指令可以這樣確認,只是令人生氣的服務狀態回饋根本看不出是有無異常(無任何故障訊息)。
sudo netstat -tlnp | grep 8891
(空值)
sudo systemctl status opendkim.service
● opendkim.service - LSB: Start the OpenDKIM service
Loaded: loaded (/etc/init.d/opendkim; generated)
Active: active (exited) since Tue 2025-08-05 08:36:19 CST; 28min ago
Docs: man:systemd-sysv-generator(8)
CPU: 5ms
8月 05 08:36:19 linux systemd[1]: Starting opendkim.service - LSB: Start the OpenDKIM service...
8月 05 08:36:19 linux systemd[1]: Started opendkim.service - LSB: Start the OpenDKIM service.
編輯OpenDKIM設定檔案,確定8891的PORT真的有跟postfix一樣定義和使用。
sudo nano /etc/default/opendkim
# default:
# SOCKET=local:$RUNDIR/opendkim.sock
# listen on loopback on port 12345:
SOCKET=inet:8891@localhost
我的設定檔本來是使用.SOCK,為了配合/etc/postfix/master.cf,修改對應用法和也讓未來瀏覽查修容易辨識。
修改成呼叫8891埠後,要記得重啟服務sudo systemctl restart opendkim.service。
這時候在檢查狀態,就會看到有相關的開啟PORT能用了。
netstat -tlnp | grep 8891
tcp 0 0 127.0.0.1:8891 LISTEN 274850/opendkim
#2.跑OpenDMARC的.sock
雖然有執行過已經有的狀態,啟動服務看起來沒有觸發任何反應和檔案。
sudo mkdir -p /var/spool/postfix/opendmarc
sudo chown opendmarc:opendmarc /var/spool/postfix/opendmarc
sudo chmod 755 /var/spool/postfix/opendmarc
檢查就會發現OpenDMARC的服務就是沒正常啟動,指令ps -ef | grep opendmarc也不會列出任何狀態。
所以定義檔的.PID檔案也就沒有存在,ls /var/run/opendmarc/opendmarc.pid。
試著檢查sudo grep opendmarc /var/log/mail.log會看到大量類似這樣的錯誤訊息:
…
2025-08-03T10:01:06.446707+08:00 linux postfix/smtpd[413815]: warning: connect to Milter service local:opendmarc/opendmarc.sock: No such file or directory
2025-08-03T10:09:18.402542+08:00 linux postfix/smtpd[414791]: warning: connect to Milter service local:opendmarc/opendmarc.sock: No such file or directory
…
確定一下升級後,是不是有仍安裝這個軟體,sudo apt-get install opendmarc。
因為服務啟動後,還是沒找到對應的檔案,所以試著看服務觸發的機制。
發現他不是讀設定檔和使用.sock檔案,所以要修改[Service]的內容作配合。
sudo nano /lib/systemd/system/opendmarc.service
[Unit]
Description=OpenDMARC Milter
Documentation=man:opendmarc(8) man:opendmarc.conf(5)
After=network-online.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/opendmarc/opendmarc.pid
#ExecStart=/usr/sbin/opendmarc
ExecStart=/usr/sbin/opendmarc -c /etc/opendmarc.conf -p local:/var/spool/postfix/opendmarc/opendmarc.sock
ExecReload=/bin/kill -USR1 $MAINPID
Restart=on-failure
User=opendmarc
[Install]
WantedBy=multi-user.target
存檔修改後,重新讀檔案和重新啟動服務,去看更改後的狀態。
sudo systemctl daemon-reload
sudo systemctl restart opendmarc.service
sudo systemctl status opendmarc.service
sudo ls -l /var/spool/postfix/opendmarc/opendmarc.sock
應該就會檢查到恢復正常運作了。
sudo ls -l /var/spool/postfix/opendmarc/opendmarc.sock
srwxrwxr-x 1 opendmarc opendmarc 0 8月 5 09:02 /var/spool/postfix/opendmarc/opendmarc.sock
而MailScanner本來就收信發信都正常,就當作33333埠是可以運作的。
或者可以下netstat -tlnp | grep 33333來確定狀態。
最後,/etc/postfix/main.cf 中,smtpd_milters 和 non_smtpd_milters 調整配合後的設定如下:
#1.跑OpenDKIM的PORT: 8891
#2.跑OpenDMARC的.sock
#3.跑MailScanner的PORT: 33333
smtpd_milters = inet:localhost:8891, local:opendmarc/opendmarc.sock, inet:127.0.0.1:33333
# 如果MailScanner仍是使用HOLD方式被動式去掃信件,那要改成這樣:
#smtpd_milters = inet:localhost:8891, local:opendmarc/opendmarc.sock
non_smtpd_milters = $smtpd_milters
把這幾個服務重啟,記得看一下status狀態。
sudo systemctl restart opendkim.service
sudo systemctl restart opendmarc.service
sudo systemctl restart mailscanner.service
sudo systemctl restart postfix.service
這幾個相關的服務重啟後,就可以再去dkimvalidator.com去測試驗證信件是不是有正確的封裝宣告進去。
像我這次第七次修改的檢查,終於看到DKIM的那行最重要的字串,有產生在信件封包裡面。這樣應該就不會再被其他信箱服務判斷異常和退信了。
Received: from …(… [000])
by relay-4.us-west-2.tx-prod (Postfix) with ESMTPS id …
for <000@dkimvalidator.com>; Tue, 5 Aug 2025 01:21:19 +0000 (UTC)
X-Spam-Status: No
X-yoursite-MailScanner-From: …
X-yoursite-MailScanner: Found to be clean
X-yoursite-MailScanner-ID: …
X-yoursite-MailScanner-Information: Please contact the ISP for more information
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=jir.idv.tw; s=2025;
t=…; bh=…=;
h=Date:From:To:Subject:In-Reply-To:References:From;
b=…=
Received: from linux (000)
by linux (Postfix) with ESMTPSA id …
for <000@dkimvalidator.com>; Tue, 5 Aug 2025 09:17:21 +0800 (CST)
MIME-Version: 1.0
Date: Tue, 05 Aug 2025 09:17:21 +0800
From: … <...>
To: 000@dkimvalidator.com
Subject: =?UTF-8?Q?Fwd=…=
In-Reply-To: <...>
References: <...>
Message-ID: <...>
X-Sender:
Content-Type: text/plain; charset=UTF-8;
format=flowed
Content-Transfer-Encoding: 8bit
8/05 -7TH除錯信件測試。