Appearance
23. 邮件服务基础
本章节指导在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 配置邮件服务,配合 SecureFX 传输配置文件。内容涵盖 Postfix(SMTP)、Dovecot(IMAP/POP3)、邮件中继及 SPF/DKIM/DMARC 安全配置,以测试邮件发送为例,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 ZSLinux 环境中测试,确保实验一致性。
23.1 前提条件
- RHEL 8.10 已安装(参考第 1 章),主机名设置为
ZSLinux. - 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 6 章).
- 使用 SecureFX 传输文件(SFTP 协议).
- 具有 root 或 sudo 权限.
- 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 4 和 7 章).
- 防火墙和 SELinux 启用(参考第 13 章).
- 网络连接正常,时间同步配置完成(参考第 3 章).
- 域名配置完成(示例:
mail.example.com).
23.2 邮件服务器配置
23.2.1 配置 Postfix
- 安装 Postfix:bash
dnf install -y postfix # 安装 Postfix 邮件服务器 systemctl enable --now postfix # 启用并启动 Postfix 服务 - 配置 Postfix:bash
vim /etc/postfix/main.cf # 编辑 Postfix 配置文件 # 修改或添加: myhostname = mail.example.com mydomain = example.com myorigin = $mydomain inet_interfaces = all mydestination = $myhostname, localhost.$mydomain, $mydomain systemctl restart postfix # 重启 Postfix 服务 - 配置防火墙和 SELinux:bash
firewall-cmd --permanent --add-service=smtp # 允许 SMTP 端口(25) firewall-cmd --reload # 应用防火墙规则 setsebool -P httpd_can_sendmail 1 # 允许 Web 应用发送邮件
23.2.2 设置邮件中继
- 配置 Postfix 中继到外部服务器(示例:
smtp.gmail.com):bashvim /etc/postfix/main.cf # 编辑 Postfix 配置文件 # 添加: relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = encrypt vim /etc/postfix/sasl_passwd # 创建中继密码文件 # 添加: [smtp.gmail.com]:587 user@gmail.com:password postmap /etc/postfix/sasl_passwd # 生成哈希数据库 chmod 600 /etc/postfix/sasl_passwd # 设置文件权限 systemctl restart postfix # 重启 Postfix 服务
23.2.3 配置 Dovecot
- 安装 Dovecot:bash
dnf install -y dovecot # 安装 Dovecot 邮件访问服务器 systemctl enable --now dovecot # 启用并启动 Dovecot 服务 - 配置 IMAP/POP3:bash
vim /etc/dovecot/conf.d/10-mail.conf # 编辑邮件存储配置 # 修改: mail_location = maildir:/var/mail/%u vim /etc/dovecot/conf.d/10-auth.conf # 编辑认证配置 # 修改: disable_plaintext_auth = yes auth_mechanisms = plain login systemctl restart dovecot # 重启 Dovecot 服务 - 配置防火墙:bash
firewall-cmd --permanent --add-service=imap --add-service=imaps --add-service=pop3 --add-service=pop3s # 允许 IMAP(143, 993)和 POP3(110, 995) firewall-cmd --reload # 应用防火墙规则
23.3 邮件服务安全
23.3.1 配置 SPF/DKIM/DMARC
- 安装 OpenDKIM:bash
dnf install -y opendkim # 安装 OpenDKIM 工具 systemctl enable --now opendkim # 启用并启动 OpenDKIM 服务 - 配置 DKIM:bash
mkdir -p /etc/opendkim/keys # 创建 DKIM 密钥目录 opendkim-genkey -s mail -d example.com -D /etc/opendkim/keys/ # 生成 DKIM 密钥对 chown -R opendkim:opendkim /etc/opendkim/keys # 设置密钥权限 vim /etc/opendkim.conf # 编辑 OpenDKIM 配置文件 # 添加: Domain example.com KeyFile /etc/opendkim/keys/mail.private Selector mail vim /etc/opendkim/TrustedHosts # 编辑信任主机 # 添加: 127.0.0.1 localhost mail.example.com - 集成 DKIM 到 Postfix:bash
vim /etc/postfix/main.cf # 编辑 Postfix 配置文件 # 添加: smtpd_milters = inet:localhost:8891 non_smtpd_milters = $smtpd_milters systemctl restart postfix opendkim # 重启 Postfix 和 OpenDKIM 服务 - 配置 DNS(SPF/DKIM/DMARC):bash
# 示例 DNS 记录(需手动配置 DNS 服务器): # SPF: example.com. 3600 IN TXT "v=spf1 a mx ~all" # DKIM: mail._domainkey.example.com. 3600 IN TXT "v=DKIM1; k=rsa; p=<public_key_from_/etc/opendkim/keys/mail.txt>" # DMARC: _dmarc.example.com. 3600 IN TXT "v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com;"
23.3.2 防止垃圾邮件
- 配置 Postfix 反垃圾邮件:bash
vim /etc/postfix/main.cf # 编辑 Postfix 配置文件 # 添加: smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination smtpd_helo_restrictions = permit_mynetworks, reject_invalid_helo_hostname systemctl restart postfix # 重启 Postfix 服务
23.4 验证
- 验证 Postfix:bash
telnet localhost 25 # 测试 SMTP 连接 mailq # 检查邮件队列 echo "Test email" | mail -s "Test Subject" user@example.com # 发送测试邮件 - 验证 Dovecot:bash
dovecot -n # 验证 Dovecot 配置 telnet localhost 143 # 测试 IMAP 连接 telnet localhost 110 # 测试 POP3 连接 - 验证 SPF/DKIM:bash
cat /etc/opendkim/keys/mail.txt # 检查 DKIM 公钥 dig TXT mail._domainkey.example.com # 检查 DNS DKIM 记录 - 测试 SSH 连接:bash
ssh -p 2222 testuser@ZSLinux # 测试 SSH 连接(参考第 6 章) - 常见问题:
- 邮件发送失败:检查 Postfix 日志(
tail /var/log/maillog)或服务状态(systemctl status postfix)。 - IMAP/POP3 无法连接:验证 Dovecot 配置(
dovecot -n)或防火墙(firewall-cmd --list-services)。 - DKIM 无效:确认 DNS 记录或 OpenDKIM 服务(
systemctl status opendkim)。
- 邮件发送失败:检查 Postfix 日志(
23.5 实践任务
- 使用 SecureCRT 登录
ZSLinux,安装 Postfix 并配置域名mail.example.com. - 配置 Dovecot 支持 IMAP 和 POP3,测试连接。
- 设置 Postfix 邮件中继到
smtp.gmail.com. - 配置 DKIM 并验证 DNS 记录。
23.6 自测问题
- 问题:如何配置 Postfix 中继到外部 SMTP 服务器?
- 答案:编辑
/etc/postfix/main.cf,添加relayhost = [smtp.gmail.com]:587和 SASL 配置,然后systemctl restart postfix.
- 答案:编辑
- 问题:如何测试 IMAP 连接?
- 答案:
telnet localhost 143
- 答案:
- 问题:如何生成 DKIM 密钥?
- 答案:
opendkim-genkey -s mail -d example.com -D /etc/opendkim/keys/
- 答案:
