共计 4077 个字符,预计需要花费 11 分钟才能阅读完成。
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/hosts | |
192.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.info | |
mydomain = crazytechgeek.info | |
myorigin = $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 smtp | |
or | |
# telnet localhost 25 |
如果没有装置 telnet 服务,能够应用该命令装置
# dnf install telnet -y
您应该失去如下所示的输入
[root@server1 ~]# telnet localhost 25 | |
Trying 127.0.0.1... | |
Connected to localhost. | |
Escape character is '^]'. | |
220 server1.crazytechgeek.info ESMTP Postfix |
以上确认与邮件服务器的连贯失常。接下来,键入命令
# ehlo localhost
输入将是这样的
250-server1.crazytechgeek.info | |
250-PIPELINING | |
250-SIZE 10240000 | |
250-VRFY | |
250-ETRN | |
250-STARTTLS | |
250-ENHANCEDSTATUSCODES | |
250-8BITMIME | |
250-DSN | |
250 SMTPUTF8 |
接下来,运行以橙色突出显示的命令,如 mail from、rcpt to、data,而后最初键入 quit
mail from:<pkumar> | |
250 2.1.0 Ok | |
rcpt to:<postfixuser> | |
250 2.1.5 Ok | |
data | |
354 End data with <CR><LF>.<CR><LF> | |
Hello, Welcome to my mailserver (Postfix) | |
. | |
250 2.0.0 Ok: queued as B56BF1189BEC | |
quit | |
221 2.0.0 Bye | |
Connection 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/new | |
1573580091.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.crt | |
Signature ok | |
subject=C = IN, ST = New Delhi, L = New Delhi, O = IT, OU = IT, CN = server1.crazytechgeek.info, emailAddress = info@crazytechgeek.info | |
Getting Private key | |
# |
将私钥和证书文件复制到 /etc/postfix 目录
# cp mail.key mail.crt /etc/postfix
更新 postfix 配置文件中的私钥和证书文件门路
# vi /etc/postfix/main.cf | |
……… | |
smtpd_use_tls = yes | |
smtpd_tls_cert_file = /etc/postfix/mail.crt | |
smtpd_tls_key_file = /etc/postfix/mail.key | |
smtpd_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/ | |
# ll | |
total 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 命令列出正在排队的邮件
# mailq | |
Mail queue is empty | |
# |
咱们心愿本教程对您有所帮忙,并心愿您能够轻松地设置本地 Postfix 服务器。
我的开源我的项目
- course-tencent-cloud(酷瓜云课堂 – gitee 仓库)
- course-tencent-cloud(酷瓜云课堂 – github 仓库)