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/

相关文章
相关标签/搜索