CentOS 7 搭建 Sendmail + Dovecot 邮件服务器:SMTP、POP3、IMAP 与 TLS

CentOS 7 搭建 Sendmail + Dovecot 邮件服务器:SMTP、POP3、IMAP 与 TLS # CentOS 7 搭建 Sendmail Dovecot 邮件服务器SMTP、POP3、IMAP 与 TLS1. 服务介绍Sendmail 是经典邮件传输代理 MTA负责通过 SMTP 接收、路由和投递邮件。Dovecot 提供 POP3、IMAP 和用户认证使客户端能够读取服务器邮箱。本实验在 CentOS 7 搭建适合教学和内网测试的邮件系统Sendmail 负责 SMTP 与本地投递Dovecot 负责 POP3/IMAPTLS 保护客户端与服务器之间的连接。2. 准备运行环境• 服务端CentOS 7.x主机名mail.example.com地址10.1.100.101。• 客户端Linux 或 Windows地址10.1.100.102。• 邮件域example.com。• 操作权限root或具备sudo权限的管理员账号。• 网络要求软件源可用服务端和客户端互通。• DNS 要求实验环境至少配置mail.example.com的 A 记录和example.com的 MX 记录。hostnamectl set-hostname mail.example.com echo 10.1.100.101 mail.example.com mail /etc/hosts hostname -f ip addr ip route yum repolist示例 DNS 记录mail.example.com. IN A 10.1.100.101 example.com. IN MX 10 mail.example.com.3. 相关知识• Sendmail 是 MTA负责 SMTP 收发和路由Dovecot 是邮件访问服务负责 POP3、IMAP 和认证。• SMTP 常用 TCP 25客户端提交常用 587SMTPS 常用 465。IMAP 使用 143/993POP3 使用 110/995。• Sendmail 默认可能只监听回环地址。允许远程连接前必须检查DAEMON_OPTIONS。• CentOS 7 常默认安装 Postfix。Sendmail 与 Postfix 不能同时占用 TCP 25切换前应停止并禁用 Postfix。•/etc/mail/sendmail.mc是主要模板修改后用make -C /etc/mail生成sendmail.cf不要直接长期维护生成文件。•/etc/mail/access控制允许、拒绝和中继来源。错误配置RELAY会形成开放中继被滥用发送垃圾邮件。• 本实验只允许回环和10.1.100.0/24内网中继不向任意互联网地址开放转发。• MX 记录决定域的收信服务器A 记录解析邮件主机PTR 反向解析、SPF、DKIM、DMARC 影响公网投递信誉。• Sendmail 本地投递默认使用 mbox 邮箱。本文让 Dovecot 读取/var/spool/mail/用户名。• TLS 只加密传输不替代账号权限、中继限制和反垃圾策略。4. 实验步骤4.1 安装软件并处理 MTA 冲突yum -y install sendmail sendmail-cf dovecot mailx openssl systemctl disable --now postfix 2/dev/null || true ss -lntp | grep :25 alternatives --set mta /usr/sbin/sendmail.sendmail alternatives --display mta确认 Sendmail 组件rpm -q sendmail sendmail-cf dovecot mailx sendmail -d0.1 -bv root4.2 创建邮件用户和邮箱useradd alice useradd bob passwd alice passwd bob touch /var/spool/mail/alice /var/spool/mail/bob chown alice:mail /var/spool/mail/alice chown bob:mail /var/spool/mail/bob chmod 660 /var/spool/mail/alice /var/spool/mail/bob检查账号和邮箱id alice id bob ls -l /var/spool/mail/alice /var/spool/mail/bob4.3 配置 Sendmail 基本参数备份配置cp -a /etc/mail/sendmail.mc /etc/mail/sendmail.mc.bak.$(date %F-%H%M%S) cp -a /etc/mail/access /etc/mail/access.bak.$(date %F-%H%M%S)编辑/etc/mail/sendmail.mc。找到仅监听127.0.0.1的行DAEMON_OPTIONS(Portsmtp,Addr127.0.0.1, NameMTA)dnl在行首加dnl注释使 Sendmail 按系统网络配置监听 SMTPdnl DAEMON_OPTIONS(Portsmtp,Addr127.0.0.1, NameMTA)dnl在MAILER定义之前加入本地域和主机名define(confDOMAIN_NAME, mail.example.com)dnl LOCAL_DOMAIN(example.com)dnl MASQUERADE_AS(example.com)dnl FEATURE(masquerade_envelope)dnl把本地域写入/etc/mail/local-host-namesexample.com mail.example.com4.4 配置中继范围编辑/etc/mail/accessConnect:127.0.0.1 RELAY Connect:10.1.100 RELAY生成 access 数据库makemap hash /etc/mail/access.db /etc/mail/accessConnect:10.1.100只适用于本实验网段。生产环境应缩小到实际客户端网段或使用经过认证的 587 提交服务禁止配置Connect:0 RELAY。4.5 生成 TLS 证书实验环境生成自签名证书mkdir -p /etc/pki/mail/private openssl req -new -x509 -nodes -days 3650 -newkey rsa:2048 \ -keyout /etc/pki/mail/private/mail.key \ -out /etc/pki/mail/mail.crt \ -subj /CCN/STLab/LLab/OExample/OUIT/CNmail.example.com chmod 600 /etc/pki/mail/private/mail.key chmod 644 /etc/pki/mail/mail.crt在sendmail.mc的MAILER定义前加入define(confCACERT_PATH, /etc/pki/tls/certs)dnl define(confCACERT, /etc/pki/tls/certs/ca-bundle.crt)dnl define(confSERVER_CERT, /etc/pki/mail/mail.crt)dnl define(confSERVER_KEY, /etc/pki/mail/private/mail.key)dnl生成配置并检查make -C /etc/mail sendmail -C /etc/mail/sendmail.cf -bt -d0.4 /dev/null4.6 配置 Dovecot备份配置目录cp -a /etc/dovecot /etc/dovecot.bak.$(date %F-%H%M%S)编辑/etc/dovecot/dovecot.confprotocols imap pop3 listen *编辑/etc/dovecot/conf.d/10-mail.confmail_location mbox:~/mail:INBOX/var/spool/mail/%u编辑/etc/dovecot/conf.d/10-auth.confdisable_plaintext_auth yes auth_mechanisms plain login编辑/etc/dovecot/conf.d/10-ssl.confssl required ssl_cert /etc/pki/mail/mail.crt ssl_key /etc/pki/mail/private/mail.key检查 Dovecot 最终配置doveconf -n doveconf -n | egrep protocols|listen|mail_location|ssl|auth_mechanisms4.7 配置防火墙并启动服务firewall-cmd --permanent --add-servicesmtp firewall-cmd --permanent --add-serviceimap firewall-cmd --permanent --add-serviceimaps firewall-cmd --permanent --add-servicepop3 firewall-cmd --permanent --add-servicepop3s firewall-cmd --reload systemctl enable --now sendmail dovecot systemctl restart sendmail dovecot不要关闭 SELinux。Sendmail 和 Dovecot 使用系统默认路径时发行版策略通常已包含所需权限。出现拒绝时先检查 AVC 日志。4.8 本机发送测试邮件echo Sendmail local delivery test | mail -s local test alice sleep 2 mail -u alice也可以直接调用 Sendmailsendmail -v aliceexample.com EOF From: rootexample.com To: aliceexample.com Subject: Sendmail SMTP test This is a Sendmail test message. EOF5. 验证结果5.1 验证服务和端口systemctl --no-pager --full status sendmail dovecot ss -lntp | egrep :25|:110|:143|:993|:995 firewall-cmd --list-services预期 Sendmail 监听 TCP 25Dovecot 监听 110、143、993、995若配置ssl required明文端口会要求升级到 TLS。5.2 验证 DNSdig short A mail.example.com dig short MX example.com dig short -x 10.1.100.101内网实验没有反向 DNS 时PTR 可能为空公网邮件服务器应由地址提供商配置正确 PTR。5.3 验证 Sendmail 路由、队列和日志sendmail -bv aliceexample.com mailq mailstats grep -E sendmail|dovecot /var/log/maillog | tail -n 100 journalctl -u sendmail -u dovecot -n 100 --no-pager查看邮箱是否收到邮件ls -lh /var/spool/mail/alice tail -n 40 /var/spool/mail/alice5.4 验证 SMTP 与 TLSopenssl s_client -starttls smtp -connect mail.example.com:25 -servername mail.example.com连接后应看到 Sendmail SMTP 欢迎信息和证书信息。输入EHLO client.example.com可查看服务能力。5.5 验证 Dovecot 认证和收信协议doveadm auth test alice openssl s_client -connect mail.example.com:993 -servername mail.example.com openssl s_client -connect mail.example.com:995 -servername mail.example.comWindows 或其他邮件客户端参数• 收件服务器mail.example.comIMAPS 端口 993或 POP3S 端口 995。• 发件服务器mail.example.comSMTP 端口 25仅限实验网段。• 用户名alice密码Linux 用户密码。• 自签名证书会触发信任提示生产环境应更换受信任证书。服务状态正常、端口监听正确、本地邮件进入/var/spool/mail/alice、Dovecot 认证成功、TLS 握手正常说明 Sendmail 与 Dovecot 邮件系统配置生效。