1. 準備域名
域名註冊
域名解析
域名購買完成後, 需要將域名解析到你的服務器IP
完成該實驗共需要添加兩條記錄:
A 記錄
記錄類型:A
主機記錄:@
記錄值:youripMX 記錄
記錄類型:MX
主機記錄:@
記錄值:yourdomain.com(替換爲自己域名)
生效檢查
域名設置解析後需要過一段時間纔會生效,通過 ping 命令檢查域名是否生效 ,如:
ping yourdomain.com
如果 ping 命令返回的信息中含有你設置的解析的 IP 地址,說明解析成功。(使用
ctrl + c
停止)檢查 MX 記錄
替換下面命令中的
yourdomain.com
爲您自己的註冊的域名:nslookup -q=mx yourdomain.com
如果
nslookup
命令返回的信息中含有你設置的域名的記錄值,說明解析成功。
2. 實驗之前
配置一個功能完善的郵件服務器並不是一項容易的工作,本實驗只是搭建了一個簡單的郵件服務器,一些更爲強大的功能還需要你去探索。
Postfix、Dovecot 簡介
Postfix
Postfix 是一個標準的 MTA「Mail Transfer Agent」服務器,它負責通過 SMTP 協議管理髮送到本機的郵件以及由本機發向外界的郵件。
Dovecot
Dovecot 是一個優秀的 IMAP/POP 服務器用以接收外界發送到本機的郵件。
Postfix、Dovecot 安裝
在 CentOS 7 上,我們可以直接使用
yum
進行下載安裝:yum -y install postfix dovecot
3. Postfix
配置 Postfix
有關教程中配置參數的具體含義,請參照 Postfix 配置文檔。
配置
在終端中輸入以下命令以修改
Postfix
相關配置:注意:記得將 yourdomain.com 替換爲你自己的域名
postconf -e 'myhostname = server.yourdomain.com' postconf -e 'mydestination = localhost, localhost.localdomain' postconf -e 'myorigin = $mydomain' postconf -e 'mynetworks = 127.0.0.0/8' postconf -e 'inet_interfaces = all' postconf -e 'inet_protocols = all' postconf -e 'mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain' postconf -e 'home_mailbox = Maildir/' postconf -e 'smtpd_sasl_type = dovecot' postconf -e 'smtpd_sasl_path = private/auth' postconf -e 'smtpd_sasl_auth_enable = yes' postconf -e 'broken_sasl_auth_clients = yes' postconf -e 'smtpd_sasl_authenticated_header = yes' postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination' postconf -e 'smtpd_use_tls = yes' postconf -e 'smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem' postconf -e 'smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem'
配置中
Postfix
使用sasl
和tls
來完成身份認證和傳輸信息加密。試驗中使用了
Dovecot
默認的ssl
證書和私鑰,如果你需要修改爲自己的,請替換最後兩行配置的路徑。配置 smtps
部分郵件客戶端依賴於使用
465
端口提供加密連接,所以我們修改配置,允許Postfix
使用465
端口發送郵件。打開
/etc/postfix/master.cf
文件,將如下兩行前的#
去除:smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes
(注意: -o 前要保留空格)
啓動
使用以下命令,將
Postfix
設爲自動啓動並首次啓動該服務:systemctl enable postfix.service systemctl start postfix.service
Postfix 日誌
Postfix
系統的日誌文件在系統的這個目錄下的/var/log/maillog
文件,此文件記錄了Postfix
服務器的運行狀態信息。
4. Dovecot
配置 Dovecot
修改
dovecot.conf
打開
/etc/dovecot/dovecot.conf
文件,在最下方加入以下配置:ssl_cert = </etc/pki/dovecot/certs/dovecot.pem ssl_key = </etc/pki/dovecot/private/dovecot.pem protocols = imap pop3 lmtp listen = * mail_location = Maildir:~/Maildir disable_plaintext_auth = no
如果前面你修改爲了自己的 ssl 證書和私鑰,請替換開始兩行配置的路徑。
修改
10-master.conf
打開
/etc/dovecot/conf.d/10-master.conf
文件,找到service auth
部分,將以下行前面的#
去除:unix_listener /var/spool/postfix/private/auth { mode = 0666 }
啓動 Dovecot
使用以下命令,將
Dovecot
設爲自動啓動並首次啓動該服務:systemctl enable dovecot.service systemctl start dovecot.service
查看
/var/log/maillog
文件,查看服務是否成功啓動。如成功啓動,日誌裏應包含如下信息:
Jun 26 12:00:28 localhost postfix/postfix-script[28338]: starting the Postfix mail system Jun 26 12:00:29 localhost postfix/master[28340]: daemon started -- version 2.10.1, configuration /etc/postfix Jun 26 12:28:40 localhost dovecot: master: Dovecot v2.2.10 starting up for imap, pop3, lmtp (core dumps disabled)
5. 創建賬戶
該配置下郵箱賬戶依賴於系統用戶,所以通過添加系統用戶的方式創建郵箱賬戶。
添加用戶
在終端中使用 useradd
命令添加用戶:
useradd test
使用 passwd
命令設置對應用戶密碼:
passwd test
6. 測試
如測試中遇到異常,請查看 maillog 日誌文件中的錯誤信息。
服務器端發送測試
使用 su
命令切換用戶:
su test
我們可以使用 mail
命令發送郵件,將 [email protected]
替換爲你的其他郵箱。
echo "Mail Content" | mail -s "Mail Subject" [email protected]
然後可以前往你的接收方郵箱查收。