溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

發(fā)布時間:2021-02-02 11:26:53 來源:億速云 閱讀:248 作者:小新 欄目:服務(wù)器

這篇文章主要介紹怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

Postfix 是一個自由開源的 MTA(郵件傳輸代理),用于在 Linux 系統(tǒng)上路由或傳遞電子郵件。在本指南中,你將學(xué)習(xí)如何在 CentOS 8 上安裝和配置 Postfix。

實(shí)驗(yàn)室設(shè)置:

  • 系統(tǒng):CentOS 8 服務(wù)器

  • IP 地址:192.168.1.13

  • 主機(jī)名:server1.crazytechgeek.info(確保域名指向服務(wù)器的 IP)

步驟 1)更新系統(tǒng)

第一步是確保系統(tǒng)軟件包是最新的。為此,請按如下所示更新系統(tǒng):

# dnf update

繼續(xù)之前,還請確保不存在其他 MTA(如 Sendmail),因?yàn)檫@將導(dǎo)致與 Postfix 配置沖突。例如,要刪除 Sendmail,請運(yùn)行以下命令:

# dnf remove sendmail

步驟 2)設(shè)置主機(jī)名并更新 /etc/hosts

使用下面的 hostnamectl 命令在系統(tǒng)上設(shè)置主機(jī)名:

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

此外,你需要在 /etc/hosts 中添加系統(tǒng)的主機(jī)名和 IP:

# vim /etc/hosts
192.168.1.13  server1.crazytechgeek.info

保存并退出文件。

步驟 3)安裝 Postfix 郵件服務(wù)器

驗(yàn)證系統(tǒng)上沒有其他 MTA 在運(yùn)行后,運(yùn)行以下命令安裝 Postfix:

# dnf install postfix

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Install-Postfix-Centos8

步驟 4)啟動并啟用 Postfix 服務(wù)

成功安裝 Postfix 后,運(yùn)行以下命令啟動并啟用 Postfix 服務(wù):

# systemctl start postfix
# systemctl enable postfix

要檢查 Postfix 狀態(tài),請運(yùn)行以下 systemctl 命令:

# systemctl status postfix

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Start-Postfix-check-status-centos8

太好了,我們已經(jīng)驗(yàn)證了 Postfix 已啟動并正在運(yùn)行。接下來,我們將配置 Postfix 從本地發(fā)送郵件到我們的服務(wù)器。

步驟 5)安裝 mailx 郵件客戶端

在配置 Postfix 服務(wù)器之前,我們需要安裝 mailx,要安裝它,請運(yùn)行以下命令:

# dnf install mailx

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Install-Mailx-CentOS8

步驟 6)配置 Postfix 郵件服務(wù)器

Postfix 的配置文件位于 /etc/postfix/main.cf 中。我們需要對配置文件進(jìn)行一些修改,因此請使用你喜歡的文本編輯器將其打開:

# vi /etc/postfix/main.cf

更改以下幾行:

myhostname = server1.crazytechgeek.info
mydomain = crazytechgeek.info
myorigin = $mydomain
## 取消注釋并將 inet_interfaces 設(shè)置為 all##
inet_interfaces = all
## 更改為 all ##
inet_protocols = all
## 注釋 ##
#mydestination = $myhostname, localhost.$mydomain, localhost
## 取消注釋 ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## 取消注釋并添加 IP 范圍 ##
mynetworks = 192.168.1.0/24, 127.0.0.0/8
## 取消注釋 ##
home_mailbox = Maildir/

完成后,保存并退出配置文件。重新啟動 postfix 服務(wù)以使更改生效:

# systemctl restart postfix

步驟 7)測試 Postfix 郵件服務(wù)器

測試我們的配置是否有效,首先,創(chuàng)建一個測試用戶。

# useradd postfixuser
# passwd postfixuser

接下來,運(yùn)行以下命令,從本地用戶 pkumar 發(fā)送郵件到另一個用戶 postfixuser。

# telnet localhost smtp
或者
# telnet localhost 25

如果未安裝 telnet 服務(wù),那么可以使用以下命令進(jìn)行安裝:

# dnf install telnet -y

如前所述運(yùn)行命令時,應(yīng)獲得如下輸出:

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

上面的結(jié)果確認(rèn)與 postfix 郵件服務(wù)器的連接正常。接下來,輸入命令:

# 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

接下來,運(yùn)行橙色高亮的命令,例如 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

完成 telnet 命令可從本地用戶 pkumar 發(fā)送郵件到另一個本地用戶 postfixuser,如下所示:

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Send-email-with-telnet-centos8

如果一切都按計(jì)劃進(jìn)行,那么你應(yīng)該可以在新用戶的家目錄中查看發(fā)送的郵件:

# ls /home/postfixuser/Maildir/new
1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
#

要閱讀郵件,只需使用 cat 命令,如下所示:

# cat /home/postfixuser/Maildir/new/1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Read-postfix-email-linux

Postfix 郵件服務(wù)器日志

Postfix 郵件服務(wù)器郵件日志保存在文件 /var/log/maillog 中,使用以下命令查看實(shí)時日志,

# tail -f /var/log/maillog

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

postfix-maillogs-centos8

保護(hù) Postfix 郵件服務(wù)器

建議始終確??蛻舳撕?Postfix 服務(wù)器之間的通信安全,這可以使用 SSL 證書來實(shí)現(xiàn),它們可以來自受信任的權(quán)威機(jī)構(gòu)或自簽名證書。在本教程中,我們將使用 openssl 命令生成用于 Postfix 的自簽名證書,

我假設(shè) openssl 已經(jīng)安裝在你的系統(tǒng)上,如果未安裝,請使用以下 dnf 命令:

# dnf install openssl -y

使用下面的 openssl 命令生成私鑰和 CSR(證書簽名請求):

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

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Postfix-Key-CSR-CentOS8

現(xiàn)在,使用以下 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 = root@linuxtechi
Getting Private key
#

現(xiàn)在將私鑰和證書文件復(fù)制到 /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 服務(wù)以使上述更改生效:

# systemctl restart postfix

讓我們嘗試使用 mailx 客戶端將郵件發(fā)送到內(nèi)部本地域和外部域。

從 pkumar 發(fā)送內(nèi)部本地郵件到 postfixuser 中:

# echo "test email" | mailx -s "Test email from Postfix MailServer" -r root@linuxtechi root@linuxtechi

使用以下命令檢查并閱讀郵件:

# 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

怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器

Read-Postfixuser-Email-CentOS8

從 postfixuser 發(fā)送郵件到外部域(root@linuxtechi.com):

# echo "External Test email" | mailx -s "Postfix MailServer" -r root@linuxtechi root@linuxtechi

注意:如果你的 IP 沒有被任何地方列入黑名單,那么你發(fā)送到外部域的郵件將被發(fā)送,否則它將被退回,并提示你的 IP 被 spamhaus 之類的數(shù)據(jù)庫列入黑名單。

檢查 Postfix 郵件隊(duì)列

使用 mailq 命令列出隊(duì)列中的郵件:

# mailq
Mail queue is empty
#

完成!我們的 Postfix 配置正常工作了!目前就這樣了。我們希望你覺得本教程有見地,并且你可以輕松地設(shè)置本地 Postfix 服務(wù)器。

以上是“怎樣在CentOS8上安裝和配置Postfix郵件服務(wù)器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI