﻿# 設定 Postfix + Postgrey 有效阻擋非法的垃圾信來源

延續昨天的文章主題，透過網友 Willie 的推薦，我決定替我的 [Postfix](http://www.postfix.org/) 加上 [Postgrey](http://postgrey.schweikert.ch/) 機制，Postgrey 的運作機制是讓 SMTP 收到郵件後先回應 450 (Requested mail action not taken: mailbox unavailable) 給來源 SMTP 伺服器，若來源伺服器是標準的 SMTP 伺服器就會格一段時間自動重試，但如果對方是用發垃圾信的工具，就不會將信件重發一次 (因為他正忙著發上千萬封的垃圾郵件)，也就達到有效阻擋垃圾郵件的目的。

在 Ubuntu 安裝 Postgrey 是在簡單不過的事，三個步驟就可完成安裝設定：

1\. 先利用 apt-get install postgrey 安裝好，然後再看一下 /etc/default/postgrey 的設定 (基本上不用改)

2\. 設定 Postfix 的 /etc/postfix/main.cf 設定檔

```
smtpd_recipient_restrictions =
    permit_mynetworks
    permit_sasl_authenticated
    reject_unauth_destination
    check_policy_service inet:127.0.0.1:60000
```

3\. 重新載入 Postfix 設定

```
/etc/init.d/postfix reload
```

註: 預設 postgrey 是透過 daemon 運作的，預設會 LISTEN 本機網路(localhost) 的 Port 60000

\---

不過我還有壹台 **Fedora Core 3** 的主機實在太舊了，只好從官網下載套件回來安裝，不過礙於說明文件太少，只好自己嘗試，以下是我整理的安裝步驟：

1\. 安裝主機所缺少的 Perl 模組 ( 我機器剛好缺少 IO::Multiplex 模組 )

```
perl -e shell -MCPAN
```

安裝示意圖如下：

   ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/c69f5d5b04c948ffec64-image.png) 

2\. 建立 postgrey 專用的使用者與群組

```
useradd -r -g postgrey postgrey
```

3\. 下載程式並解壓縮

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/95b95c3f92773a476646-image.png)

4\. 將 postgrey 主程式複製到 /usr/sbin/ 目錄下 ( 註: postgrey 是用 Perl 寫的 )

```
cp postgrey /usr/sbin/
```

5\. 將預設白名單複製到 /etc/postfix/ 目錄下 ( 兩個檔案 )

```
cp postgrey_whitelist_* /etc/postfix/
```

6\. 建立 /var/spool/postfix/postgrey 目錄

```
mkdir /var/spool/postfix/postgrey
chown postgrey /var/spool/postfix/postgrey
```

7\. 啟動 postgrey 服務，並將以下指令加入 **/etc/rc.local** 檔案讓該服務可以在重開機時自動啟動

```
/usr/sbin/postgrey --pidfile=/var/run/postgrey.pid -d -i 60000 --user=postgrey --group=postgrey
```

8\. 設定 Postfix 的 **/etc/postfix/main.cf** 設定檔

```
smtpd_recipient_restrictions =    permit_mynetworks    permit_sasl_authenticated    reject_unauth_destination    check_policy_service inet:127.0.0.1:60000
```

9\. 重新載入 Postfix 設定

```
/etc/init.d/postfix reload
```

\---

最後我們還是習慣檢查一下系統記錄，確保一切運作正常：

```
tail -f /var/log/maillog
```

\---

另外也可設定 [PSMon](http://www.psmon.com) 監控該服務是否正常運作

```xml
<Process postgrey>
    spawncmd    /usr/sbin/postgrey --pidfile=/var/run/postgrey.pid -d --inet=60000 --user=postgrey --group=postgrey
    pidfile     /var/run/postgrey.pid
    killcmd     killall postgrey
</Process>
```

**補充說明**

由於 Postgrey 預設拒收郵件的時間是 5 分鐘 ( 300 秒 )，如果有需要縮短暫時拒收的時間可以調整服務的啟動參數，加上 \--dealy=秒數 即可，例如：

```
/usr/sbin/postgrey --pidfile=/var/run/postgrey.pid     -d -i 60000 --user=postgrey --group=postgrey --delay=120
```

**成果效益**

我今天啟用 [Postgrey](http://postgrey.schweikert.ch/) 之後，一天的垃圾信量已經低於 30 封，真是跟之前天差地遠阿！ ^\_^

**相關連結**

-   [Postgrey - Postfix Greylisting Policy Server](http://postgrey.schweikert.ch/)
-   [Greylisting: The Next Step in the Spam Control War](http://projects.puremagic.com/greylisting/)
-   [Postfix SMTP Access Policy Delegation](http://www.postfix.org/SMTPD_POLICY_README.html)

-   SMTP Reply Codes

-   [Postfix 加上 DNSBL, Postgrey 防 Spam](http://plog.longwin.com.tw/my_note-unix/2006/12/23/postfix_spam_2006)
-   [Postgrey 安裝筆記](http://dz.adj.idv.tw/archiver/tid-162.html)
