postfix+dovecot+SSL 搭建邮件服务器

postfix+dovecot+SSL 搭建邮件服务器

一、配置postfix

首先,我们关闭防火墙

systemctl disable iptables

接下来我们安装postfix

yum install postfix

然后我们修改下postfix的配置文件

    [root@mail~] # vim /etc/postfix/main.cf

    //修改第76行的邮局主机名。

    myhostname = mail.ijoshua.cn

    //修改第83行的邮局域名。

    mydomain = ijoshua.cn

    //修改第99行的寄出邮件域名,$mydomain的值已在上面定义。

    myorigin = $mydomain

    //修改第116行的监听网卡。

    inet_interfaces = all

    //修改第164行的可接收邮件的主机名和域名。

    mydestination = $myhostname, $mydomain

然后我们创建一个邮箱账号

    [root@mail~] # useradd boss

    [root@mail~] # echo "123456" | passwd --stdin boss   

    Changing password for user boss. passwd: all authentication tokens updated successfully.

启动postfix服务

    [root@mail~] # systemctl restart postfix

    [root@mail~] # systemctl enable postfix

    ln -s '/usr/lib/systemd/system/postfix.service' '/etc/systemd/system/multi-user.target.wants/postfix.service'

注意:在这里 如果你以前安装过,但是失败了,会提示你“/etc/systemd/system/multi-user.target.wants/postfix.service”文件已存在,可以使用

rm -f

/etc/systemd/system/multi-user.target.wants/postfix.service

将文件删除,然后再执行

然后我们来测试使用postfix发送一封邮件

这里需要注意的是 ,如果你的服务器是阿里云的服务器 这个时候是收不到邮件的……,解决办法会在后面给出(当然也就是不用25端口……)

二、配置dovecot

首先,我们安装下dovecot

    [root@mail~] # yum install dovecot -y

    Loaded plugins: langpacks, product-id, subscription-manager

    ………………省略部分安装过程………………

    Installing: dovecot x86_64 1:2.2.10-4.el7 rhel7 3.2 M

    Installing for dependencies: clucene-core x86_64 2.3.3.4-11.el7 rhel7 528 k

    ………………省略部分安装过程………………

    Complete!

然后,我们修改一下配置文件

    [root@mail~] # vim /etc/dovecot/dovecot.conf

    //修改第24行的支持邮局协议。

    protocols = imap pop3 lmtp

    //然后追加允许明文认证(25行)。

    disable_plaintext_auth = no

    //修改第48行的允许登陆网段地址,全部允许即为(0.0.0.0/0)。

    login_trusted_networks = 192.168.0.0/24

接下来,我们配置一下邮件的格式和存储路径

    [root@mail~] # vim /etc/dovecot/conf.d/10-mail.conf

    mail_location = mbox:~/mail:INBOX=/var/mail/%u

然后,我们创建邮件的存储目录(这步决定你是否能收到邮件哦)

    [root@mail~] # su - boss

    Last login: Sat Aug 15 16:15:58 CST 2015 on pts/1

    [boss@mail ~]$ mkdir -p mail/.imap/INBOX

最后,我们启动dovecot

    [root@mail~] # systemctl restart dovecot

    [root@mail~] # systemctl enable dovecot

    ln -s '/usr/lib/systemd/system/dovecot.service' '/etc/systemd/system/multi-user.target.wants/dovecot.service'

注意:这里的最后一步 和postfix 的最后一步可以有同样的问题 ,解决方式也一样

如果诸位小伙伴使用的服务器不是阿里云的话,没有域名的话 推荐使用freenom.可以申请免费的域名(玩儿玩儿还是可以的)

freenom需要注意的是 可以使用谷歌账号登录,当然 需要翻墙。

DNS解析

注意:最上面的那个空白的,是输入“域名”(这个域名不是指文字“域名”,是你申请的免费域名),然后第三个的MAIL 输入的是“mail.域名”。

这里配置完之后,本地ping 一下,一般需要一小会就能配置完成,所以刚开始ping不通的小伙伴可以稍微等一等。如果能ping通了,那么就可以使用了。可以使用Foxmail

还有一点非常重要的就是,你服务器上的端口一定要开  IMAP 是143端口 POP3是110端口 SMTP是25端口

不出什么问题的话(不是阿里云服务器) ,这就可以发送和接收邮件啦。

