天天看點

RHCE教育訓練筆記——Postfix

在RHEL 6中,Postfix預設已安裝并随機啟動。有兩個檔案很重要,如下:

主配檔案 /etc/postfix/main.cf

程序檔案 /etc/postfix/master.cf

在實際操作中,修改主配檔案,主要涉及到的幾個參數是(前面的數字是初始時的行号):

75 #myhostname = host.domain.tld

注:myhostname 參數指定運作postfix郵件系統的主機的主機名。預設地,該值被設定為本地機器名。可以指定該值,需要注意的是,要指定完整的主機名。如:myhostname = zero.domain.com

83 #mydomain = domain.tld

注:mydomain參數指定域名,預設地,postfix将myhostname的第一部分删除而作為mydomain的值。

99 #myorigin = $mydomain

注:myorigin參數指明發件人所在的域名。如果使用者的郵件位址為[email protected],則該參數指定@後面的域名。預設地,postfix使用本地主機名作為myorigin,但是建議最好使用域名,因為這樣更具有可讀性。比如:安裝postfix的主機為zero.domain.com則我們可以這樣指定myorigin: myorigin = domain.com。當然也可以引用其他參數,如:myorigin = $mydomain。

116 inet_interfaces = localhost

注:inet_interfaces 參數指定postfix系統監聽的網絡接口。預設地,postfix監聽所有的網絡接口。如果postfix運作在一個虛拟的ip位址上,則必須指定其監聽的位址。如:inet_interfaces = all, inet_interface = 192.168.1.1

164 mydestination = $myhostname, localhost.$mydomain, localhost

注:mydestination參數指定postfix接收郵件時收件人的域名,換句話說,也就是postfix系統要接收什麼樣的郵件。比如:使用者的郵件位址為[email protected],也就是域為domain.com,則你就需要接收所有收件人為[email protected]的郵件。與myorigin一樣,預設地,postfix使用本地主機名作為mydestination。如:mydestination = $mydomain,mydestination = domain.com

264 #mynetworks = 168.100.189.0/28, 127.0.0.0/8

mynetworks 參數指定所在的網絡的網絡位址,postfix系統根據其值來差別使用者是遠端的還是本地的,如果是本地網絡使用者則允許其通路。可以用标準的A、B、C類網絡位址,也可以用CIDR(無類域間路由)位址來表示,如:192.168.1.0/24, 192.168.1.0/26

546 #header_checks = regexp:/etc/postfix/header_checks

注:在postfix中,通過header_checks參數限制接收郵件的信頭的格式,如果符合指定的格式,則拒絕接收該郵件。可以指定一個或多個查詢清單,如果新郵件的信頭符合清單中的某一項則拒絕該接收郵件。

一、準備工作

本實驗平台為Centos 6.2。

先确定主機名是帶域名的形式。如果不帶,請使用hostname指令更名,并修改/etc/sysconfig/network檔案中HOSTNAME的值。

檢視主機名

[root@zero ~]# hostname

zero.sq.com

二、去掉郵件位址中的主機頭

1.在不修改任何配置的情況下,管理者root向普通使用者sqing發郵件test01。

[root@zero ~]# mail sqing

Subject: test01

00000000000001111111111111111

.

EOT

在另一個終端使用sqing登入檢視

[sqing@zero ~]$ mail

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/sqing": 1 message 1 new

>N  1 root                  Wed May 22 02:41  18/560   "test01"

& 1

Message  1:

From [email protected]  Wed May 22 02:41:49 2013

Return-Path: <[email protected]>

X-Original-To: sqing

Delivered-To: [email protected]

Date: Wed, 22 May 2013 02:41:49 +0800

To: [email protected]

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

& q

可以看到,此時郵件的發件人和收件人的郵件位址,域名部分顯示的是主機頭加域名,如[email protected],但在實際中,一船是不帶主機名的,隻使用主機所在域的域名,如[email protected]

2.為了達到這個效果,需要進行以下修改,

将mydomain和myorigin開啟,并将mydomain的值改為sq.com。

同時,還要修改,mydestination的值,否則,郵件發出後,對方收不到,而自己這邊又沒有退信郵件。

[root@zero ~]# cat /etc/postfix/main.cf

1

2

3

4

5

<code>……其他内容略……</code>

<code>83 mydomain = sq.com  </code><code>#改為主機所在的域,不加主機頭</code>

<code>99 myorigin = $mydomain  </code><code>#改為本地域名</code>

<code>116 inet_interfaces = all  </code><code>#改為監聽all,本實驗環境是在locahost上操作,可以不改</code>

<code>164 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain  </code><code>#增加本地域名</code>

重新開機服務,restart或reload

[root@zero ~]# service postfix restart

Shutting down postfix:                                     [  OK  ]

Starting postfix:                                          [  OK  ]

再測試

管理者root向普通使用者sqing發郵件test02。

Subject: test02

000000000000000022222222222222222

在root使用者登入的終端檢視sqing的郵件

[root@zero ~]# mail –u sqing

"/var/spool/mail/sqing": 2 messages 1 new

   1 root                  Wed May 22 02:41  19/571   "test01"

