设置 Postfix MTA 和 IMAP/POP3

为了从我们的 CentOS 7 服务器发送电子邮件,我们需要设置现代邮件传输代理 (MTA)。邮件传输代理是负责通过 SMTP 为系统用户或企业 Internet 域发送出站邮件的守护进程。

值得注意的是,本教程仅教授设置守护进程以供本地使用的过程。我们不会详细介绍为业务运营设置 MTA 的高级配置。这是许多技能的组合,包括但不限于:DNS、获取未被列入黑名单的静态可路由 IP 地址以及配置高级安全和服务设置。简而言之,本教程旨在让您熟悉基本配置。请勿使用本教程进行面向 Internet 的主机的 MTA 配置。

由于其兼顾安全性和易于管理,我们选择 Postfix 作为本教程的 MTA。在旧版本的 CentOS 中安装的默认 MTA 是 SendmailSendmail 是一款出色的 MTA。但是,以作者的拙见,Postfix 在解决 MTA 的以下注意事项时达到了最佳效果。在最新版本的 CentOS 中,Postfix 已取代 Sendmail 成为默认 MTA。

Postfix 是一种广泛使用且有据可查的 MTA。它得到了积极的维护和开发。它需要的配置最少(这只是电子邮件),并且系统资源效率高(再次强调,这只是电子邮件)。

步骤 1 − 从 YUM 包管理器安装 Postfix。

[root@centos]# yum -y install postfix

步骤 2 −配置 Postfix 配置文件。

Postfix 配置文件位于:/etc/postfix/main.cf

在简单的 Postfix 配置中,必须为特定主机配置以下内容:主机名、域、原点、inet_interfaces 和目标。

配置主机名 − 主机名是 Postfix 主机的完全限定域名。在 OpenLDAP 章节中,我们将 CentOS 框命名为:域 vmnet.local 上的 centos。让我们在本章中坚持这一点。

# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
myhostname = centos.vmnet.local

配置域 − 如上所述,我们将在本教程中使用的域是 vmnet.local

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
mydomain = vmnet.local

配置 origin − 对于单个服务器和域设置,我们只需取消注释以下部分并保留默认的 Postfix 变量。

# SENDING MAIL
#
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to append $myhostname,
# which is fine for small sites.  If you run a domain with multiple
# machines, you should (1) change this to $mydomain and (2) set up
# a domain-wide alias database that aliases each user to
# user@that.users.mailhost.
#
# For the sake of consistency between sender and recipient addresses,
# myorigin also specifies the default domain name that is appended
# to recipient addresses that have no @domain part. 
#
myorigin = $myhostname
myorigin = $mydomain

配置网络接口 − 我们将让 Postfix 监听我们的单个网络接口以及与该接口关联的所有协议和 IP 地址。只需保留 Postfix 的默认设置即可。

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes. 
#
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost
# Enable IPv4, and IPv6 if supported
inet_protocols = all

步骤 3 − 为 Postfix 配置 SASL 支持。

如果没有 SASL 身份验证支持,Postfix 将仅允许本地用户发送电子邮件。否则,当用户发送电子邮件离开本地域时,它将给出 relaying denied 错误。

注意SASLSimple Application Security Layer Framework 是一个用于支持不同应用层协议之间的不同技术的身份验证的框架。SASL 开发人员(和消费者)不会将身份验证机制留给应用层协议,而是利用当前的身份验证协议来实现更高级别的协议,而这些协议可能没有内置便利性或更安全的身份验证(当谈到访问安全服务时)。

安装"cyrus-sasl* 包

[root@centos]# yum -y install  cyrus-sasl 
Loaded plugins: fastestmirror, langpacks 
Loading mirror speeds from cached hostfile 
 * base: repos.forethought.net 
 * extras: repos.dfw.quadranet.com 
 * updates: mirrors.tummy.com 
Package cyrus-sasl-2.1.26-20.el7_2.x86_64 already installed and latest version
Nothing to do