如果使用foxmail 连接不上呢。可以查看下日志 /var/log/maillog 这个里面会记载你为啥没登录上。  telnet 192.168.100.3 110

三、SSL

本来其乐融融的使用这邮件服务器,这个时候,一个噩耗传来了。。

    connect to mx3.qq.com[183.57.48.35]:25: Connection timed out

    C663F409F6: to=<xxxxxxxx@qq.com>, relay=none, delay=64923, delays=64833/0.01/90/0, dsn=4.4.1, status=deferred (connect to mx2.qq.com[14.17.41.170]:25: Connection timed out)

发现连接超时,诶呀这就尴尬了,不过莫慌。、

阿里云服务器的小伙伴看这里~,报这个错误的小伙伴也看这里~

这个问题出现的原因呢,是服务器提供商把我们的25端口给封锁了,阿里云服务器的小伙伴可以去申请,不过应该是不给解封。。。。。,那么既然我们没有办法使用25端口了,我们就换一个高端一点的端口,也就是163、腾讯、网易都在用的端口,就是SSL加密后的端口

POP3 是 995 端口、SMTP是465端口 、IMAP 是993

我们都知道SSL 是需要签名证书的。那么接下来,我们来操作起来。

1、生成自己的签名SSL证书

    [root@mail ~]# cd /etc/pki/tls/certs

    

    # 生成私钥

    [root@mail certs]# make server.key

    umask 77 ; \

    /usr/bin/openssl genrsa -aes128 2048 > server.key

    Generating RSA private key, 2048 bit long modulus

    ..............................................................+++

    .........+++

    e is 65537 (0x010001)

    Enter pass phrase:    #输入密码

    Verifying - Enter pass phrase:  #重复输入密码

    

    #下面这个步骤是删除server.key中的密码

    [root@mail certs]# openssl rsa -in server.key -out server.key

    Enter pass phrase for server.key:  #输入刚才设置的密码

    writing RSA key

    

    # 生成CSR(证书签名请求)

    [root@mail certs]# make server.csr

    umask 77 ; \

    /usr/bin/openssl req -utf8 -new -key server.key -out server.csr

    You are about to be asked to enter information that will be incorporated

    into your certificate request.

    What you are about to enter is what is called a Distinguished Name or a DN.

    There are quite a few fields but you can leave some blank

    For some fields there will be a default value,

    If you enter '.', the field will be left blank.

    -----

    Country Name (2 letter code) [XX]:CN   #国家简称

    State or Province Name (full name) []:Beijing   #

    Locality Name (eg, city) [Default City]:Beijing  #城市

    Organization Name (eg, company) [Default Company Ltd]:OPS  #公司名

    Organizational Unit Name (eg, section) []:OPS   #部门名

    Common Name (eg, your name or your server's hostname) []:mail.opsky.top

    Email Address []:admin@opsky.top 

    

    Please enter the following 'extra' attributes

    to be sent with your certificate request

    A challenge password []:  #直接按Enter

    An optional company name []:  #直接按Enter

    

    #生成自签名证书

    [root@mail certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650

    Signature ok

    subject=C = CN, ST = Beijing, L = Beijing, O = OPS, OU = OPS, CN = mail.opsky.top, emailAddress = admin@opsky.top

    Getting Private key

    

    #设置权限

    [root@mail certs]# chmod 400 server.*

证书生成完事之后,我们就可以是用server.crt和server.key加密通信的数据啦

然后,我们回来给postfix添加配置

vi /etc/postfix/main.cf

将下面的内容添加至文件最后

    smtpd_use_tls = yes

    smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt

    smtpd_tls_key_file = /etc/pki/tls/certs/server.key

    smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache

然后修改另一个文件

vi /etc/postfix/master.cf

找到下面的这两行,肯定有(别说找不到&……仔细找一哈),然后接开注释

    smtps       inet   n       -       n       -       -       smtpd

      -o smtpd_tls_wrappermode=yes

然后,我们修改dovecot的配置

 vi /etc/dovecot/conf.d/10-ssl.conf

修改其中的

    # 第6行:

    ssl = yes

    # 第12,13行:

    ssl_cert = </etc/pki/tls/certs/server.crt

    ssl_key = </etc/pki/tls/certs/server.key

配置完成后,重启postfix 和dovecot

    systemctl restart postfix

    systemctl restart dovecot

如果你的防火墙没关,那么添加如下设置(如果关了 就忽略奥)

    [root@mail ~]# iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT

    [root@mail ~]# iptables -I INPUT 6 -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT

    [root@mail ~]# iptables -I INPUT 7 -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT

然后,我们就改完啦,我们再用foxmail登录一下,。发现……还是没有登录上,日志中报了这个错误

    Error: SSL private key file is password protected, but password isn't given

     Fatal: Couldn't parse private ssl_key: error:0906A068:PEM routines:PEM_do_header:bad password read

诶我这小暴脾气,就冲着屏幕一顿毒打(然后默默的抚摸显示器,工作要紧工作要紧。。。。)

那么解决办法呢~,就是下面的办法,执行下面的命令

killall dovecot

然后执行下面的命令后,输入一下你SSL证书的密码

dovecot -p

完成之后,你再去登录,就可以正常登录啦,也可以正常收发邮件啦。

这里参考了几篇大佬的博客:

postfix+dovecot:https://www.cnblogs.com/zydev/p/6307482.html

生成SSL证书:https://blog.csdn.net/shile/article/details/78437258

配置postfix和dovecot启用SSL加密:https://blog.csdn.net/shile/article/details/78442093

--------------------------- I`am Joshua,may god bless you

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS+Postfix+Dovecot+Postfixadmin+Roundcube邮件服务器搭建步骤如下: 1. 安装 CentOS 操作系统,并更新至最新版。 2. 安装 Postfix 邮件服务器,并进行基本配置。 3. 安装 Dovecot IMAP/POP3 服务器,并进行基本配置。 4. 安装 Postfixadmin 邮箱管理系统,并进行基本配置。 5. 安装 Roundcube Webmail 邮件客户端,并进行基本配置。 具体步骤如下: 1. 安装 CentOS 操作系统,并更新至最新版。 在安装 CentOS 操作系统时,选择最小化安装,并根据实际情况进行分区和网络配置。安装完成后,使用以下命令更新系统: ``` yum update ``` 2. 安装 Postfix 邮件服务器,并进行基本配置。 使用以下命令安装 Postfix: ``` yum install postfix ``` 安装完成后,修改 /etc/postfix/main.cf 文件,使其支持 TLS 和 SASL 认证: ``` smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt smtpd_tls_key_file = /etc/pki/tls/private/server.key smtpd_tls_security_level = may smtp_tls_security_level = may smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination ``` 然后重启 Postfix 服务: ``` systemctl restart postfix ``` 3. 安装 Dovecot IMAP/POP3 服务器,并进行基本配置。 使用以下命令安装 Dovecot: ``` yum install dovecot ``` 安装完成后,修改 /etc/dovecot/dovecot.conf 文件,使其支持 TLS 和 SASL 认证: ``` ssl_cert = </etc/pki/tls/certs/server.crt ssl_key = </etc/pki/tls/private/server.key auth_mechanisms = plain login ``` 然后重启 Dovecot 服务: ``` systemctl restart dovecot ``` 4. 安装 Postfixadmin 邮箱管理系统,并进行基本配置。 使用以下命令安装 Postfixadmin: ``` yum install postfixadmin ``` 安装完成后,修改 /etc/httpd/conf.d/postfixadmin.conf 文件,使其支持 SSL: ``` SSLEngine on SSLCertificateFile /etc/pki/tls/certs/server.crt SSLCertificateKeyFile /etc/pki/tls/private/server.key ``` 然后重启 Apache 服务: ``` systemctl restart httpd ``` 访问 https://your-domain.com/postfixadmin,使用管理员账号登录,创建邮箱账号和域名等相关配置。 5. 安装 Roundcube Webmail 邮件客户端,并进行基本配置。 使用以下命令安装 Roundcube: ``` yum install roundcubemail ``` 安装完成后,修改 /etc/httpd/conf.d/roundcubemail.conf 文件,使其支持 SSL: ``` SSLEngine on SSLCertificateFile /etc/pki/tls/certs/server.crt SSLCertificateKeyFile /etc/pki/tls/private/server.key ``` 然后重启 Apache 服务: ``` systemctl restart httpd ``` 访问 https://your-domain.com/roundcubemail,使用邮箱账号登录,即可使用 Roundcube 邮件客户端。 以上就是 CentOS+Postfix+Dovecot+Postfixadmin+Roundcube邮件服务器搭建步骤,如有问题可以参考相关文档或者咨询技术人员。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值