22. 邮件服务基础
本章节指导在 RHEL 9(主机名 LinuxDC)上使用 SecureCRT 配置邮件服务,配合 SecureFX 传输配置文件。内容涵盖 Postfix(SMTP)、Dovecot(IMAP/POP3)、邮件中继及 SPF/DKIM/DMARC 安全配置,以测试邮件发送为例,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 LinuxDC 环境中测试,确保实验一致性。
22.1 前提条件
- RHEL 9 已安装(参考第 1 章),主机名设置为
LinuxDC。 - 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 9 章)。
- 使用 SecureFX 传输文件(SFTP 协议)。
- 具有 root 或 sudo 权限。
- 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 2 和第 5 章)。
- 防火墙和 SELinux 启用(参考第 12 章)。
- 网络连接正常,时间同步配置完成(参考第 4 章)。
- 域名配置完成(示例:
mail.example.com)。
💡 提示:邮件服务需公网 IP 和 DNS 记录以避免垃圾邮件过滤。在 RHEL 10 中,Postfix 和 Dovecot 支持更多内置加密和自动化配置,提升安全部署效率。
22.2 邮件服务器配置
22.2.1 配置 Postfix (SMTP)
-
安装 Postfix:
dnf install -y postfix # 安装 Postfix SMTP 服务器 systemctl enable --now postfix # 启用并启动 Postfix 服务 -
配置主文件:
vim /etc/postfix/main.cf # 修改关键参数: myhostname = mail.example.com mydomain = example.com myorigin = $mydomain inet_interfaces = all mydestination = $myhostname, localhost.$mydomain, localhost mynetworks = 127.0.0.0/8 192.168.1.0/24 # 允许中继网络 -
重启服务:
systemctl restart postfix -
防火墙配置:
firewall-cmd --permanent --add-service=smtp firewall-cmd --reload
🧠 知识点:Postfix 是 MTA(Mail Transfer Agent),处理邮件发送。测试:
telnet localhost 25。✅ 检查点:
postconf -n显示配置,mail -s "Test" user@example.com < /dev/null发送测试邮件。
22.2.2 配置 Dovecot (IMAP/POP3)
-
安装 Dovecot:
dnf install -y dovecot # 安装 Dovecot IMAP/POP3 服务器 systemctl enable --now dovecot # 启用并启动 Dovecot 服务 -
配置主文件:
vim /etc/dovecot/dovecot.conf # 添加/修改: protocols = imap pop3 listen = * -
配置认证:
vim /etc/dovecot/conf.d/10-auth.conf # 修改: disable_plaintext_auth = yes auth_mechanisms = plain login -
防火墙配置:
firewall-cmd --permanent --add-service=imap firewall-cmd --permanent --add-service=pop3 firewall-cmd --reload
💡 提示:Dovecot 支持 PAM 认证。测试:
telnet localhost 143(IMAP) 或 110 (POP3)。✅ 检查点:
doveconf -n显示配置,客户端如 Thunderbird 连接成功。
22.3 邮件中继配置
-
配置中继到外部 SMTP(示例:Gmail):
vim /etc/postfix/main.cf # 添加: relayhost = [smtp.gmail.com]:587 smtp_use_tls = yes smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous -
创建密码文件:
vim /etc/postfix/sasl_passwd # 添加: [smtp.gmail.com]:587 yourgmail@gmail.com:yourpassword postmap /etc/postfix/sasl_passwd # 生成 hash 文件 chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db -
重启 Postfix:
systemctl restart postfix
🧠 知识点:中继避免本地 IP 被黑名单。测试:
echo "Test body" | mail -s "Test subject" recipient@example.com。
22.4 安全配置(SPF/DKIM/DMARC)
22.4.1 配置 SPF
- DNS TXT 记录(外部 DNS 管理):
TXT record: v=spf1 a mx include:_spf.google.com ~all
22.4.2 配置 DKIM
-
安装 OpenDKIM:
dnf install -y opendkim # 安装 OpenDKIM systemctl enable --now opendkim # 启用并启动 -
生成密钥:
opendkim-genkey -s mail -d example.com -D /etc/opendkim/keys/ # 生成密钥 chown opendkim:opendkim /etc/opendkim/keys/mail.private -
配置 OpenDKIM:
vim /etc/opendkim.conf # 修改: Mode signing KeyTable /etc/opendkim/KeyTable SigningTable refile:/etc/opendkim/SigningTable ExternalIgnoreList refile:/etc/opendkim/TrustedHosts InternalHosts refile:/etc/opendkim/TrustedHosts -
配置 KeyTable:
vim /etc/opendkim/KeyTable # 添加: mail._domainkey.example.com example.com:mail:/etc/opendkim/keys/mail.private -
配置 SigningTable:
vim /etc/opendkim/SigningTable # 添加: *@example.com mail._domainkey.example.com -
配置 TrustedHosts:
vim /etc/opendkim/TrustedHosts # 添加: 127.0.0.1 localhost LinuxDC example.com -
集成 Postfix:
vim /etc/postfix/main.cf # 添加: milter_default_action = accept milter_protocol = 6 smtpd_milters = unix:/run/opendkim/opendkim.sock non_smtpd_milters = $smtpd_milters -
重启服务:
systemctl restart opendkim postfix -
DNS TXT 记录:
mail._domainkey TXT v=DKIM1; k=rsa; p=<public key from mail.txt>
22.4.3 配置 DMARC
- DNS TXT 记录:
_dmarc TXT v=DMARC1; p=quarantine; rua=mailto:admin@example.com
✅ 检查点:使用工具如 dkimvalidator.com 测试邮件签名。
💡 提示:SPF/DKIM/DMARC 防止邮件伪造。在 RHEL 10 中,OpenDKIM 支持更多自动化密钥轮换。
22.5 常见问题与排查
🔍 故障排查:
-
问题 1:邮件发送失败
解决:journalctl -u postfix,检查 main.cf、防火墙(firewall-cmd --list-services)。 -
问题 2:IMAP/POP3 连接错误
解决:journalctl -u dovecot,验证端口(ss -tuln | grep 143)。 -
问题 3:DKIM 签名无效
解决:检查密钥权限(ls -l /etc/opendkim/keys),日志(journalctl -u opendkim),DNS(dig TXT mail._domainkey.example.com)。
22.6 实践任务
- 使用 SecureCRT 通过 SSH(端口 2222)登录
LinuxDC,安装 Postfix 并配置域名mail.example.com。 - 配置 Dovecot 支持 IMAP 和 POP3,测试连接。
- 设置 Postfix 邮件中继到
smtp.gmail.com。 - 配置 DKIM 并验证 DNS 记录。
✅ 检查点:邮件发送/接收正常,安全配置验证通过。
22.7 自测问题
-
问题 1:如何配置 Postfix 中继到外部 SMTP 服务器?
答案:编辑/etc/postfix/main.cf,添加relayhost = [smtp.gmail.com]:587和 SASL 配置,然后systemctl restart postfix。 -
问题 2:如何测试 IMAP 连接?
答案:telnet localhost 143 -
问题 3:如何生成 DKIM 密钥?
答案:opendkim-genkey -s mail -d example.com -D /etc/opendkim/keys/
🧾 总结:
本章完整介绍了 RHEL 9 邮件服务基础的配置,包括 Postfix、Dovecot 和安全机制的设置。这些技能扩展业务应用,为后续 VPN 和高可用配置提供通信支持。