为 SASL Auth 配置 /etc/postfix/main.cf

smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_sasl_security_options = noanonymous
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

我的 SASL 选项在 main.conf

##配置 SASL 选项条目:
smtpd_sasl_auth_enable = yes
smptd_recipient_restrictions =
permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtp_sasl_type = dovecot
smtp_sasl_path = private/auth/etc

步骤 4 − 配置 FirewallD 以允许传入 SMTP 服务。

[root@centos]# firewall-cmd --permanent --add-service=smtp 
success

[root@centos]# firewall-cmd --reload 
success

[root@centos]#

现在让我们检查以确保我们的 CentOS 主机允许并响应端口 25(SMTP)上的请求。

Nmap scan report for 172.16.223.132 
Host is up (0.00035s latency). 
Not shown: 993 filtered ports 
PORT    STATE  SERVICE 
   20/tcp  closed ftp-data 
   21/tcp  open   ftp 
   22/tcp  open   ssh 
   25/tcp  open   smtp 
   80/tcp  open   http 
   389/tcp open   ldap 
   443/tcp open   https 
MAC Address: 00:0C:29:BE:DF:5F (VMware)

如您所见,SMTP 正在监听,守护进程正在响应来自我们内部 LAN 的请求。

安装 Dovecot IMAP 和 POP3 服务器

Dovecot 是一个安全的 IMAP 和 POP3 服务器,旨在处理从小型到大型组织的传入邮件需求。由于它在 CentOS 中的广泛使用,我们将使用 Dovecot 作为安装和配置 CentOS 和 MTA SASL 提供程序的传入邮件服务器的示例。

如前所述,我们不会为 DNS 配置 MX 记录或创建安全规则以允许我们的服务处理域的邮件。因此,仅在面向 Internet 的主机上设置这些服务可能会为没有 SPF 记录的安全漏洞留下杠杆空间。

步骤 1 −安装 Dovecot。

[root@centos]# yum -y install dovecot

第 2 步 − 配置 dovecot。

dovecot 的主配置文件位于:/etc/dovecot.conf。我们将首先备份主配置文件。在进行编辑之前始终备份配置文件是一种很好的做法。这样,id(例如)换行符就会被文本编辑器破坏,多年的更改就会丢失。恢复很容易,只需将当前备份复制到生产中即可。

dovecot 启用协议和守护进程服务>

# Protocols we want to be serving. 
protocols = imap imaps pop3 pop3s

现在,我们需要启用 dovecot 守护进程在启动时进行监听 −

[root@localhost]# systemctl start  dovecot 
[root@localhost]# systemctl enable dovecot

让我们确保 Dovecot 在本地监听以下指定端口:imap、pop3、imap 安全以及 pop3 安全。

[root@localhost]# netstat -antup | grep dovecot 
 tcp        0        0 0.0.0.0:110        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:143        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:993        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:995        0.0.0.0:*        LISTEN        4368/dovecot
 tcp6       0        0 :::110                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::143                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::993                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::995                :::*          LISTEN        4368/dovecot

[root@localhost]#

如图所示,dovecot 正在监听 IPv4 和 IPv4 的指定端口。

POP3 110
POP3s 995
IMAP 143
IMAPs 993

现在,我们需要制定一些防火墙规则。

[root@localhost]# firewall-cmd --permanent --add-port=110/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=143/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=995/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=993/tcp 
success
 
[root@localhost]# firewall-cmd --reload 
success
 
[root@localhost]#

我们的传入邮件服务器正在接受对 LAN 上的主机的 POP3POP3sIMAPIMAPs 请求。

Port Scanning host: 192.168.1.143

   Open TCP Port:   21          ftp 
   Open TCP Port:   22          ssh 
   Open TCP Port:   25          smtp 
   Open TCP Port:   80          http 
   Open TCP Port:   110         pop3 
   Open TCP Port:   143         imap 
   Open TCP Port:   443         https 
   Open TCP Port:   993         imaps 
   Open TCP Port:   995         pop3s