65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 Postfix、Dovecot 和 Opendkim 安装和配置电子邮件服务器

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2014年11月28日

CPOL

6分钟阅读

viewsIcon

29949

在本文中,我们将讨论 Postfix、Dovecot 和 DKIM,因此我们将引导您完成在 CentOS 7 系统上使用 Postfix、Dovecot 和 OpenDKIM 安装和配置电子邮件服务器的步骤。

引言

电子邮件服务器是负责在 Internet 上接收和发送电子邮件消息的系统或一组系统。电子邮件服务器有多种类型,例如使用简单邮件传输协议(SMTP)的 SMTP 服务器,该协议用于电子邮件传输。SMTP 服务器通常与 IMAP 或 POP3 服务器结合运行,后者的目的是提供电子邮件检索和/或存储。运行电子邮件服务器并非易事。它需要安装、配置、理解和维护许多不同的服务。

正如您所能想象的,市面上有许多不同的 SMTP、POP3 和 IMAP 服务器。在本文中,我们将讨论 Postfix、Dovecot 和 DKIM,因此我们将引导您完成在 CentOS 7 系统上使用 Postfix、Dovecot 和 OpenDKIM 安装和配置电子邮件服务器的步骤。对于本文,我们使用的是 Rose Hosting 的 Linux VPS 上的 CentOS 7,但您也可以使用任何其他运行 CentOS 7 且最好具有公共 IP 地址的系统。

在继续之前,建议您验证您的主机/域名是否为有效的 FQDN(完全限定域名)并且具有有效的 MX DNS 记录。为此,您可以使用 dig 等工具。如果系统中尚未安装 dig,请运行以下命令进行安装:

# if !type -path "dig" > /dev/null 2>&1; then yum install bind-utils -y; fi

在我们的例子中,电子邮件服务器的主机名是 galaxy.mydomain.com,域名是 mydomain.com。该域名具有以下 MX 记录:

# dig MX mydomain.com @4.2.2.2 +short
0 mydomain.com.

这会告知 Internet 上的所有人,解析 mydomain.com 的计算机将负责 mydomain.com 的电子邮件。

还建议电子邮件服务器的公共 IP 地址具有与电子邮件服务器主机名匹配的有效 rDNS(反向 DNS)记录。您可以使用 dig 进行验证:

# dig -x 1.2.3.4 +short
galaxy.mydomain.com.

访问您的服务器

要完成本文,您需要在 CentOS 系统上拥有 root 访问权限(或 sudo 权限)。因此,请使用您喜欢的 SSH 客户端连接到您的服务器。在类 Unix 操作系统中,您可以打开终端并执行:

# sshroot@YOUR_SERVER_IP -p 22

注意:如果 SSH 侦听的端口不是默认端口,请确保更改端口。同时,请务必将 YOUR_SERVER_IP 替换为您的实际服务器 IP 地址。

一旦您登录到 CentOS 7 系统,请使用 yum 安装(如果尚未安装)名为 screen 的工具:

# yum install screen

并使用以下命令启动一个新的 screen 会话:

# screen -U -S postfix-dovecot-dkim

更新系统

进入 screen 会话后,最好确保您的系统已完全更新。因此,请运行以下 yum 命令来更新您的 CentOS 7:

# yum update

注意:如果进行了内核升级,建议重启系统。

SSL 证书

您需要一个 SSL 证书来确保电子邮件服务器的安全,并能够通过 SSL 与其他服务器或客户端进行通信。在我们的示例中,我们使用的是自签名证书,可以使用以下命令生成:

# yum install openssl
# mkdir -p /root/SSL/mydomain.com
# cd /root/SSL/mydomain.com

# opensslgenrsa -out mydomain.com.key 2048
# opensslreq -new -x509 -nodes -days 365 -key mydomain.com.key -out mydomain.com.crt

输入您的 SSL 证书详细信息,例如国家、城市、通用名称等。例如:

Country Name (2 letter code) [XX]:US
State or Province Name (full name) []:Oregon
Locality Name (eg, city) [Default City]:Portland
Organization Name (eg, company) [Default Company Ltd]:E-Mail Dept.
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:mydomain.com

