CentOS7下搭建郵件服務器(dovecot + postfix + SSL)

 

花了基本上兩天的時間去配置CentOS7下的郵件服務器。其中艱辛太多了,必定得總結下。web

本文的目的在於經過一系列配置,在CentOS 7下搭建dovecot + postfix + SSL 服務器,而且可以經過郵件客戶端(本文中是Airmail)進行收發郵件。vim

前提條件

  1. 你得有個主機或者VPS
  2. 你有一個主域名好比 fancycoding.com 還有一個二級域名好比 mail.fancycoding.com
  3. 二級域名的 SSL 證書。

配置你的DNS記錄

  1. 確認主域名有A記錄指向服務器IP
  2. 添加一個郵件二級域名好比 mail.fancycoding.com 指向服務器ip
  3. 主域名下添加一則MX記錄指向郵件二級域名 好比 mail.fancycoding.com。若是你做爲郵件服務器的域名沒有多個,那麼MX優先級能夠隨便寫(反正只有一個),最高1,最低50,當優先級高的解析無效時,就會去解析低的。
  4. 添加一則txt記錄做爲SPF(Sender Policy Framework)。關於SPF的格式能夠去http://www.openspf.org/SPF_Record_Syntax 查看。 好比我設置的是
1
v=spf1 a mx ~all

就是除了個人A記錄和MX記錄外,若是有其餘域發出郵件的話,那都是僞造的。centos

這些步驟完成後,能夠用如下命令檢測是否生效ruby

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很麻煩不是麼。session

1 2
yum -y install postfix yum remove sendmail

sendmail是centos默認安裝的,超級難用,能夠放心刪掉。app

1
vim /etc/postfix/main.cf

默認的應該有很大一堆,不用管它。在文件最底部寫入如下內容dom

這裏假設你的:ssh

域名證書私鑰在/etc/ssl/private/mail.fancycoding.keypost

公鑰在/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:

  1. 我出問題了,可是不知道是什麼問題,怎麼辦

    tail /var/log/maillog

  2. 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
  3. Recipient address rejected: Access denied (in reply to RCPT TO command)

    netstat -tap 看一下端口是否都正常

    /etc/postfix/main.cf 中

    • 檢查 myorigin 是不是你的二級域名,而不是某些教程中的/etc/mailname
    • 確保home_mailbox沒有被定義
  4. 別人沒法發郵件給建立的郵箱,提示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/

相關文章
相關標籤/搜索