欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > Centos Linux 7 搭建邮件服务器(postfix + dovecot)

Centos Linux 7 搭建邮件服务器(postfix + dovecot)

2025/4/18 11:39:49 来源:https://blog.csdn.net/MrZhangTS/article/details/143558622  浏览:    关键词:Centos Linux 7 搭建邮件服务器(postfix + dovecot)

准备工作

1. 一台公网服务器(需要不被服务商限制发件收件的,也就是端口25、110、143、465、587、993、995不被限制),如有防火墙或安全组需要把这些端口开放

2. 一个域名,最好是com cn org的一级域名

3. 域名备案(如果服务器是国外的则不需要备案)

一、配置域名解析

1. 登录阿里云,找到云解析DNS

选择域名,如abc.com

①. 添加MX记录

- 记录类型:MX

- 主机记录:@

- 记录值:mail.abc.com

- MX优先级:1

②添加A记录

- 记录类型:A

- 主机记录:mail

- 记录值:服务器IP

- TTL:10分钟

③添加SPF记录

- 记录类型:TXT

- 主机记录:@

- 记录值:v=spf1 mx:mail.abc.com ip4:服务器IP -all

- TTL:10分钟

最后完成如下图

2. 安装Postfix、Dovecot

yum update -y
yum -y install postfix dovecot  cyrus-sasl

2. 创建自签名证书

(1). 创建证书

复制代码

