在 CentOS 上搭建邮件服务器通常涉及 Postfix(SMTP 服务)+ Dovecot(IMAP/POP3 服务)+ 数据库/系统用户 + SSL。下面我以 CentOS 7 / 8 为例,给你一个可落地的通用搭建步骤(以域名 mail.example.com 为例)。
yum update -y
# CentOS 8 用 dnf
dnf update -y
hostnamectl set-hostname mail.example.com
在域名 DNS 中添加:
A mail.example.com → 服务器IP
MX example.com → mail.example.com
(可选)TXT 记录做 SPF:
example.com TXT "v=spf1 mx ~all"
yum 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
# 开启 SASL 认证
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
# TLS
smtpd_tls_cert_file = /etc/pki/tls/certs/mail.crt
smtpd_tls_key_file = /etc/pki/tls/private/mail.key
smtpd_use_tls = yes
# 限制匿名发送
smtpd_recipient_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
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
vi /etc/dovecot/conf.d/10-master.conf
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
useradd -m user1
passwd user1
邮箱地址:
user1@example.com
systemctl enable postfix dovecot
systemctl start postfix dovecot
开放端口:
firewall-cmd --add-service=smtp --permanent
firewall-cmd --add-port=465/tcp --permanent
firewall-cmd --add-port=587/tcp --permanent
firewall-cmd --add-port=110/tcp --permanent
firewall-cmd --add-port=143/tcp --permanent
firewall-cmd --reload
telnet mail.example.com 25
或使用 mail 命令:
echo "test" | mail -s "test" user1@example.com
mail.example.com:465 / 587mail.example.com:993mail.example.com:995如果你愿意,我可以:
你打算用在 学习 / 公司内网 / 公网生产 哪种场景?