龙空技术网

如何在 CentOS 8 上配置邮件服务器 ?

鸠摩智首席音效师 133

前言:

而今兄弟们对“centos7mailserver”大概比较关切,同学们都需要学习一些“centos7mailserver”的相关知识。那么小编同时在网上搜集了一些关于“centos7mailserver””的相关内容,希望大家能喜欢,大家一起来了解一下吧!

how-to-install-postfix-server-on-centos8

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

实验准备操作系统 : CentOS 8 server网络地址 : 192.168.1.13主机名称: server1.crazytechgeek.info (确保域名指向服务器的 IP)1) 更新系统

# dnf update

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

# dnf remove sendmail
2) 设置 hostname 并更新 /etc/hosts 文件

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

# hostnamectl set-hostname server1.crazytechgeek.info# exec bash

在 /etc /hosts 文件中添加系统的主机名和 IP

# vim /etc/hosts192.168.1.13   server1.crazytechgeek.info
3) 安装 Postfix Mail Server

确认系统上没有其他 MTA 正在运行后,执行命令安装 Postfix

# dnf install postfix
4) 开启 Postfix 服务

安装成功后,启动并启用 Postfix 服务

# systemctl start postfix# systemctl enable postfix

检查 Postfix 服务状态

# systemctl status postfix
5) 安装 mailx 邮件客户端

在配置 Postfix 服务器前,需要先安装 mailx feature,使用此命令安装 mailx feature

# dnf install mailx
6) 配置 Postfix Mail Server

编辑 /etc/postfix/main.cf 文件

# vi /etc/postfix/main.cf

对以下行进行更改

myhostname = server1.crazytechgeek.infomydomain = crazytechgeek.infomyorigin = $mydomain## Uncomment and Set inet_interfaces to all ##inet_interfaces = all## Change to all ##inet_protocols = all## Comment ###mydestination = $myhostname, localhost.$mydomain, localhost##- Uncomment ##mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain## Uncomment and add IP range ##mynetworks = 192.168.1.0/24, 127.0.0.0/8## Uncomment ##home_mailbox = Maildir/

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

# systemctl restart postfix
7) 测试 Postfix Mail Server

首先,创建一个测试用户

# useradd postfixuser# passwd postfixuser

运行以下命令,从本地用户 pkumar 向另一个用户 postfixuser 发送电子邮件

# telnet localhost smtpor# telnet localhost 25

如果没有安装 telnet 服务,可以使用该命令安装

# dnf install telnet -y

您应该得到如下所示的输出

[root@server1 ~]# telnet localhost 25Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.220 server1.crazytechgeek.info ESMTP Postfix

以上确认与邮件服务器的连接正常。接下来,键入命令

# ehlo localhost

输出将是这样的

250-server1.crazytechgeek.info250-PIPELINING250-SIZE 10240000250-VRFY250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 SMTPUTF8

接下来,运行以橙色突出显示的命令,如 mail from、rcpt to、data,然后最后键入 quit

mail from:<pkumar>250 2.1.0 Okrcpt to:<postfixuser>250 2.1.5 Okdata354 End data with <CR><LF>.<CR><LF>Hello, Welcome to my mailserver (Postfix).250 2.0.0 Ok: queued as B56BF1189BECquit221 2.0.0 ByeConnection closed by foreign host

完整的信息如下所示

If everything went according to plan, you should be able to view the email sent at the new user’s home directory.

如果一切按计划进行,您应该能够在用户的主目录中看到发送的电子邮件。

# ls /home/postfixuser/Maildir/new1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info#

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

# cat /home/postfixuser/Maildir/new/1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
Postfix mail server 日志

使用下面的命令查看实时日志

# tail -f /var/log/maillog
Postfix Mail Server 安全

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

安装 openssl

# dnf install openssl -y

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

# openssl req -nodes -newkey rsa:2048 -keyout mail.key -out mail.csr

使用以下 openssl 命令生成自签名证书

# openssl x509 -req -days 365 -in mail.csr -signkey mail.key -out mail.crtSignature oksubject=C = IN, ST = New Delhi, L = New Delhi, O = IT, OU = IT, CN = server1.crazytechgeek.info, emailAddress = info@crazytechgeek.infoGetting Private key#

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

# cp mail.key mail.crt /etc/postfix

更新 postfix 配置文件中的私钥和证书文件路径

# vi /etc/postfix/main.cf………smtpd_use_tls = yessmtpd_tls_cert_file = /etc/postfix/mail.crtsmtpd_tls_key_file = /etc/postfix/mail.keysmtpd_tls_security_level = may………

重新启动 postfix 服务使上述更改生效

# systemctl restart postfix

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

(1) 从 pkumar 用户向 postfixuser 发送本地内部邮件

# echo "test email" | mailx -s "Test email from Postfix MailServer" -r pkumar@crazytechgeek.info postfixuser@crazytechgeek.info

使用以下方法检查并阅读邮件

# cd /home/postfixuser/Maildir/new/# lltotal 8-rw-------. 1 postfixuser postfixuser 476 Nov 12 17:34 1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info-rw-------. 1 postfixuser postfixuser 612 Nov 13 02:40 1573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info# cat 1573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info

(2) 从 postfixuser 发送电子邮件到外部域 (info@linuxtechi.com)

# echo "External Test email" | mailx -s "Postfix MailServer" -r pkumar@crazytechgeek.info info@linuxtechi.com

注意: 如果您的 IP 没有被列入黑名单,那么您的电子邮件将被发送到外部域,否则将被反弹,说 IP 在某某垃圾邮件数据库中被列入黑名单。

查看 Postfix 邮件队列

使用 mailq 命令列出正在排队的邮件

# mailqMail queue is empty#

我们希望本教程对您有所帮助,并希望您可以轻松地设置本地 Postfix 服务器。

我的开源项目

酷瓜云课堂 - 开源在线教育解决方案

course-tencent-cloud(酷瓜云课堂 - gitee 仓库)course-tencent-cloud(酷瓜云课堂 - github 仓库)

标签: #centos7mailserver