如何在CentOS8上安裝和配置Postfix郵件服務(wù)器的方法示例
Postfix 是一個(gè)自由開源的 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 配置沖突。例如,要?jiǎng)h除 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
Install-Postfix-Centos8
步驟 4)啟動(dòng)并啟用 Postfix 服務(wù)
成功安裝 Postfix 后,運(yùn)行以下命令啟動(dòng)并啟用 Postfix 服務(wù):
# systemctl start postfix # systemctl enable postfix
要檢查 Postfix 狀態(tài),請運(yùn)行以下 systemctl 命令:
# systemctl status postfix
Start-Postfix-check-status-centos8
太好了,我們已經(jīng)驗(yàn)證了 Postfix 已啟動(dòng)并正在運(yùn)行。接下來,我們將配置 Postfix 從本地發(fā)送郵件到我們的服務(wù)器。
步驟 5)安裝 mailx 郵件客戶端
在配置 Postfix 服務(wù)器之前,我們需要安裝 mailx,要安裝它,請運(yùn)行以下命令:
# dnf install mailx
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/
完成后,保存并退出配置文件。重新啟動(dòng) postfix 服務(wù)以使更改生效:
# systemctl restart postfix
步驟 7)測試 Postfix 郵件服務(wù)器
測試我們的配置是否有效,首先,創(chuàng)建一個(gè)測試用戶。
# useradd postfixuser # passwd postfixuser
接下來,運(yùn)行以下命令,從本地用戶 pkumar 發(fā)送郵件到另一個(gè)用戶 postfixuser。
# telnet localhost smtp 或者 # telnet localhost 25
如果未安裝 telnet 服務(wù),那么可以使用以下命令進(jìn)行安裝:
# dnf install telnet -y
如前所述運(yùn)行命令時(shí),應(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ā)送郵件到另一個(gè)本地用戶 postfixuser,如下所示:
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
Read-postfix-email-linux
Postfix 郵件服務(wù)器日志
Postfix 郵件服務(wù)器郵件日志保存在文件 /var/log/maillog 中,使用以下命令查看實(shí)時(shí)日志,
# tail -f /var/log/maillog
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
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
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ù)器。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Apache、Nginx下Font Awesome在 Firefox 中不顯示問題解決方法
這篇文章主要介紹了Apache、Nginx下Font Awesome在 Firefox 中不顯示問題解決方法,在配置中增加一個(gè)響應(yīng)頭即可解決這個(gè)問題,需要的朋友可以參考下2014-07-07Linux云服務(wù)器搭建SFTP服務(wù)器圖片服務(wù)器的操作
這篇文章主要介紹了Linux云服務(wù)器搭建SFTP服務(wù)器圖片服務(wù)器的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Apache配置參數(shù)deny和allow的使用實(shí)例
這篇文章主要介紹了Apache配置參數(shù)deny和allow的使用實(shí)例,需要的朋友可以參考下2015-06-06Linux下rsync遠(yuǎn)程數(shù)據(jù)同步命令的詳細(xì)介紹
rsync是一款開源的、快速的、多功能的、可實(shí)現(xiàn)全量及增量的本地或遠(yuǎn)程數(shù)據(jù)同步備份的優(yōu)秀工具。rsync軟件適用于unix/linux/windows等多種操作系統(tǒng)平臺。下面這篇文章主要介紹了Linux下rsync命令的相關(guān)資料,需要的朋友可以參考借鑒。2017-02-02linux如何將某個(gè)文件夾移動(dòng)到另一個(gè)文件夾下
這篇文章主要介紹了linux如何將某個(gè)文件夾移動(dòng)到另一個(gè)文件夾下問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06解決centos7中tomcat啟動(dòng)與本機(jī)訪問問題
這篇文章主要介紹了解決centos7中tomcat啟動(dòng)與本機(jī)訪問問題,需要的朋友可以參考下2018-08-08