在 CentOS 上搭建邮件服务器,常见方案是 Postfix(MTA)+ Dovecot(IMAP/POP3)+ 数据库/系统用户。下面以 CentOS 7/8 为例,给你一个可直接落地的入门级教程(单域、可收发外网邮件)。
example.com)# 临时关闭 SELinux
setenforce 0
# 永久关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
在你的域名解析中配置:
example.com. MX 10 mail.example.com.
mail.example.com. A 你的公网IP
(可选,但强烈建议)
example.com. TXT "v=spf1 mx ~all"
yum install -y postfix dovecot cyrus-sasl
CentOS 8 用:
dnf install -y postfix dovecot cyrus-sasl
vi /etc/postfix/main.cf
关键配置:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 邮件存储
home_mailbox = Maildir/
# SASL 认证
smtpd_sasl_type = cyrus
smtpd_sasl_path = smtpd
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination
vi /etc/dovecot/dovecot.conf
protocols = imap pop3
listen = *
vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
使用系统用户(最简单):
useradd -m user1
passwd user1
systemctl enable postfix dovecot
systemctl start postfix dovecot
firewall-cmd --permanent --add-port=25/tcp
firewall-cmd --permanent --add-port=110/tcp
firewall-cmd --permanent --add-port=143/tcp
firewall-cmd --reload
echo "test" | mail -s "hello" user1@example.com
ls ~/Maildir/new
mail.example.comuser1tail -f /var/log/maillog
如果你愿意,我可以:
✅ 帮你写 CentOS 8 完整脚本
✅ 教你 用 Docker 搭邮件服务器
✅ 配置 企业级虚拟域邮箱
你现在是 学习 / 测试 / 生产环境?