22- 邮件服务基础(Postfix + Dovecot) ✉️
前提条件 ✅
- 已完成 Web 服务(Apache2/Nginx)和数据库基础(MariaDB/PostgreSQL)
- 以 ops 用户登录(具有 sudo 权限)
- 系统运行在 Debian 12 Bookworm
- 防火墙已放行邮件相关端口(25/tcp SMTP、465/tcp SMTPS、587/tcp Submission、143/tcp IMAP、993/tcp IMAPS、110/tcp POP3、995/tcp POP3S)
- 域名已解析(建议有真实域名 + MX 记录测试,学习环境可用自建域名或 localhost 测试)
- 全局约定:主机名 LinuxDC,IP 192.168.1.100
详细步骤 🛠️
- 安装 Postfix(SMTP 发送服务器)
安装:
sudo apt update
sudo apt install -y postfix postfix-mysql
安装向导选择: - General type of mail configuration:Internet Site - System mail name:mail.linuxdc.local(或你的真实域名,如 mail.example.com)
基本配置(编辑 /etc/postfix/main.cf):
sudo nano /etc/postfix/main.cf
关键修改/添加:
myhostname = mail.linuxdc.local
mydomain = linuxdc.local
myorigin = $mydomain
inet_interfaces = all
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.1.0/24
smtpd_banner = $myhostname ESMTP $mail_name
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_security_level = may
smtp_tls_security_level = may
重启:
sudo systemctl restart postfix
sudo systemctl status postfix
- 安装 Dovecot(IMAP/POP3 接收服务器)
安装:
sudo apt install -y dovecot-core dovecot-imapd dovecot-pop3d dovecot-mysql
配置基本文件:
sudo nano /etc/dovecot/conf.d/10-mail.conf
修改:
mail_location = maildir:~/Maildir
认证配置(使用系统用户):
sudo nano /etc/dovecot/conf.d/10-auth.conf
确保:
disable_plaintext_auth = no # 学习环境,生产建议 yes + SSL
auth_mechanisms = plain login
SSL 配置(使用自签名证书测试):
sudo nano /etc/dovecot/conf.d/10-ssl.conf
ssl = required
ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem
ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key
重启:
sudo systemctl restart dovecot
sudo systemctl status dovecot
- 创建测试邮箱用户
使用系统用户(简单方式):
sudo adduser mailuser
# 设置密码
测试发送邮件(本地):
echo "Test body" | mail -s "Test Subject" mailuser@localhost
检查接收:
sudo su - mailuser
mail
- 使用 TLS + Submission 端口(587)安全发送
编辑 Postfix master.cf:
sudo nano /etc/postfix/master.cf
取消注释或添加:
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
重启 Postfix。
- 客户端测试(推荐 Thunderbird 或 Outlook)
- IMAP:服务器 mail.linuxdc.local,端口 993(SSL/TLS)
- SMTP:服务器 mail.linuxdc.local,端口 587(STARTTLS),认证 mailuser + 密码
-
POP3:端口 995(SSL/TLS)
-
防火墙与安全加固
sudo ufw allow Postfix sudo ufw allow Dovecot sudo ufw reload
生产建议: - 使用 Let's Encrypt 证书替换自签名 - 启用 SPF、DKIM、DMARC(后续章节可扩展) - 限制 mynetworks,只允许信任 IP - 结合 fail2ban 监控 /var/log/mail.log
实践任务 🎯
- 安装 Postfix + Dovecot,完成安全初始化
- 创建 mailuser 用户,发送测试邮件并用 mail 命令查看
- 配置 submission 端口 587 + TLS,尝试用 Thunderbird 客户端连接接收/发送
- 查看 /var/log/mail.log,确认有正常登录和投递记录
自测问题 ❓
- Postfix 的 mydestination 和 mynetworks 参数分别控制什么?
- Dovecot 中 mail_location = maildir:~/Maildir 的含义是什么?
- 为什么生产环境推荐使用 587 端口(submission)而非 25 端口发送邮件?
- 如何让 Postfix 只接受本地或信任网段的邮件中继?
总结 📌
Postfix + Dovecot 是 Debian 上经典的开源邮件服务器组合。
本章完成了基本 SMTP 发送 + IMAP/POP3 接收环境搭建,支持 TLS 加密和系统用户认证。
后续可扩展 DKIM/SPF、防垃圾邮件(SpamAssassin)、Webmail(如 Roundcube)等,形成完整邮件系统。