1. 程式人生 > >Linux下Postfix的配置和使用

Linux下Postfix的配置和使用

開發十年,就只剩下這套架構體系了! >>>   

Postfix為何物,詳見:http://zh.wikipedia.org/wiki/Postfix

0.關於Postfix

postfix的產生是為了替代傳統的sendmail.相較於sendmail,postfix在速度。效能和穩定性上都更勝一籌。如今眼下許多的主流郵件服務事實上都在採用postfix. 當我們須要一個輕量級的的郵件server是,postfix不失為一種選擇。

1. postfix是免費的:
    postfix想要作用的範圍是廣大的Internet使用者。試圖影響大多數的Internet上的電子郵件系統,因此它是免費的。
2. 更快:
    postfix在效能上大約比sendmail快三倍。一部執行postfix的臺式PC每天能夠收發上百萬封郵件。
3. 相容性好:
    postfix是sendmail相容的,從而使sendmail使用者能夠非常方便地遷移到postfix。Postfix支援/var[/spool]/mail、/etc/aliases、 NIS、和 ~/.forward 檔案。
   4. 更健壯:
postfix被設計成在重負荷之下仍然能夠正常工作。當系統執行超出了可用的記憶體或磁碟空間時,postfix會自己主動降低執行程序的數目。當處理的郵件數目增長時,postfix執行的程序不會跟著新增。
5. 更靈活:
postfix是由超過一打的小程式組成的,每一個程式完畢特定的功能。

你能夠
通過配置檔案設定每一個程式的執行引數。


   6. 安全性
postfix具有多層防禦結構,能夠有效地抵禦惡意入侵者。如大多數的postfix程式能夠執行在較低的許可權之下,不能夠通過網路訪問安全性相關的本地投遞程式等等。

以下來介紹linux上怎樣搭建和配置postfix服務:

 

1.關掉sendmail相關的全部服務,最好是直接解除安裝sendmail.

# service sendmail stop
# chkconfig sendmail off

#rpm -qa | grep sendmail | xargs rpm -e

 

2.安裝postfix.

redhat6.0以上版本號應該是預設集成了postfix服務的,假如沒有安裝的話,能夠手動安裝。

 

rpm -qa | grep postifx (檢視是否安裝)

yum install postfix

 

3.安裝完畢後。改動配置檔案:/etc/postfix/main.cfg

 

vi /etc/postfix/main.cf


myhostname = sample.test.com  ← 設定系統的主機名


mydomain = test.com  ← 設定域名(我們將讓此處設定將成為E-mail地址“@”後面的部分)


myorigin = $mydomain  ← 將發信地址“@”後面的部分設定為域名(非系統主機名)


inet_interfaces = all  ← 接受來自全部網路的請求


mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain  ← 指定發給本地郵件的域名


home_mailbox = Maildir/  ← 指定使用者郵箱資料夾

 

<儲存退出。>

 

4.為本機加入DNS server.

為什DNS Server?由於在郵件傳送過程中。須要把郵件地址的domain地址轉化成IP地址,再去傳送給相應的收件人,這裡涉及到DNS中的A記錄和MX記錄相關的知識,不熟悉的同學能夠google或者百度 腦補一下 :-)

 

怎樣加入DNS server呢,DNS server去哪裡尋找?

vim /etc/resolv.conf 

加入例如以下行:

nameserver 8.8.8.8

nameserver 8.8.4.4

 

上面用的8.8.8.8/8.8.4.4是Google Free DNS server,當然還有非常多免費的DNS server供大家使用,能夠google一下:-)

 

5.測試一下郵件是否可以傳送成功:

命令列輸入$: > echo "Mail Content" | mail -s "Mail Subject" [email protected]

 

Note:if you see below warings after you run above command.

send-mail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
send-mail: warning: inet_protocols: configuring for IPv4 support only
postdrop: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
postdrop: warning: inet_protocols: configuring for IPv4 support only

that means you don't have IPv6 configured in your OS's network stack, but your mailer (presumably postfix) is configured to use IPv6. Since there is no IPv6 for your mailer to use, it's warning you that it's only going to use IPv4.

 

To disable the waring messsage, go to /etc/postfix/main.cf and change from:

inet_protocols = all

to:

inet_protocols = ipv4

This will only use ipv4 and the warning message will go away.

You will have to issue a stop and start for postfix to register the change. 

service postfix restart

 

6.檢視log,確認郵件傳送狀態:

Postfix郵件的log位置是:/var/log/maillog

傳送成功的話。會返回250和OK,也能夠去自己的郵件client查收。

 

一切OK的話,那Postfix mail service應該就搭建成功了。

 

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

另外一些實用的postfix維護命令。一遍日常的檢測和維護:

mailq :會列出當前在postfix傳送佇列中的全部郵件

postsuper -d ALL:刪除當前等待發送佇列的全部郵件,包含傳送失敗的退信

當然還有非常多,就不一一列舉了,大家能夠網上搜索擴充套件。Good Luck!

 

7.Update Mail From sender

 

echo "[email protected]   [email protected]" >> /etc/postfix/generic 
echo "smtp_generic_maps = hash:/etc/postfix/generic" >>/etc/postfix/main.cf 
postmap /etc/postfix/generic
service pos