[root@mail ~]# cd# 生成私钥
[root@mail ~]# openssl genrsa -des3 -out server.key 2048
Enter pass phrase:    #输入密码
Verifying - Enter pass phrase:  #重复输入密码#下面这个步骤是删除server.key中的密码
[root@mail ~]# openssl rsa -in server.key -out server.key.insecure
Enter pass phrase for server.key:  #输入刚才设置的密码
writing RSA key#重命名证书
[root@mail ~]# mv server.key server.key.secure
[root@mail ~]# mv server.key.insecure server.key# 生成CSR(证书签名请求)
[root@mail ~]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key: #输入刚才设置的密码
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.abc.com
Email Address []:admin@abc.com  
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  #直接按Enter
An optional company name []:  #直接按Enter#生成自签名证书
[root@mail ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Enter pass phrase for server.key:  #直接按Enter
Certificate request self-signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = OPS, OU = OPS, CN = mail.abc.com, emailAddress = admin@abc.com
Getting Private key#设置权限
[root@mail ~]# chmod 400 server.*

复制代码

(2). 创建完成把证书拷贝到系统证书目录

# 创建目录
mkdir -p /etc/ssl/private/
# 拷贝
sudo cp server.crt /etc/ssl/certs
sudo cp server.key /etc/ssl/private

3. 配置Postfix

(1). 修改主配置

复制代码

vim /etc/postfix/main.cf# 修改以下项,注释的解开,不要有重复项
# 设置为 mail.域名
myhostname = mail.abc.com
# 设置为 域名
mydomain = abc.com
# 往外发邮件的邮件域
myorigin = $mydomain
#监听的网卡
inet_interfaces = all
inet_protocols = ipv4
#服务的对象
mydestination = $myhostname, $mydomain, localhost.$mydomain, localhost
#邮件存放的目录
home_mailbox = Maildir/# 新添加以下配置
#--------自定义(下面可以复制粘贴到文件最后面,用于设置服务器验为主,第一行设置发送附件大小)
#message_size_limit = 100000
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous,noplaintext
mynetworks = 127.0.0.0/8
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destinationsmtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_tls_security_options = noanonymous
broken_sasl_auth_clients = yesrelayhost =
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/ssl/certs/server.crt
smtpd_tls_key_file = /etc/ssl/private/server.key
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_auth_only = yes

复制代码

(2). 配置开启smtps

复制代码

vim /etc/postfix/master.cf# 分别把下列配置前面的#去掉
smtps     inet  n       -       n       -       -       smtpd-o smtpd_tls_wrappermode=yes-o smtpd_sasl_auth_enable=yes-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject-o milter_macro_daemon_name=ORIGINATING

复制代码

(3). 最后检查Postfix配置是否正确

postfix check

4. 配置Dovecot

(1). 修改主配置文件

复制代码

vim /etc/dovecot/dovecot.conf# 注释的解开注释,不要有重复项
protocols = imap pop3 lmtp
listen = *, ::# 以下内容添加到文件最后
mail_location = maildir:~/Maildir

复制代码

(2). 设置邮箱目录

vim /etc/dovecot/conf.d/10-mail.conf# 设置mail_location为
mail_location = maildir:~/Maildir

(3). 配置SSL证书

复制代码

vim /etc/dovecot/conf.d/10-ssl.conf====================
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
改成
ssl_cert = </etc/ssl/certs/server.crt
ssl_key = </etc/ssl/private/server.key
====================

复制代码

(4). 配置sasl认证以及SSL端口

复制代码

vim /etc/dovecot/conf.d/10-master.conf====================# Postfix smtp-authunix_listener /var/spool/postfix/private/auth {mode = 0660user = postfixgroup = postfix}====================inet_listener imap {#port = 143}
改成inet_listener imap {#port = 143port = 0}
====================
service pop3-login {inet_listener pop3 {#port = 110}
}
改成
service pop3-login {inet_listener pop3 {#port = 110port = 0}
}
====================

复制代码

(5). 配置认证

vim /etc/dovecot/conf.d/10-auth.confauth_mechanisms = plain  改成  auth_mechanisms = plain login

(6). 配置dovecot服务消除master.pid报错

复制代码

vim /usr/lib/systemd/system/dovecot.service# Type=forking后添加
ExecStartPost=/bin/sh -c 'chown root:dovecot /var/run/dovecot/master.pid'# 加载系统服务
systemctl daemon-reload

复制代码

5. 安装系统日志服务

复制代码

sudo yum install -y rsyslog
# 设置开机自启动
sudo systemctl enable rsyslog
# 启动
sudo systemctl start rsyslog#后续查看邮件服务日志方式(需要重启postfix和dovecot)
tail -f /var/log/maillog

复制代码

6. 启动邮箱服务并新增邮箱用户

(1). 启动并设置开机自启

复制代码

#启动
sudo systemctl start postfix
sudo systemctl start dovecot#设置开机自启动
sudo systemctl enable postfix
sudo systemctl enable dovecot

复制代码

后续需要重启命令为

sudo systemctl restart postfix
sudo systemctl restart dovecot

查看运行状态命令为

sudo systemctl status postfix
sudo systemctl status dovecot

(2). 查看服务端口启动是否完整

netstat -lntp

如图25 465 993 995端口正常启动

7. 安装mailx(用于测试发邮件)

sudo yum install -y mailx#======测试发件======
echo '搭建邮件服务器成功啦!!!' | mail -s '通知!' xxx@qq.com

8. 创建邮箱账号

(1). 创建账号

useradd -m 用户名
passwd 用户名
# 输入密码
# 确认密码

(2). 验证账号

sudo doveadm auth test 用户名

二、使用Foxmail收发邮件

1. windows10版本的Foxmail 新建账号完成后闪退,所以先设置下

首先找到Foxmail快捷方式,右键属性。
找到兼容性,并且在下方找到“以管理员身份运行此程序”,勾选,确定

2. 登录账号

打开Foxmail,新建账号 => 其它邮箱 => 手动设置 => POP3

邮件账号:账号@abc.com

密码:xxxxxx

POP服务器:mail.abc.com    SSL 995(勾选)

SMTP服务器:mail.abc.com  SSL 465(勾选)

注:虽然发件服务(SMTP)启用了SSL走的端口是465,但是实测25端口还得开放,不然无法接收外部邮件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词