温馨提示×

centos怎么搭建邮件服务器

小樊
55
2026-09-11 20:11:08
栏目: 云计算

在 CentOS 上搭建邮件服务器通常涉及 Postfix(SMTP 服务)+ Dovecot(IMAP/POP3 服务)+ 数据库/系统用户 + SSL。下面我以 CentOS 7 / 8 为例,给你一个可落地的通用搭建步骤(以域名 mail.example.com 为例)。


一、前期准备

1. 系统更新

yum update -y
# CentOS 8 用 dnf
dnf update -y

2. 设置主机名

hostnamectl set-hostname mail.example.com

3. 域名解析(非常重要)

在域名 DNS 中添加:

A    mail.example.com    → 服务器IP
MX   example.com        → mail.example.com

(可选)TXT 记录做 SPF:

example.com TXT "v=spf1 mx ~all"

二、安装邮件服务组件

1. 安装 Postfix + Dovecot

yum install -y postfix dovecot cyrus-sasl

三、配置 Postfix(SMTP)

1. 修改主配置文件

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

四、配置 Dovecot(收信)

1. 修改 Dovecot 配置

vi /etc/dovecot/dovecot.conf
protocols = imap pop3
listen = *

2. 配置邮箱位置

vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir

3. 配置 SASL 认证

vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login

4. 配置服务监听

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

客户端配置

  • SMTP: mail.example.com:465 / 587
  • IMAP: mail.example.com:993
  • POP3: mail.example.com:995

八、可选增强(生产环境建议)

  • ✅ 使用 Let’s Encrypt 免费 SSL
  • ✅ 配置 DKIM / DMARC
  • ✅ 使用 MariaDB + PostfixAdmin 管理虚拟用户
  • ✅ 防垃圾邮件(SpamAssassin / fail2ban)

如果你愿意,我可以:

  • 给你 CentOS 8 + PostfixAdmin 虚拟用户方案
  • 或 Docker 一键邮件服务器
  • 或 只收信 / 只发信场景

你打算用在 学习 / 公司内网 / 公网生产 哪种场景?

0 踩