CentOS/RedHat下Postfix邮件服务器搭建

验证互联网访问权限


1
2
echo 'nameserver 114.114.114.114' >> /etc/resolv.conf
ping www.baidu.com

说明
邮件服务器需要能够访问互联网,若无法访问请先开通相关策略再继续下面的操作

基础配置


CentOS/RedHat6.x

1
2
3
4
5
6
iptables -F
service iptables stop
chkconfig iptables off
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
reboot

CentOS/RedHat7.x

1
2
3
4
5
6
iptables -F
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
reboot

设置主机名并添加本地host解析


CentOS/RedHat6.x

1
2
3
4
5
6
hostname <postfix.example.com>
# 永久设置需修改/etc/sysconfig/network中的HOSTNAME参数

# 添加本地解析
cat /etc/hosts
x.x.x.x <postfix.example.com>

CentOS/RedHat7.x

1
2
3
4
5
hostnamectl set-hostname <postfix.example.com>

# 添加本地解析
cat /etc/hosts
x.x.x.x <postfix.example.com>

安装postfix


1
yum install postfix -y

配置postfix

1
2
3
4
5
6
7
8
cp -a /etc/postfix/main.cf /etc/postfix/main.cf.bak
postconf -e myhostname=postfix.example.com
postconf -e inet_interfaces=all
postconf -e inet_protocols=ipv4
postconf -e mydestination=$myhostname,localhost.$mydomain,localhost
postconf -e unknown_local_recipient_reject_code=550
postconf -e mynetworks_style=subnet
postconf -e mynetworks=172.18.0.0/16,127.0.0.0/8

配置参数说明:
myhostname:主机名
inet_interfaces:监听的网卡接口
inet_protocols:设置postfix只使用IPv4
mydestination:可接受邮件地址域名
unknown_local_recipient_reject_code:当客户端试图寄信给不存在的本地网域用户时,postfix用于拒绝客户端的smtp 响应码
mynetworks_style:设置局域网网络类型,默认为subnetsubnet代表是子网络,当设定为subnet时,postfix将会自行根据ifconfig上登记的IP和网络屏蔽作运算,自动求出子网范围;如果设定成class,则是不理会屏蔽,自动信任同一个class等级的计算机,如果该服务器使用拨接上网,这样设定将使得同一家ISP的拨接用户都可以利用本服务器转信,这是非常危险的(除非你是ISP公司);设定host则仅该单机可以寄信。
mynetworks:需要收发的客户端的地址,根据实际环境修改

启动postfix


CentOS/RedHat6.x

1
2
service postfix start
chkconfig postfix on

CentOS/RedHat7.x

1
2
systemctl start postfix
systemctl enable postfix

测试发送邮件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum install telnet -y
telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 postfix.example.com ESMTP Postfix
mail from: test@postfix.com //发件人邮箱
250 2.1.0 Ok
rcpt to: 379148058@qq.com //收件人邮箱
250 2.1.5 Ok
data //邮件内容
354 End data with <CR><LF>.<CR><LF>
test mail
. //以“.”结束
250 2.0.0 Ok: queued as 6D75940F10
quit //退出
221 2.0.0 Bye
Connection closed by foreign host.

发送完成后登录收件人邮箱查看是否正常接收到测试邮件。

说明
若在收件箱中未查到到邮件,可先确认是否在垃圾箱中,如果没有则在邮件服务器上执行mailq查看邮件发送进度和相关报错信息进一步排查。