主题
CentOS 7 邮件服务部署指南
🚀 一、部署步骤
1. 系统环境检查
bash
# 检查系统版本
cat /etc/redhat-release
# 检查是否已安装邮件服务
rpm -qa | grep postfix
rpm -qa | grep dovecot1
2
3
4
5
2
3
4
5
2. 安装邮件服务器组件
安装Postfix(SMTP服务器)和Dovecot(POP3/IMAP服务器)
bash
# 切换成root用户
su - root
# 安装Postfix和Dovecot
yum -y install postfix dovecot
# 验证安装
rpm -qa | grep postfix
rpm -qa | grep dovecot1
2
3
4
5
6
7
2
3
4
5
6
7
输出示例:
postfix-2.10.1-9.el7.x86_64
dovecot-2.2.36-6.el7.x86_641
2
2
3. 配置主机名和DNS
bash
# 设置主机名(重要!)
hostnamectl set-hostname mail.example.com
# 编辑hosts文件
vim /etc/hosts1
2
3
4
5
2
3
4
5
在hosts文件中添加:
192.168.1.100 mail.example.com mail1
bash
# 验证主机名
hostname
hostname -f1
2
3
2
3
4. 配置Postfix
bash
# 备份原始配置文件
cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
# 编辑Postfix主配置文件
vim /etc/postfix/main.cf1
2
3
4
5
2
3
4
5
需要修改和确认的关键配置项:
ini
# 修改服务器主机名
myhostname = mail.example.com
# 修改域名
mydomain = example.com
# 设置发件人域名
myorigin = $mydomain
# 设置监听地址(允许所有接口)
inet_interfaces = all
# 设置监听端口
inet_protocols = ipv4
# 设置本地邮件域名
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 设置本地网络(允许哪些网络使用本邮件服务器)
mynetworks = 192.168.1.0/24, 127.0.0.0/8
# 设置邮件存储位置
home_mailbox = Maildir/
# 设置邮件大小限制(默认10MB,可根据需要调整)
message_size_limit = 10240000
# 设置邮箱大小限制(默认1GB)
mailbox_size_limit = 1073741824
# 启用SMTP认证
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5. 配置Dovecot
bash
# 备份原始配置文件
cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak
# 编辑Dovecot主配置文件
vim /etc/dovecot/dovecot.conf1
2
3
4
5
2
3
4
5
添加/修改以下配置:
ini
# 启用协议
protocols = imap pop3
# 监听所有接口
listen = *, ::1
2
3
4
5
2
3
4
5
bash
# 编辑10-mail.conf配置文件
vim /etc/dovecot/conf.d/10-mail.conf1
2
2
修改邮件位置:
ini
# 设置邮件存储位置
mail_location = maildir:~/Maildir1
2
2
bash
# 编辑10-auth.conf配置文件
vim /etc/dovecot/conf.d/10-auth.conf1
2
2
修改认证配置:
ini
# 禁用明文认证(生产环境推荐)
disable_plaintext_auth = yes
# 允许明文认证(测试环境)
auth_mechanisms = plain login1
2
3
4
2
3
4
bash
# 编辑10-master.conf配置文件
vim /etc/dovecot/conf.d/10-master.conf1
2
2
在service auth部分添加:
ini
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
}1
2
3
4
5
6
7
2
3
4
5
6
7
6. 创建系统邮箱用户
bash
# 创建邮件用户
useradd -s /sbin/nologin user1
useradd -s /sbin/nologin user2
# 设置用户密码
echo "user1:123456" | chpasswd
echo "user2:123456" | chpasswd
# 为用户创建Maildir目录
mkdir -p /home/user1/Maildir
mkdir -p /home/user2/Maildir
chmod -R 700 /home/user1/Maildir
chmod -R 700 /home/user2/Maildir
chown -R user1:user1 /home/user1/Maildir
chown -R user2:user2 /home/user2/Maildir1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
7. 启动邮件服务
bash
# 启动Postfix服务
systemctl start postfix
systemctl enable postfix
# 启动Dovecot服务
systemctl start dovecot
systemctl enable dovecot
# 检查服务状态
systemctl status postfix
systemctl status dovecot1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
8. 防火墙配置
bash
# 开放SMTP端口(25, 587)
firewall-cmd --permanent --add-port=25/tcp
firewall-cmd --permanent --add-port=587/tcp
# 开放POP3端口(110)
firewall-cmd --permanent --add-port=110/tcp
# 开放IMAP端口(143)
firewall-cmd --permanent --add-port/143/tcp
# 重新加载防火墙配置
firewall-cmd --reload
# 验证端口开放
firewall-cmd --list-ports1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
或者直接关闭防火墙:
bash
systemctl stop firewalld
systemctl disable firewalld
setenforce 01
2
3
2
3
🧪 二、部署后测试
1. 服务状态测试
bash
# 检查Postfix服务状态
systemctl status postfix
# 检查Dovecot服务状态
systemctl status dovecot
# 检查端口监听
netstat -tlnp | grep -E ':(25|110|143|587)'
# 检查进程
ps aux | grep postfix
ps aux | grep dovecot1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
预期输出:
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 1234/master
tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 5678/dovecot
tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN 5678/dovecot1
2
3
2
3
2. 本地邮件发送测试
bash
# 使用mail命令发送测试邮件
echo "测试邮件内容" | mail -s "测试邮件主题" user1@localhost
# 查看用户邮件
su - user1
mail
# 输入数字查看邮件,输入q退出1
2
3
4
5
6
7
2
3
4
5
6
7
3. 邮件日志查看
bash
# 查看邮件日志
tail -f /var/log/maillog
# 或使用journalctl
journalctl -u postfix -f
journalctl -u dovecot -f1
2
3
4
5
6
2
3
4
5
6
4. 使用telnet测试SMTP
bash
# 测试SMTP连接
telnet localhost 251
2
2
连接后输入:
EHLO mail.example.com
MAIL FROM: user1@example.com
RCPT TO: user2@example.com
DATA
Subject: 测试邮件
这是一封测试邮件。
.
QUIT1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
5. 使用telnet测试POP3
bash
# 测试POP3连接
telnet localhost 1101
2
2
连接后输入:
USER user1
PASS 123456
LIST
RETR 1
QUIT1
2
3
4
5
2
3
4
5
📊 三、常用管理命令
服务管理
bash
# 启动Postfix
sudo systemctl start postfix
# 停止Postfix
sudo systemctl stop postfix
# 重启Postfix
sudo systemctl restart postfix
# 查看Postfix状态
sudo systemctl status postfix
# 启动Dovecot
sudo systemctl start dovecot
# 停止Dovecot
sudo systemctl stop dovecot
# 重启Dovecot
sudo systemctl restart dovecot
# 查看Dovecot状态
sudo systemctl status dovecot1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
邮件队列管理
bash
# 查看邮件队列
mailq
# 强制发送队列中的邮件
postqueue -f
# 删除队列中的所有邮件
postsuper -d ALL
# 删除特定邮件
postsuper -d <邮件ID>
# 查看邮件队列详细信息
postqueue -p1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
邮件日志查看
bash
# 查看邮件日志
sudo tail -f /var/log/maillog
# 查看发送失败的邮件
grep -i "error" /var/log/maillog
# 查看邮件统计
grep "status=sent" /var/log/maillog | wc -l
grep "status=bounced" /var/log/maillog | wc -l1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
邮件用户管理
bash
# 创建邮件用户
useradd -s /sbin/nologin username
echo "username:password" | chpasswd
# 删除邮件用户
userdel username
# 修改用户密码
passwd username
# 查看邮箱大小
du -sh /home/username/Maildir1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
🔧 四、故障排除
常见问题及解决方案
1. 邮件无法发送
bash
# 检查Postfix服务状态
sudo systemctl status postfix
# 检查邮件日志
sudo tail -f /var/log/maillog
# 检查配置文件
postconf -n
# 检查DNS解析
nslookup mail.example.com
dig mx example.com1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
2. 无法收信
bash
# 检查Dovecot服务状态
sudo systemctl status dovecot
# 检查Dovecot配置
dovecot -n
# 检查用户邮箱权限
ls -la /home/username/Maildir
# 检查POP3/IMAP端口
netstat -tlnp | grep -E ':(110|143)'1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3. 认证失败
bash
# 检查Dovecot认证配置
grep -r "auth_mechanisms" /etc/dovecot/
# 检查用户密码
passwd username
# 查看认证日志
grep "auth" /var/log/maillog1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
4. 邮件被拒收
bash
# 检查发件人IP是否被列入黑名单
# 访问:https://mxtoolbox.com/blacklists.aspx
# 检查DNS记录
dig mx example.com
dig a mail.example.com
# 检查SPF记录(如果配置了)
dig txt example.com | grep v=spf11
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
5. 防火墙阻止连接
bash
# 检查防火墙状态
sudo firewall-cmd --list-all
# 开放必要端口
sudo firewall-cmd --permanent --add-port=25/tcp
sudo firewall-cmd --permanent --add-port=587/tcp
sudo firewall-cmd --permanent --add-port=110/tcp
sudo firewall-cmd --permanent --add-port=143/tcp
sudo firewall-cmd --reload1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
📝 五、客户端配置示例
Outlook配置
接收服务器(IMAP):
- 服务器地址:mail.example.com
- 端口:143
- 加密:无(测试环境)或 STARTTLS(生产环境)
- 用户名:user1
- 密码:用户密码
发送服务器(SMTP):
- 服务器地址:mail.example.com
- 端口:25(或587)
- 加密:无(测试环境)或 STARTTLS(生产环境)
- 认证:是
- 用户名:user1
- 密码:用户密码
Thunderbird配置
- 打开账户设置
- 添加邮件账户
- 填写服务器信息:
- IMAP服务器:mail.example.com,端口143
- SMTP服务器:mail.example.com,端口25
- 完成配置
⚠️ 六、安全建议
生产环境安全配置
bash
# 1. 启用TLS加密
yum install -y mod_ssl
# 编辑Postfix配置
vim /etc/postfix/main.cf1
2
3
4
5
2
3
4
5
添加TLS配置:
ini
# TLS配置
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_security_level = may
smtpd_tls_protocols = !SSLv2, !SSLv31
2
3
4
5
2
3
4
5
bash
# 2. 生成自签名证书
cd /etc/pki/tls/certs
make postfix.pem1
2
3
2
3
bash
# 3. 配置SPF记录(在DNS服务器上添加)
# example.com. IN TXT "v=spf1 mx -all"
# 4. 配置DKIM(可选)
yum install -y opendkim1
2
3
4
5
2
3
4
5
访问控制
bash
# 限制SMTP访问
vim /etc/postfix/main.cf1
2
2
添加:
ini
# 只允许特定IP发送邮件
smtpd_client_restrictions =
permit_mynetworks,
reject
# 限制邮件大小
message_size_limit = 10485760 # 10MB
# 限制收件人数量
smtpd_recipient_limit = 1001
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
完成以上步骤后,邮件服务器就成功部署在CentOS 7上了!