获取证书和密钥后,使用以下命令将它们分别复制到 /etc/pki/tls/certs//etc/pki/tls/private/

# cp -av mydomain.com.crt /etc/pki/tls/certs/
# cp -avmydomain.com.key /etc/pki/tls/private/

安装 Dovecot

在安装 Dovecot 之前,让我们先简单介绍一下。什么是 Dovecot?它是一个 POP3 和 IMAP 服务器,为 Thunderbird 或 Outlook 等邮件用户代理(MUA)提供一种访问电子邮件服务器上电子邮件的方式。

使用 yum 安装 dovecot:

# yum install dovecot

安装完成后,您需要编辑 /etc/dovecot 中的一些 Dovecot 配置文件,并添加/编辑一些配置参数。让我们从 /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/20-imap.conf 开始,在那里我们将设置查找电子邮件的邮件位置:

# vim +/mail_location /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir
# vim /etc/dovecot/conf.d/20-imap.conf
protocolimap {
mail_location = maildir:~/Maildir
}

接下来,编辑 /etc/dovecot/conf.d/10-ssl.conf 并设置以下参数:

# vim +/"ssl =" /etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/pki/tls/certs/mydomain.com.crt
ssl_key = </etc/pki/tls/private/mydomain.com.key

注意:请仔细检查证书和密钥是否确实存在于 ssl_cert 和 ssl_key 中指定的路径。

/etc/dovecot/conf.d/10-auth.conf 中,将 disable_plaintext_auth 设置为 no,并启用 plain 和 login 认证机制:

# vim +/disable_plaintext_auth /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login

我们将使用 Postfix 中的 Dovecot 的 SMTP 认证服务来认证电子邮件帐户,因此请编辑 /etc/dovecot/conf.d/10-master.conf 并确保 service auth {} 部分包含以下代码段:

# vim /etc/dovecot/conf.d/10-master.conf
...
serviceauth {
    # Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
    }
}
...

最后,让我们编辑 /etc/dovecot/dovecot.conf,设置启用的协议并将 Dovecot 绑定到所有接口:

# vim +/"protocols =" /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
listen = *

使用 systemctl 重启系统上的 Dovecot 服务,并将其添加到系统的启动项中:

# systemctl restart dovecot
# systemctl status dovecot
# systemctl enable dovecot

安装 Postfix

什么是 Postfix?它是一个邮件传输代理(MTA),负责将电子邮件消息从一台计算机传输到另一台计算机。MTA 既可以充当发送电子邮件的客户端,也可以通过 SMTP 协议充当接收电子邮件的服务器。

使用 yum 安装 postfix:

# yum install postfix

安装完成后,创建 /etc/mail 目录,编辑 Postfix 主配置文件 /etc/postfix/main.cf 并设置以下配置选项:

# mkdir /etc/mail
# vim /etc/postfix/main.cf
inet_interfaces = all
inet_protocols = ipv4

myhostname=galaxy.mydomain.com
mydestination = /etc/mail/my_domains, $myhostname
virtual_alias_maps = hash:/etc/mail/virtual
home_mailbox = Maildir/

tls_random_source = dev:/dev/urandom
broken_sasl_auth_clients = yes

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = $smtpd_sasl_security_options
smtpd_use_tls = yes
smtpd_tls_key_file  = /etc/pki/tls/private/mydomain.com.key
smtpd_tls_cert_file = /etc/pki/tls/certs/mydomain.com.crt
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

注意:请确保将 galaxy.mydomain.com 替换为您的实际服务器主机名。同时,请验证 smtpd_tls_key_filesmtpd_tls_cert_file 中使用的路径是否存在。

接下来,创建两个配置文件 /etc/mail/my_domains/etc/mail/virtual。第一个将包含 Postfix 处理的所有域名,第二个将包含虚拟电子邮件别名。

# touch /etc/mail/my_domains /etc/mail/virtual
postmap /etc/mail/virtual
Edit /etc/postfix/master.cf and enable the submission (587) and SSL (465) ports in Postfix:
# vim /etc/postfix/master.cf

submissioninet n       -       n       -       -       smtpd

smtpsinet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes

使用 systemctl 重启 Postfix 服务以使更改生效:

# systemctl restart postfix
# systemctl status postfix
# systemctl enable postfix

添加域名、帐户和别名

将 mydomain.com 添加到 /etc/mail/my_domains ,以便 Postfix 可以接受和中继该域的电子邮件。每个域名都应单独一行添加。

# echo mydomain.com >> /etc/mail/my_domains

要在电子邮件服务器上创建新的 john@mydomain.com 电子邮件帐户,您可以使用以下命令:

# useradd -s /sbin/nologin -m john
# passwd john

如果您想添加一些别名,例如 helpdesk@mydomain.com 或 sales@mydomain.com,您可以将以下内容添加到 /etc/mail/virtual

helpdesk@mydomain.com john
sales@mydomain.com john

每次更改此配置文件后,您都必须对其进行 postmap 处理并重新启动 Postfix 以使更改生效。例如:

# postmap /etc/mail/virtual
# systemctl restart postfix

设置 OpenDKIM

DKIM 是一种数字电子邮件签名和验证技术,它在电子邮件服务器上对电子邮件进行数字签名。此功能可用于进一步验证电子邮件消息是否已签名……

启用 EPEL 存储库

您可以使用 yum 轻松安装 EPEL 存储库,如下所示:

# yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm

验证 EPEL 是否已在系统上启用:

# yumrepolist

安装 OpenDKIM

使用 yum 安装 OpenDKIM:

# yum install opendkim

配置 OpenDKIM

以下配置是合理的,应适用于大多数设置。但是,您可以根据自己的情况进行任何所需的更改。

复制 opendkimconfig 文件并按如下方式修改它。最后,保存文件并退出 vim。

# mv /etc/opendkim.conf{,.orig}
# vim /etc/opendkim.conf
AutoRestart             Yes
AutoRestartRate         10/1h
LogWhy                  Yes
Syslog                  Yes
SyslogSuccess           Yes
Mode                    sv
Canonicalization        relaxed/simple
ExternalIgnoreListrefile:/etc/opendkim/TrustedHosts
InternalHostsrefile:/etc/opendkim/TrustedHosts
KeyTablerefile:/etc/opendkim/KeyTable
SigningTablerefile:/etc/opendkim/SigningTable
SignatureAlgorithm      rsa-sha256
Socket                  inet:8891@localhost
PidFile                 /var/run/opendkim/opendkim.pid
UMask                   022
UserIDopendkim:opendkim
TemporaryDirectory      /var/tmp

设置 DKIM 私钥/公钥

现在您需要创建必要的 DKIM 私钥和公钥。按所示执行以下语句。

# mkdir /etc/opendkim/keys/mydomain.com
# opendkim-genkey -D /etc/opendkim/keys/mydomain.com/ -d mydomain.com -s mail
# chown -R opendkim: /etc/opendkim/keys/mydomain.com
# mv /etc/opendkim/keys/mydomain.com/mail.private /etc/opendkim/keys/mydomain.com/mail

编辑 KeyTable 文件:

# vim /etc/opendkim/KeyTable

mail._domainkey.mydomain.com mydomain.com:mail:/etc/opendkim/keys/mydomain.com/mail

现在编辑 SigningTable 文件:

# vim /etc/opendkim/SigningTable

*@mydomain.com mail._domainkey.mydomain.com

将受信任的主机添加到文件,如下所示。请务必将 mydomain.com 替换为您的实际域名。

# vim /etc/opendkim/TrustedHosts

127.0.0.1
mydomain.com
galaxy.mydomain.com

在域的区域文件中添加 TXT 记录:

# cat /etc/opendkim/keys/mydomain.com/mail.txt

使用 dig 验证 DKIM TXT 记录:

# dig +short mail._domainkey.mydomain.com TXT

将 DKIM 集成到 Postfix 中

# vim /etc/postfix/main.cf

smtpd_milters           = inet:127.0.0.1:8891
non_smtpd_milters       = $smtpd_milters
milter_default_action   = accept
milter_protocol         = 2
# systemctl restart opendkim
# systemctl enable opendkim

# systemctl restart postfix

这样就完成了。您现在应该拥有一个功能齐全的 Postfix、Dovecot 和 DKIM 设置,可以为您的域名发送和接收 DKIM 签名的电子邮件。

© . All rights reserved.