80497312 2019-08-14
交流是非常重要的。你的电脑可能正试图告诉你一些重要的事情。但是,如果你没有正确配置邮件传输代理(MTA),那么你可能不会收到通知。Postfix 是一个易于配置且以强大的安全记录而闻名的 MTA。遵循以下步骤,以确保从本地服务发送的电子邮件通知将通过 Postfix MTA 路由到你的互联网电子邮件账户中。
使用 dnf
来安装一些必须软件包(你应该配置了 sudo,对吧?):
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i</span>
<span class="com">#</span><span class="pln"> dnf install postfix mailx</span>
如果以前配置了不同的 MTA,那么你可能需要将 Postfix 设置为系统默认。使用 alternatives
命令设置系统默认 MTA:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> alternatives </span><span class="pun">--</span><span class="pln">config mta</span>
<span class="typ">There</span><span class="pln"> are </span><span class="lit">2</span><span class="pln"> programs which provide </span><span class="str">'mta'</span><span class="pun">.</span>
<span class="pln"> </span><span class="typ">Selection</span><span class="pln"> </span><span class="typ">Command</span>
<span class="pun">*+</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">sendmail</span><span class="pun">.</span><span class="pln">sendmail</span>
<span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">sendmail</span><span class="pun">.</span><span class="pln">postfix</span>
<span class="typ">Enter</span><span class="pln"> to keep the current selection</span><span class="pun">[+],</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> type selection number</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span>
你需要创建一个 Postfix 查询表条目,其中包含你要用于发送电子邮件账户的地址和密码:
<span class="com">#</span><span class="pln"> MY_EMAIL_ADDRESS</span><span class="pun">=</span><span class="pln">glb@gmail</span><span class="pun">.</span><span class="pln">com</span>
<span class="com">#</span><span class="pln"> MY_EMAIL_PASSWORD</span><span class="pun">=</span><span class="pln">abcdefghijklmnop</span>
<span class="com">#</span><span class="pln"> MY_SMTP_SERVER</span><span class="pun">=</span><span class="pln">smtp</span><span class="pun">.</span><span class="pln">gmail</span><span class="pun">.</span><span class="pln">com</span>
<span class="com">#</span><span class="pln"> MY_SMTP_SERVER_PORT</span><span class="pun">=</span><span class="lit">587</span>
<span class="com">#</span><span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"[$MY_SMTP_SERVER]:$MY_SMTP_SERVER_PORT $MY_EMAIL_ADDRESS:$MY_EMAIL_PASSWORD"</span><span class="pln"> </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">password_maps</span>
<span class="com">#</span><span class="pln"> </span><span class="kwd">chmod</span><span class="pln"> </span><span class="lit">600</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">password_maps</span>
<span class="com">#</span><span class="pln"> </span><span class="kwd">unset</span><span class="pln"> MY_EMAIL_PASSWORD</span>
<span class="com">#</span><span class="pln"> history </span><span class="pun">-</span><span class="pln">c</span>
如果你使用的是 Gmail 账户,那么你需要为 Postfix 配置一个“应用程序密码”而不是使用你的 Gmail 密码。有关配置应用程序密码的说明,参阅“使用应用程序密码登录”。
接下来,你必须对 Postfix 查询表运行 postmap
命令,以创建或更新 Postfix 实际使用的文件的散列版本:
<span class="com">#</span><span class="pln"> postmap </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">password_maps</span>
散列后的版本将具有相同的文件名,但后缀为 .db
。
更新 Postfix 的 main.cf
配置文件,以引用刚刚创建 Postfix 查询表。编辑该文件并添加以下行:
<span class="pln">relayhost </span><span class="pun">=</span><span class="pln"> smtp</span><span class="pun">.</span><span class="pln">gmail</span><span class="pun">.</span><span class="pln">com</span><span class="pun">:</span><span class="lit">587</span>
<span class="pln">smtp_tls_security_level </span><span class="pun">=</span><span class="pln"> verify</span>
<span class="pln">smtp_tls_mandatory_ciphers </span><span class="pun">=</span><span class="pln"> high</span>
<span class="pln">smtp_tls_verify_cert_match </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">hostname</span>
<span class="pln">smtp_sasl_auth_enable </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">yes</span>
<span class="pln">smtp_sasl_security_options </span><span class="pun">=</span><span class="pln"> noanonymous</span>
<span class="pln">smtp_sasl_password_maps </span><span class="pun">=</span><span class="pln"> hash</span><span class="pun">:</span><span class="str">/etc/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">password_maps</span>
这里假设你使用 Gmail 作为 relayhost
设置,但是你可以用正确的主机名和端口替换系统应该将邮件发送到的邮件主机。
有关上述配置选项的最新详细信息,参考 man 帮助:
<span class="pln">$ </span><span class="kwd">man</span><span class="pln"> postconf</span><span class="pun">.</span><span class="lit">5</span>
更新 main.cf
文件后,启用并启动 Postfix 服务:
<span class="com">#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> enable </span><span class="pun">--</span><span class="pln">now postfix</span><span class="pun">.</span><span class="pln">service</span>
然后,你可以使用 exit
命令或 Ctrl+D
以 root 身份退出 sudo
会话。你现在应该能够使用 mail
命令测试你的配置:
<span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">'It worked!'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Test: $(date)"</span><span class="pln"> glb@gmail</span><span class="pun">.</span><span class="pln">com</span>
如果你安装了像 logwatch、mdadm、fail2ban、apcupsd 或 certwatch 这样的服务,你现在可以更新它们的配置,以便它们的电子邮件通知转到你的 Internet 电子邮件地址。
另外,你可能希望将发送到本地系统 root 账户的所有电子邮件都转到互联网电子邮件地址中,将以下行添加到系统的 /etc/alises
文件中(你需要使用 sudo
编辑此文件,或首先切换到 root
账户):
<span class="pln">root</span><span class="pun">:</span><span class="pln"> glb</span><span class="pun">+</span><span class="pln">root@gmail</span><span class="pun">.</span><span class="pln">com</span>
现在运行此命令重新读取别名:
<span class="com">#</span><span class="pln"> newaliases</span>
@
符号之间添加字母数字标记,如上所示,以便更轻松地识别和过滤从计算机收到的电子邮件。查看邮件队列:
<span class="pln">$ mailq</span>
清除队列中的所有电子邮件:
<span class="com">#</span><span class="pln"> postsuper </span><span class="pun">-</span><span class="pln">d ALL</span>
过滤设置,以获得感兴趣的值:
<span class="pln">$ postconf </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="str">"^relayhost\|^smtp_"</span>
查看 postfix/smtp
日志:
<span class="pln">$ </span><span class="kwd">journalctl</span><span class="pln"> </span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">pager </span><span class="pun">-</span><span class="pln">t postfix</span><span class="pun">/</span><span class="pln">smtp</span>