花了基本上兩天的時間去配置CentOS7下的郵件服務器。其中艱辛太多了,一定得總結下。
本文的目的在於通過一系列配置,在CentOS 7下搭建dovecot + postfix + SSL 服務器,並且能夠通過郵件客戶端(本文中是Airmail)進行收發郵件。
前提條件
- 你得有個主機或者VPS
- 你有一個主域名比如 fancycoding.com 還有一個二級域名比如 mail.fancycoding.com
- 二級域名的 SSL 證書。
配置你的DNS記錄
- 確認主域名有A記錄指向服務器IP
- 添加一個郵件二級域名比如 mail.fancycoding.com 指向服務器ip
- 主域名下添加一則MX記錄指向郵件二級域名 比如 mail.fancycoding.com。如果你作為郵件服務器的域名沒有多個,那么MX優先級可以隨便寫(反正只有一個),最高1,最低50,當優先級高的解析無效時,就會去解析低的。
- 添加一則txt記錄作為SPF(Sender Policy Framework)。關於SPF的格式可以去http://www.openspf.org/SPF_Record_Syntax 查看。 比如我設置的是
1 |
v=spf1 a mx ~all |
就是除了我的A記錄和MX記錄外,如果有其他域發出郵件的話,那都是偽造的。
這些步驟完成后,可以用以下命令檢測是否生效
1 2 |
dig MX yourdomain +short @ns host your.subdomain ns |
比如我的域名是放在dnspod的,那么按照上圖配置后,應該是這樣:
1 2 3 4 5 6 7 8 9 |
Robin-MacdeMac-mini ~$dig MX fancycoding.com +short @f1g1ns1.dnspod.net 50 mail.fancycoding.com. Robin-MacdeMac-mini ~$host mail.fancycoding.com f1g1ns1.dnspod.net Using domain server: Name: f1g1ns1.dnspod.net Address: 119.167.195.3#53 Aliases: mail.fancycoding.com has address 107.170.242.137 |
安裝Postfix
以下操作最好在root權限下進行。不然每次都要sudo很麻煩不是么。
1 2 |
yum -y install postfix yum remove sendmail |
sendmail是centos默認安裝的,超級難用,可以放心刪掉。
1 |
vim /etc/postfix/main.cf |
默認的應該有很大一堆,不用管它。在文件最底部寫入以下內容
這里假設你的:
域名證書私鑰在/etc/ssl/private/mail.fancycoding.key
公鑰在/etc/ssl/certs/mail.fancycoding.crt
CA證書在/etc/ssl/certs/cacert.pem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
myhostname = mail.fancycoding.com mydomain = fancycoding.com myorigin = mail.fancycoding.com mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128, 192.168.1.0/24 inet_interfaces = all mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain smtpd_sasl_auth_enable = yes smtpd_sasl_type = cyrus smtpd_sasl_security_options = noanonymous broken_sasl_auth_clients = yes smtpd_sasl_authenticated_header = yes smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination smtpd_tls_auth_only = no smtp_use_tls = yes smtpd_use_tls = yes smtp_tls_note_starttls_offer = yes smtpd_tls_key_file = /etc/ssl/private/mail.fancycoding.key smtpd_tls_cert_file = /etc/ssl/certs/mail.fancycoding.crt smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem smtpd_tls_received_header = yes smtpd_tls_session_cache_timeout = 3600s |
再打開/etc/postfix/master.cf:
1 |
vim /etc/postfix/master.cf |
找到
1 |
#smtp inet n - n - - smtpd |
取消其前面的注釋”#”,然后找到submission這一行,同樣取消前面的注釋,並添加如下:
1 2 3 4 5 6 7 8 9 |
submission inet n - - - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_wrappermode=no -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING -o smtpd_sasl_type=dovecot -o smtpd_sasl_path=private/auth |
特別注意smtpd_recipient_restrictions不要寫錯了,每個逗號之間都是一個單詞,沒有空格。
接下來配置你的aliases,這個是郵件用戶名的別名。比如發送個webmaster@yourdomain.com的郵件,會自動轉道root@youdomain.com。
1 |
vim /etc/aliases |
可以看到已經設置了很多的別名了。如果你想把這些人都轉發給一個真實的用戶,比如mike,那么就在最底下添加一行:
1 |
root:mike |
安裝Dovecot
1 |
-y install postfix |
進入/etc/dovecot/dovecot.conf
1 |
vim /etc/dovecot/dovecot.conf |
在最下面添加以下內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protocols = imap pop3 mail_location = mbox:~/mail:INBOX=/var/mail/%u pop3_uidl_format = %08Xu%08Xv service auth { unix_listener /var/spool/postfix/private/auth { group = postfix mode = 0660 user = postfix } } ssl=required ssl_cert = </etc/ssl/certs/mail.fancycoding.crt ssl_key = </etc/ssl/private/mail.fancycoding.key |
開啟所有服務
1 2 3 |
newaliases service postfix restart service dovecot restart |
看下log
1 |
cat /var/log/maillog |
如果你看到如下一行且沒有warning或者error,那就大功告成了:
Sep 10 22:54:51 fancycoding dovecot: master: Dovecot v2.2.10 starting up for imap, pop3 (core dumps disabled)
測試郵件發送與接收
1 |
mail -s TestTitle sombody@someone.com |
之后會進入交互模式,隨便輸入點東西,然后按Ctrl+D,則會開始發送。
如果許久沒有收到發來的郵件,那可能得看一下log有啥報錯沒有了。
如果提示沒有mail這個指令,那么
1 |
yum -y install mailx |
你也可以給你的用戶發郵件,比如我用qq郵箱發給webmaster@fancycoding.com,那么會看到log里面有這樣的記錄
1 2 3 4 5 6 |
Sep 10 23:17:14 fancycoding postfix/smtpd[27682]: connect from smtpbgsg2.qq.com[54.254.200.128] Sep 10 23:17:16 fancycoding postfix/smtpd[27682]: B334A61941: client=smtpbgsg2.qq.com[54.254.200.128] Sep 10 23:17:17 fancycoding postfix/cleanup[27686]: B334A61941: message-id=<tencent_1BB3D41C7EDAF8ED30A8BF1D@qq.com> Sep 10 23:17:17 fancycoding postfix/qmgr[26975]: B334A61941: from=<84084888@qq.com>, size=2050, nrcpt=1 (queue active) Sep 10 23:17:17 fancycoding postfix/local[27687]: B334A61941: to=<root@mail.fancycoding.com>, orig_to=<webmaster@fancycoding.com>, relay=local, delay=0.81, delays=0.81/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox) Sep 10 23:17:17 fancycoding postfix/qmgr[26975]: B334A61941: removed |
可以看到,qq的smtp服務器smtpbgsg2.qq.com連接到我們的服務器上,原目標是webmaster@fancycoding.com,經過別名轉換后發送到了root@mail.fancycoding.com。
那么用mail指令就能看到新郵件了:
1 2 3 4 5 |
[root@fancycoding ~]# mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 1 message 1 new >N 1 、Darkness Wed Sep 10 23:17 62/2153 "HI_WEBMASTER_TITLE" & |
用郵件客戶端連接
不想發郵件的時候還用命令行對吧,也不想收郵件的時候要ssh登錄然后用指令查詢是吧。那么我們之前做的那么多,不就是為了用郵件客戶端連接我們的郵件服務器嗎?
比如我要新建一個名字為robin的用戶,但這個用戶我禁止所有人登錄:
1 2 |
useradd -s /sbin/nologin username passwd username |
打開郵件客戶端,新建賬戶,這里以Airmail為例:
如果一路順風,你就可以用郵件客戶端進行收發郵件啦。
如果有朋友找你開通郵箱,那么你只需要用useradd添加一個用戶就好了~是不是很方便!
FAQ:
-
我出問題了,但是不知道是什么問題,怎么辦
tail /var/log/maillog
-
Error: chown(/home/user/mail/.imap/INBOX, group=12(mail)) failed: Operation not permitted (egid=1000(user)
兩個解決辦法:
- sudo chmod 0600 /var/mail/*
- 在/etc/dovecot/dovecot.conf加入
mail_access_groups=mail
-
Recipient address rejected: Access denied (in reply to RCPT TO command)
netstat -tap
看一下端口是否都正常/etc/postfix/main.cf 中
- 檢查 myorigin 是否是你的二級域名,而不是某些教程中的/etc/mailname
- 確保home_mailbox沒有被定義
-
別人無法發郵件給創建的郵箱,提示554 5.7.1: Recipient address rejected: Access denied 。
這個問題就有很多了,有可能是MX解析沒弄對,MX被其他域名所接受,spf沒弄對等等。
確保dig出來的域名以這樣的形式結尾,而不是顯示
xxx handle by xxx.domain.com
mail.fancycoding.com has address 107.170.242.137
https://www.fancycoding.com/centos7-mail-server-with-dovecot-postfix-ssl/