在CentOS中,預設的郵件伺服器(SMTP方面)是sendmail,但sendmail有若干的缺點,比如,配置複雜、安全性漏洞曾被多次發現--並且依然存在隱患、郵件發送速度慢等等,這裡就不再一一敘述。而另一個被廣泛應用於郵件服務方面的“Postfix”的缺點就少得多,或者說它就是針對於sendmail的缺點,而被設計的。對應sendmail的短處,它在各方面也比較成熟。所以,無特殊要求,這裡不推薦用sendmail來構建郵件伺服器。本站介紹的郵件伺服器配置方法,也將基於Postfix。
1. 安裝Postfix。
[root@sample ~]# yum -y install postfix ← 線上安裝Postfix |
2. 對Postfix進行配置
[root@sample ~]# vi /etc/postfix/main.cf ← 編輯Postfix的設定檔#myhostname = host.domain.tld ← 找到此行,將等號後面的部分改寫為主機名稱,注意這邊需要寫網域名稱,不能寫ip地址 ↓ myhostname = sample.centospub.com ← 變為此狀態,設定系統的主機名稱 #mydomain = domain.tld ← 找到此行,將等號後面的部分改寫為網域名稱 ↓ mydomain = centospub.com ← 變為此狀態,設定網域名稱(我們將讓此處設定將成為E-mail地址“@”後面的部分) #myorigin = $mydomain ← 找到此行,將行首的#去掉 ↓ myorigin = $mydomain ← 變為此狀態,將發信地址“@”後面的部分設定為網域名稱(非系統主機名稱) inet_interfaces = localhost ← 找到此行,將“localhost”改為“all” ↓ inet_interfaces = all ← 變為此狀態,接受來自所有網路的請求 mydestination = $myhostname, localhost.$mydomain, localhost ← 找到此行,在行為添加“$mydomain” ↓ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ← 變為此狀態,指定發給本地郵件的網域名稱 #relay_domains = $mydestination ← 找到此行,將行首的#去掉 ↓ relay_domains = $mydestination ← 變為此狀態,定義允許轉寄的網域名稱 #mynetworks = 168.100.189.0/28, 127.0.0.0/8 ← 找到此行,依照自己的內網情況修改 ↓ mynetworks = 168.100.189.0/28, 127.0.0.0/8 ← 變為此狀態,指定內網和本地的IP位址範圍 #home_mailbox = Maildir/ ← 找到這一行,去掉行首的# ↓ home_mailbox = Maildir/ ← 變為此狀態,指定使用者郵箱目錄 # SHOW SOFTWARE VERSION OR NOT # # The smtpd_banner parameter specifies the text that follows the 220 # code in the SMTP server's greeting banner. Some people like to see # the mail version advertised. By default, Postfix shows no version. # # You MUST specify $myhostname at the start of the text. That is an # RFC requirement. Postfix itself does not care. # #smtpd_banner = $myhostname ESMTP $mail_name #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version) ← 找到這一行,接此行添加如下行: smtpd_banner = $myhostname ESMTP unknow ← 添加這一行,不顯示SMTP伺服器的相關資訊 在設定檔的文尾,添加如下行:
smtpd_sasl_auth_enable = yes ← 伺服器使用SMTP認證 smtpd_sasl_local_domain = $myhostname ← 指定SMTP認證的本地區名(主機名稱) smtpd_sasl_security_options = noanonymous ← 不允許匿名的方式認證 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination message_size_limit = 15728640 ← 規定郵件最大尺寸為15MB |
3. 配置SMTP認證的相關選項
為了提高安全性,我們不將系統使用者的密碼作為相應使用者SMTP認證的密碼,而將在後面為使用者建立SMTP認證專用的密碼。
[root@sample
~]# vi /usr/lib/sasl2/smtpd.conf ← 編輯SMTP認證的設定檔
pwcheck_method: saslauthd ← 找到此行,將“saslauthd”改為“auxprop”,如果此檔案為空白,那麼就建立一個新檔案
↓
pwcheck_method: auxprop ← 不使用系統使用者密碼作為使用者的SMTP認證密碼
[root@sample ~]# vi /etc/sysconfig/saslauthd
MECH=shadow ← 找到這一行,在前面加#
↓
#MECH=shadow ← 不使用shadow機制
FLAGS= ← 找到此行,在等號後面添加“sasldb”
↓
FLAGS=sasldb ← 定義認證方式為sasldb2
4.
然後建立新使用者
/usr/sbin/useradd myuser
passwd myuser
設定密碼
首先建立使用者模板下的郵箱目錄,以便於建立新使用者時,相應使用者的郵箱目錄自動被建立。
[root@sample ~]# mkdir /home/myuser/Maildir ← 在使用者模板下建立使用者郵箱目錄[root@sample ~]# chmod 700 /home/myuser/Maildir ← 設定使用者郵箱目錄屬性為700 |
5. 如果有用已存在,然後再為已經存在的使用者建立相應郵箱目錄。
[root@sample ~]# mkdir /home/myuser/Maildir ← 為使用者(這裡以centospub使用者為例)建立郵箱目錄 [root@sample ~]# chmod 700 /home/myuser/Maildir ← 設定該使用者郵箱目錄屬性為700 [root@sample ~]# chown centospub. /home/myuser/Maildir ← 設定該使用者郵箱目錄為該使用者所有 |
6. 為使用者佈建SMTP認證密碼
[root@sample ~]# /usr/sbin/saslpasswd2 -u sample.centospub.com -c centospub ← 為centospub使用者佈建SMTP認證密碼 Password: ← 在這裡輸入密碼(不會顯示) Again (for verification): ← 再次輸入密碼 |
7. 改變SALS的屬性及歸屬
[root@sample ~]# chgrp postfix /etc/sasldb2 ← 將資料庫歸屬改為postfix, [root@sample ~]# chmod 640 /etc/sasldb2 ← 將資料庫屬性改為640 |
8. 關閉sendmail服務及設定預設MTA
因為在用Postfix作為SMTP伺服器的前提下,我們不準備再用sendmail,所以將sendmail服務關掉,以確保安全及節省系統資源。
[root@sample ~]# /etc/rc.d/init.d/sendmail stop ← 關閉sendmail服務 Shutting down sendmail: [ OK ] Shutting down sm-client: [ OK ] [root@sample ~]# /sbin/chkconfig sendmail off ← 關閉sendmail自啟動 [root@sample ~]# /sbin/chkconfig --list sendmail ← 確認sendmail自啟動已被關閉(都為off就OK) sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off |
9. 最後,啟動SMTP認證及Postfix服務,並設定相應服務為自啟動。
[root@sample ~]# /sbin/chkconfig saslauthd on ← 將SMTP-Auth設定為自啟動 [root@sample ~]# /sbin/chkconfig --list saslauthd ← 確認SMTP-Auth服務狀態 saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 確認2~5為on的狀態就OK [root@sample ~]# /etc/rc.d/init.d/saslauthd start ← 啟動SMTP-Auth Starting saslauthd: [ OK ] [root@sample ~]# /sbin/chkconfig postfix on ← 將Postfix設定為自啟動 [root@sample ~]# /sbin/chkconfig --list postfix ← 確認Postfix服務狀態 postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 確認2~5為on的狀態就OK [root@sample ~]# /etc/rc.d/init.d/postfix start ← 啟動Postfix Starting postfix: [ OK ] |
參考: http://www.centospub.com/make/postfix_smtp.html
http://centossrv.com/postfix.shtml
http://fedorasrv.com/postfix.shtml