温馨提示×

centos怎么安装邮件服务器

小樊
43
2026-09-30 02:10:13
栏目: 云计算

在 CentOS 上搭建邮件服务器,常见方案是 Postfix(MTA)+ Dovecot(IMAP/POP3)+ 数据库/系统用户。下面以 CentOS 7/8 为例,给你一个可直接落地的入门级教程(单域、可收发外网邮件)。


一、准备工作

1. 系统要求

  • CentOS 7 或 CentOS 8
  • 有公网 IP
  • 域名(如 example.com)
  • 关闭 SELinux(或配置好策略)
  • 防火墙开放端口
# 临时关闭 SELinux
setenforce 0
# 永久关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

二、DNS 配置(非常重要)

在你的域名解析中配置:

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

四、配置 Postfix(发信 + 收信)

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

# 邮件存储
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

五、配置 Dovecot(收信 IMAP/POP3)

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

九、测试邮件服务器

1. 本地发信

echo "test" | mail -s "hello" user1@example.com

2. 查看邮件

ls ~/Maildir/new

3. 用邮件客户端

  • IMAP: mail.example.com
  • 用户: user1
  • 密码: 设置的系统密码

十、常见问题

1. 收不到外部邮件?

  • 检查 MX 记录
  • 检查 25 端口是否被封
  • 查看日志:
tail -f /var/log/maillog

2. 被当成垃圾邮件?

  • 配置 SPF
  • 配置 DKIM(推荐)
  • 配置 PTR 反向解析

十一、进阶推荐(生产环境)

  • Postfix + MySQL(虚拟用户)
  • DKIM(OpenDKIM)
  • 防垃圾(SpamAssassin)
  • Web 邮箱(Roundcube)
  • Let’s Encrypt(SSL)

如果你愿意,我可以: ✅ 帮你写 CentOS 8 完整脚本
✅ 教你 用 Docker 搭邮件服务器
✅ 配置 企业级虚拟域邮箱

你现在是 学习 / 测试 / 生产环境?

0 踩