&gt;N  2 root                  Wed May 22 03:03  18/539   "test02"

&amp; 2

Message  2:

From [email protected]  Wed May 22 03:03:20 2013

Return-Path: &lt;[email protected]&gt;

Delivered-To: [email protected]

Date: Wed, 22 May 2013 03:03:20 +0800

To: [email protected]

From: [email protected] (root)

三、别名

修改/etc/aliases檔案,在末行添加以下内容:

<code>sq:     sqing</code>

(注,格式為 使用者别名:使用者實名)

儲存修改後,執行newaliases指令,newaliases 的功能傳是将 /etc/aliases 轉換成一個 postfix(還有sendmai)l 所能了解的資料庫。也可以不執行newaliases指令,但需重新開機postfix服務。

[root@zero ~]# newaliases

root發測試郵件,

[root@zero ~]# mail sq

Subject: test03

0000000000000000000333333333333333333333

檢視sqing郵件,

"/var/spool/mail/sqing": 3 messages 1 new

   2 root                  Wed May 22 03:03  19/550   "test02"

&gt;N  3 root                  Wed May 22 03:15  18/537   "test03"

&amp; 3

Message  3:

From [email protected]  Wed May 22 03:15:56 2013

X-Original-To: sq

Delivered-To: [email protected]

Date: Wed, 22 May 2013 03:15:56 +0800

To: [email protected]

注,這個别名的功能,還可以當使用者組用,如上述的改為:

sq:     sqing,oping,alex

則向sq發郵件時,使用者sqing和oping都能收到郵件。

四、過慮

header_checks參數可以掃描郵件頭,header_checks查找表規則表達式為: parttern action 注:parttern是對字段頭進行比對的規則表達式,action是當parttern符合時候采取的動作,包括REJECT(将該信件退回給發信者)、WARN(将信件收下來,但是将該信件的基本資料記錄在登入檔内)、DISCARD(将該信件丢棄,并不給予原發信者回應)。

1.在/etc/postfix/main.cf中啟用header_checks檔案頭(将注釋符#去掉)

<code>546 header_checks = regexp:</code><code>/etc/postfix/header_checks</code>

2.同時,在/etc/postfix/header_checks檔案開始處添加以下一行内容:

<code>/sex/</code> <code>REJECT Sexy information is disabled.</code>

注:正規表達式 動作 傳回資訊(可不寫)。表示郵件标題裡帶“sexy”這個詞的郵件都被拒絕發送,系統管理者向發件人傳回的資訊提示是“Sexy information is disabled.”

3.測試

Root向sqing發送一封郵件标題為sexy girl的郵件,

Subject: sexy girl

ssssssssssssssssssssssssss    

Root收到退信郵件,

[root@zero ~]# mail

"/var/spool/mail/root": 1 message 1 new

&gt;N  1 Mail Delivery System  Wed May 22 05:36  70/2166  "Undelivered Mail Retu"

From MAILER-DAEMON  Wed May 22 05:36:50 2013

Return-Path: &lt;&gt;

X-Original-To: [email protected]

Delivered-To: [email protected]

Date: Wed, 22 May 2013 05:36:50 +0800 (CST)

From: [email protected] (Mail Delivery System)

Subject: Undelivered Mail Returned to Sender

To: [email protected]

Auto-Submitted: auto-replied

Content-Type: multipart/report; report-type=delivery-status;

       boundary="4947A60D7B.1369172210/zero.sq.com"

Part 1:

Content-Description: Notification

This is the mail system at host zero.sq.com.

I'm sorry to have to inform you that your message could not

be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can

delete your own text from the attached returned message.

                  The mail system

&lt;[email protected]&gt; (expanded from &lt;sqing&gt;): Sexy information is disabled.

Part 2:

Content-Description: Delivery report

Content-Type: message/delivery-status

Part 3:

Content-Description: Undelivered Message Headers

Content-Type: text/rfc822-headers

Received: by zero.sq.com (Postfix, from userid 0)

       id 4947A60D7B; Wed, 22 May 2013 05:36:50 +0800 (CST)

Date: Wed, 22 May 2013 05:36:50 +0800

To: [email protected]

MIME-Version: 1.0

Content-Transfer-Encoding: 7bit

Message-Id: &lt;[email protected]&gt;

From: [email protected] (root)

&amp;

從郵箱系統管理者傳回的結果顯示,該郵件由于存在敏感字“sexy”而被拒絕了。

郵件标題為“asexya”的郵件同樣也被攔截。

如果是要針對郵件内容過濾,在主配檔案中,增加一個body_checks檔案頭(這裡加在header_checks檔案頭下面)

<code>547 body_checks = regexp:</code><code>/etc/postfix/body_checks</code>

同時,在etc/postfix中以header_checks為子產品,建立一個body_checks檔案。

[root@zero ~]# cp -p /etc/postfix/header_checks /etc/postfix/body_checks

編輯body_checks内容時,其過慮關鍵字的格式與header_checks一樣。

本文轉自Sunshyfangtian 51CTO部落格,原文連結:http://blog.51cto.com/sunshyfangtian/1217101,如需轉載請自行聯系原作者