因為同事在做一個電子郵件服務器,准備進行郵件推廣什么的,后來遇到些問題,於是我們兩個小白一起研究。
網上有很多關於電子郵件服務器搭建的、完整的文檔。因此,一些具體的細節和配置,這里就不做詳細的說明和解釋了。主要講的是我們兩個小白花費大量時間,所查找到的,遇到的問題。
一、背景
postfix是Wietse Venema在IBM的GPL協議之下開發的MTA(郵件傳輸代理)軟件。
Dovecot 是一個開源的 IMAP 和 POP3 郵件服務器,支持 Linux/Unix 系統。
經典搭配:
postfix(作為發送郵件服務器)+dovecot(作為接收郵件服務器)+mysql(作為數據庫)
作為最小白的一個簡單的郵件服務器,我們直接用系統用戶作為認證用戶,沒有配置mysql。
二、域名解析
在域名服務提供商那里設置域名解析,example.com 設置了3個域名解析:
第一個是 :A記錄, RR值為 @ , 指向 服務器的IP地址
第二個是:MX記錄, RR值為@, 指向 example.com
第三個是:A記錄,RR值為 www, 指向服務器的IP地址
配置完畢后, ping www.example.com 如果能提示出你的服務器的IP地址, 證明 www的配置已經生效。
下來還要檢查 MX 記錄是否生效, 要用nslookup檢查一下是否MX記錄正確。
在windows系統的命令行控制體輸入 : nslookup -qt=mx linuxidc.com 回車后,
能顯示你的域名 example.com , 就代表你的 MX記錄配置正確。
如果MX記錄配置不正確, 那用QQ郵箱發郵件, 你就會收到個退信, 退信原因的內容如下 :
收件人(lLee@example.com)所屬域名不存在,郵件無法送達。
Name service error for name=linuxidc.com type=MX: Host found but no data record of requested type
三、安裝配置postfix+dovecot+cyrus.sasl
系統架構就是這樣的 MTA : Postfix
認證模塊:Cyrus SASL + Dovecot SASL
POP3/IMAP: Dovecot
安裝: Yum install postfix dovecot
Yum.install cyrus*
有一般郵件系統的知識的人知道,需要采用認證機制防止誰都可以用這個MTA發送郵件(open relay)。這個認證機制就是SASL,這里我所接觸到的兩個sasl軟件:Cyrus-SASL和Dovecot中自有的SASL。
Cyrus-SASL:這個軟件的使用saslauthd這個daemon來進行驗證,具體的配置文件可以查看/etc/sysconfig/saslauthd這個配置文件。
MECH=pam:使用什么機制來進行驗證,這里寫的是使用PAM來進行驗證。PAM,這個以后有機會再進行論述。
Dovecot-SASL: 這個軟件是dovecot對SASL的一個實現,Dovecot官網中提到,Dovecot也實現了SASL。
Dovcecot-SASL有兩個部分,一個是passdb,一個是userdb, passdb用來實現認證,userdb用來實現提取用戶的一些信息,比如郵件存儲目錄,uid等。
相關的配置可以在/etc/dovecot/dovecot.conf中找到。
認證過程也是先通過passdb認證,然后在userdb中獲取用戶信息,從而把該用戶的郵件遞送給該MUA,然后用戶就可以讀取郵件了。
postfix的main.cf這個文件中有個SASL的選項,此處直接設置為smtpd_sasl_type = dovecot
此處即是sasl使用dovecot的sasl進行驗證。即發送郵件時,去dovecot驗證,如果通過,就允許郵件發送了。因此,我們只要熟悉了dovecot的sasl怎么做的,就可以不用管postfix的驗證了。
然后呢,我們根據網上的配置dovecot的sasl驗證代碼進行測試,始終沒能成功,於是,我們就繞過了這個。
以下是相關的postfix和dovecot簡單配置:
vi /etc/postfix/main.cf
#begain #smtpd_sasl_type = dovecot #smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes broken_sasl_auth_clients = yes smtpd_delay_reject = no smtpd_sasl_local_domain = $mydomain smtpd_sasl_security_options = noanonymous smtpd_client_restrictions = permit_sasl_authenticated message_size_limit = 5242880 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination #end
vi /etc/dovecot/dovecot.conf
protocols = imap pop3 log_path = /var/log/dovecot.log info_log_path = /var/log/dovecot.info login_trusted_networks = 0.0.0.0/0 listen = *, :: disable_plaintext_auth = yes ssl_disable = no mail_location = Maildir:~/Maildir base_dir = /var/run/dovecot/
vi /etc/sasl2/smtpd.conf
pwcheck_method: saslauthd mech_list: plain login log_level:3 saslauthd_path:/var/run/saslauthd/mux
四、檢測各種配置是否成功
1、查看mta
#alertnatices --display mta
2、檢測postfix與dovecot
狀態:service postfix status | service dovecot status
重啟: service postfix|dovecot restart
有時候會出現:
這是centos版本問題,直接使用它提示的命令即可。
3、檢測默認smtp端口25是否已經監聽
Netstat -an | grep 25
五、遇到的問題
1、本地客戶端與電子郵件服務器無法連接
我們這次遇到的問題就是:服務器防火牆25和110端口,沒有打開。
解決方案:
CentOS防火牆iptables的配置方法詳解
查看當前所有的iptables配置: iptables -L -n
添加允許INPUT訪問規則,以下時常見服務的端口設置,如果需要拒絕訪問,則將ACCEPT改為DROP即可
#SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
#HTTPS
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#POP3
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
#SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
保存修改:/etc/rc.d/init.d/iptables save
重啟iptables:service iptables restart
開機啟動 | 關閉: chkconfig iptables on chkconfig iptables off
或者直接修改配置文件:
vi /etc/sysconfig/iptables
2、認證不被允許:
-ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections.
pop3-login: Info: Aborted login (tried to use disallowed plaintext auth): user=<>, rip=*,*,*,*, lip=*,*,*,*, session=<OHcXkcc6EgB5Kpmb>
解決方案: