如何在CentOS 8上安装和配置Postfix邮件服务器

codemanship 2019-11-21

如何在CentOS 8上安装和配置Postfix邮件服务器

Postfix 是一个自由开源的 MTA(邮件传输代理),用于在 Linux 系统上路由或传递电子邮件。在本指南中,你将学习如何在 CentOS 8 上安装和配置 Postfix。

实验室设置:

  • 系统:CentOS 8 服务器
  • IP 地址:192.168.1.13
  • 主机名:server1.crazytechgeek.info(确保域名指向服务器的 IP)

步骤 1)更新系统

第一步是确保系统软件包是最新的。为此,请按如下所示更新系统:

  1. <span class="com">#</span><span class="pln"> dnf update</span>

继续之前,还请确保不存在其他 MTA(如 Sendmail),因为这将导致与 Postfix 配置冲突。例如,要删除 Sendmail,请运行以下命令:

  1. <span class="com">#</span><span class="pln"> dnf remove sendmail</span>

步骤 2)设置主机名并更新 /etc/hosts

使用下面的 hostnamectl 命令在系统上设置主机名:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">hostnamectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  2. <span class="com">#</span><span class="pln"> </span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">bash</span>

此外,你需要在 /etc/hosts 中添加系统的主机名和 IP:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vim</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">hosts</span>
  2. <span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.13</span><span class="pln"> server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>

保存并退出文件。

步骤 3)安装 Postfix 邮件服务器

验证系统上没有其他 MTA 在运行后,运行以下命令安装 Postfix:

  1. <span class="com">#</span><span class="pln"> dnf install postfix</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Install-Postfix-Centos8

步骤 4)启动并启用 Postfix 服务

成功安装 Postfix 后,运行以下命令启动并启用 Postfix 服务:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> start postfix</span>
  2. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> enable postfix</span>

要检查 Postfix 状态,请运行以下 systemctl 命令:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> status postfix</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Start-Postfix-check-status-centos8

太好了,我们已经验证了 Postfix 已启动并正在运行。接下来,我们将配置 Postfix 从本地发送邮件到我们的服务器。

步骤 5)安装 mailx 邮件客户端

在配置 Postfix 服务器之前,我们需要安装 mailx,要安装它,请运行以下命令:

  1. <span class="com">#</span><span class="pln"> dnf install mailx</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Install-Mailx-CentOS8

步骤 6)配置 Postfix 邮件服务器

Postfix 的配置文件位于 /etc/postfix/main.cf 中。我们需要对配置文件进行一些修改,因此请使用你喜欢的文本编辑器将其打开:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">main</span><span class="pun">.</span><span class="pln">cf</span>

更改以下几行:

  1. <span class="pln">myhostname </span><span class="pun">=</span><span class="pln"> server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  2. <span class="pln">mydomain </span><span class="pun">=</span><span class="pln"> crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  3. <span class="pln">myorigin </span><span class="pun">=</span><span class="pln"> $mydomain</span>
  4. <span class="com">##</span><span class="pln"> </span><span class="pun">取消注释并将</span><span class="pln"> inet_interfaces </span><span class="pun">设置为</span><span class="pln"> all</span><span class="com">##</span>
  5. <span class="pln">inet_interfaces </span><span class="pun">=</span><span class="pln"> all</span>
  6. <span class="com">##</span><span class="pln"> </span><span class="pun">更改为</span><span class="pln"> all </span><span class="com">##</span>
  7. <span class="pln">inet_protocols </span><span class="pun">=</span><span class="pln"> all</span>
  8. <span class="com">##</span><span class="pln"> </span><span class="pun">注释</span><span class="pln"> </span><span class="com">##</span>
  9. <span class="com">#</span><span class="pln">mydestination </span><span class="pun">=</span><span class="pln"> $myhostname</span><span class="pun">,</span><span class="pln"> localhost</span><span class="pun">.</span><span class="pln">$mydomain</span><span class="pun">,</span><span class="pln"> localhost</span>
  10. <span class="com">##</span><span class="pln"> </span><span class="pun">取消注释</span><span class="pln"> </span><span class="com">##</span>
  11. <span class="pln">mydestination </span><span class="pun">=</span><span class="pln"> $myhostname</span><span class="pun">,</span><span class="pln"> localhost</span><span class="pun">.</span><span class="pln">$mydomain</span><span class="pun">,</span><span class="pln"> localhost</span><span class="pun">,</span><span class="pln"> $mydomain</span>
  12. <span class="com">##</span><span class="pln"> </span><span class="pun">取消注释并添加</span><span class="pln"> IP </span><span class="pun">范围</span><span class="pln"> </span><span class="com">##</span>
  13. <span class="pln">mynetworks </span><span class="pun">=</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">/</span><span class="lit">24</span><span class="pun">,</span><span class="pln"> </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">/</span><span class="lit">8</span>
  14. <span class="com">##</span><span class="pln"> </span><span class="pun">取消注释</span><span class="pln"> </span><span class="com">##</span>
  15. <span class="pln">home_mailbox </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Maildir</span><span class="pun">/</span>

完成后,保存并退出配置文件。重新启动 postfix 服务以使更改生效:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> restart postfix</span>

步骤 7)测试 Postfix 邮件服务器

测试我们的配置是否有效,首先,创建一个测试用户。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">useradd</span><span class="pln"> postfixuser</span>
  2. <span class="com">#</span><span class="pln"> </span><span class="kwd">passwd</span><span class="pln"> postfixuser</span>

接下来,运行以下命令,从本地用户 pkumar 发送邮件到另一个用户 postfixuser

  1. <span class="com">#</span><span class="pln"> telnet localhost smtp</span>
  2. <span class="pun">或者</span>
  3. <span class="com">#</span><span class="pln"> telnet localhost </span><span class="lit">25</span>

如果未安装 telnet 服务,那么可以使用以下命令进行安装:

  1. <span class="com">#</span><span class="pln"> dnf install telnet </span><span class="pun">-</span><span class="pln">y</span>

如前所述运行命令时,应获得如下输出:

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> telnet localhost </span><span class="lit">25</span>
  2. <span class="typ">Trying</span><span class="pln"> </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">...</span>
  3. <span class="typ">Connected</span><span class="pln"> to localhost</span><span class="pun">.</span>
  4. <span class="typ">Escape</span><span class="pln"> character </span><span class="kwd">is</span><span class="pln"> </span><span class="str">'^]'</span><span class="pun">.</span>
  5. <span class="lit">220</span><span class="pln"> server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span><span class="pln"> ESMTP </span><span class="typ">Postfix</span>

上面的结果确认与 postfix 邮件服务器的连接正常。接下来,输入命令:

  1. <span class="com">#</span><span class="pln"> ehlo localhost</span>

输出看上去像这样:

  1. <span class="lit">250</span><span class="pun">-</span><span class="pln">server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  2. <span class="lit">250</span><span class="pun">-</span><span class="pln">PIPELINING</span>
  3. <span class="lit">250</span><span class="pun">-</span><span class="pln">SIZE </span><span class="lit">10240000</span>
  4. <span class="lit">250</span><span class="pun">-</span><span class="pln">VRFY</span>
  5. <span class="lit">250</span><span class="pun">-</span><span class="pln">ETRN</span>
  6. <span class="lit">250</span><span class="pun">-</span><span class="pln">STARTTLS</span>
  7. <span class="lit">250</span><span class="pun">-</span><span class="pln">ENHANCEDSTATUSCODES</span>
  8. <span class="lit">250</span><span class="pun">-</span><span class="lit">8BITMIME</span>
  9. <span class="lit">250</span><span class="pun">-</span><span class="pln">DSN</span>
  10. <span class="lit">250</span><span class="pln"> SMTPUTF8</span>

接下来,运行橙色高亮的命令,例如 mail fromrcpt todata,最后输入 quit

  1. <span class="pln">mail </span><span class="kwd">from</span><span class="pun">:<</span><span class="pln">pkumar</span><span class="pun">></span>
  2. <span class="lit">250</span><span class="pln"> </span><span class="lit">2.1</span><span class="pun">.</span><span class="lit">0</span><span class="pln"> </span><span class="typ">Ok</span>
  3. <span class="pln">rcpt to</span><span class="pun">:<</span><span class="pln">postfixuser</span><span class="pun">></span>
  4. <span class="lit">250</span><span class="pln"> </span><span class="lit">2.1</span><span class="pun">.</span><span class="lit">5</span><span class="pln"> </span><span class="typ">Ok</span>
  5. <span class="pln">data</span>
  6. <span class="lit">354</span><span class="pln"> </span><span class="typ">End</span><span class="pln"> data </span><span class="kwd">with</span><span class="pln"> </span><span class="pun"><</span><span class="pln">CR</span><span class="pun">><</span><span class="pln">LF</span><span class="pun">>.<</span><span class="pln">CR</span><span class="pun">><</span><span class="pln">LF</span><span class="pun">></span>
  7. <span class="typ">Hello</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Welcome</span><span class="pln"> to </span><span class="kwd">my</span><span class="pln"> mailserver </span><span class="pun">(</span><span class="typ">Postfix</span><span class="pun">)</span>
  8. <span class="pun">.</span>
  9. <span class="lit">250</span><span class="pln"> </span><span class="lit">2.0</span><span class="pun">.</span><span class="lit">0</span><span class="pln"> </span><span class="typ">Ok</span><span class="pun">:</span><span class="pln"> queued </span><span class="kwd">as</span><span class="pln"> B56BF1189BEC</span>
  10. <span class="pln">quit</span>
  11. <span class="lit">221</span><span class="pln"> </span><span class="lit">2.0</span><span class="pun">.</span><span class="lit">0</span><span class="pln"> </span><span class="typ">Bye</span>
  12. <span class="typ">Connection</span><span class="pln"> closed by foreign host</span>

完成 telnet 命令可从本地用户 pkumar 发送邮件到另一个本地用户 postfixuser,如下所示:

如何在CentOS 8上安装和配置Postfix邮件服务器

Send-email-with-telnet-centos8

如果一切都按计划进行,那么你应该可以在新用户的家目录中查看发送的邮件:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">postfixuser</span><span class="pun">/</span><span class="typ">Maildir</span><span class="pun">/</span><span class="kwd">new</span>
  2. <span class="lit">1573580091.Vfd02I20050b8M635437.server1.crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  3. <span class="com">#</span>

要阅读邮件,只需使用 cat 命令,如下所示:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">postfixuser</span><span class="pun">/</span><span class="typ">Maildir</span><span class="pun">/</span><span class="kwd">new</span><span class="pun">/</span><span class="lit">1573580091.Vfd02I20050b8M635437.server1.crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Read-postfix-email-linux

Postfix 邮件服务器日志

Postfix 邮件服务器邮件日志保存在文件 /var/log/maillog 中,使用以下命令查看实时日志,

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">tail</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">maillog</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

postfix-maillogs-centos8

保护 Postfix 邮件服务器

建议始终确保客户端和 Postfix 服务器之间的通信安全,这可以使用 SSL 证书来实现,它们可以来自受信任的权威机构或自签名证书。在本教程中,我们将使用 openssl 命令生成用于 Postfix 的自签名证书,

我假设 openssl 已经安装在你的系统上,如果未安装,请使用以下 dnf 命令:

  1. <span class="com">#</span><span class="pln"> dnf install openssl </span><span class="pun">-</span><span class="pln">y</span>

使用下面的 openssl 命令生成私钥和 CSR(证书签名请求):

  1. <span class="com">#</span><span class="pln"> openssl req </span><span class="pun">-</span><span class="pln">nodes </span><span class="pun">-</span><span class="pln">newkey rsa</span><span class="pun">:</span><span class="lit">2048</span><span class="pln"> </span><span class="pun">-</span><span class="pln">keyout mail</span><span class="pun">.</span><span class="pln">key </span><span class="pun">-</span><span class="pln">out mail</span><span class="pun">.</span><span class="pln">csr</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Postfix-Key-CSR-CentOS8

现在,使用以下 openssl 命令生成自签名证书:

  1. <span class="com">#</span><span class="pln"> openssl x509 </span><span class="pun">-</span><span class="pln">req </span><span class="pun">-</span><span class="pln">days </span><span class="lit">365</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">in</span><span class="pln"> mail</span><span class="pun">.</span><span class="pln">csr </span><span class="pun">-</span><span class="pln">signkey mail</span><span class="pun">.</span><span class="pln">key </span><span class="pun">-</span><span class="pln">out mail</span><span class="pun">.</span><span class="pln">crt</span>
  2. <span class="typ">Signature</span><span class="pln"> ok</span>
  3. <span class="pln">subject</span><span class="pun">=</span><span class="pln">C </span><span class="pun">=</span><span class="pln"> IN</span><span class="pun">,</span><span class="pln"> ST </span><span class="pun">=</span><span class="pln"> </span><span class="typ">New</span><span class="pln"> </span><span class="typ">Delhi</span><span class="pun">,</span><span class="pln"> L </span><span class="pun">=</span><span class="pln"> </span><span class="typ">New</span><span class="pln"> </span><span class="typ">Delhi</span><span class="pun">,</span><span class="pln"> O </span><span class="pun">=</span><span class="pln"> IT</span><span class="pun">,</span><span class="pln"> OU </span><span class="pun">=</span><span class="pln"> IT</span><span class="pun">,</span><span class="pln"> CN </span><span class="pun">=</span><span class="pln"> server1</span><span class="pun">.</span><span class="pln">crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span><span class="pun">,</span><span class="pln"> emailAddress </span><span class="pun">=</span><span class="pln"> root@linuxtechi</span>
  4. <span class="typ">Getting</span><span class="pln"> </span><span class="typ">Private</span><span class="pln"> key</span>
  5. <span class="com">#</span>

现在将私钥和证书文件复制到 /etc/postfix 目录下:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">cp</span><span class="pln"> mail</span><span class="pun">.</span><span class="pln">key mail</span><span class="pun">.</span><span class="pln">crt </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span>

在 Postfix 配置文件中更新私钥和证书文件的路径:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">main</span><span class="pun">.</span><span class="pln">cf</span>
  2. <span class="pun">………</span>
  3. <span class="pln">smtpd_use_tls </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">yes</span>
  4. <span class="pln">smtpd_tls_cert_file </span><span class="pun">=</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">crt</span>
  5. <span class="pln">smtpd_tls_key_file </span><span class="pun">=</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">mail</span><span class="pun">.</span><span class="pln">key</span>
  6. <span class="pln">smtpd_tls_security_level </span><span class="pun">=</span><span class="pln"> may</span>
  7. <span class="pun">………</span>

重启 Postfix 服务以使上述更改生效:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> restart postfix</span>

让我们尝试使用 mailx 客户端将邮件发送到内部本地域和外部域。

从 pkumar 发送内部本地邮件到 postfixuser 中:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"test email"</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> mailx </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Test email from Postfix MailServer"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">r root@linuxtechi root@linuxtechi</span>

使用以下命令检查并阅读邮件:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">cd</span><span class="pln"> </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">postfixuser</span><span class="pun">/</span><span class="typ">Maildir</span><span class="pun">/</span><span class="kwd">new</span><span class="pun">/</span>
  2. <span class="com">#</span><span class="pln"> ll</span>
  3. <span class="pln">total </span><span class="lit">8</span>
  4. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-------.</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> postfixuser postfixuser </span><span class="lit">476</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="lit">17</span><span class="pun">:</span><span class="lit">34</span><span class="pln"> </span><span class="lit">1573580091.Vfd02I20050b8M635437.server1.crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  5. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-------.</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> postfixuser postfixuser </span><span class="lit">612</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">13</span><span class="pln"> </span><span class="lit">02</span><span class="pun">:</span><span class="lit">40</span><span class="pln"> </span><span class="lit">1573612845.Vfd02I20050bbM466643.server1.crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>
  6. <span class="com">#</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="lit">1573612845.Vfd02I20050bbM466643.server1.crazytechgeek</span><span class="pun">.</span><span class="kwd">info</span>

如何在CentOS 8上安装和配置Postfix邮件服务器

Read-Postfixuser-Email-CentOS8

从 postfixuser 发送邮件到外部域([email protected]):

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"External Test email"</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> mailx </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Postfix MailServer"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">r root@linuxtechi root@linuxtechi</span>

注意:如果你的 IP 没有被任何地方列入黑名单,那么你发送到外部域的邮件将被发送,否则它将被退回,并提示你的 IP 被 spamhaus 之类的数据库列入黑名单。

检查 Postfix 邮件队列

使用 mailq 命令列出队列中的邮件:

相关推荐

就是那个胖子 / 0评论 2020-06-15