天天看點

如何在 CentOS 8 上配置郵件伺服器 ?

作者:鸠摩智首席音效師
如何在 CentOS 8 上配置郵件伺服器 ?

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/hosts
192.168.1.13   server1.crazytechgeek.info           

3) 安裝 Postfix Mail Server

确認系統上沒有其他 MTA 正在運作後,執行指令安裝 Postfix

# dnf install postfix           
如何在 CentOS 8 上配置郵件伺服器 ?

4) 開啟 Postfix 服務

安裝成功後,啟動并啟用 Postfix 服務

# systemctl start postfix
# systemctl enable postfix           

檢查 Postfix 服務狀态

# systemctl status postfix           
如何在 CentOS 8 上配置郵件伺服器 ?

5) 安裝 mailx 郵件用戶端

在配置 Postfix 伺服器前,需要先安裝 mailx feature,使用此指令安裝 mailx feature

# dnf install mailx           
如何在 CentOS 8 上配置郵件伺服器 ?

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           

完整的資訊如下所示

如何在 CentOS 8 上配置郵件伺服器 ?

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           
如何在 CentOS 8 上配置郵件伺服器 ?

Postfix mail server 日志

使用下面的指令檢視實時日志

# tail -f /var/log/maillog           
如何在 CentOS 8 上配置郵件伺服器 ?

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           
如何在 CentOS 8 上配置郵件伺服器 ?

使用以下 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 = [email protected]
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 [email protected] [email protected]           

使用以下方法檢查并閱讀郵件

# 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           
如何在 CentOS 8 上配置郵件伺服器 ?

(2) 從 postfixuser 發送電子郵件到外部域 ([email protected])

# echo "External Test email" | mailx -s "Postfix MailServer" -r [email protected] [email protected]           

注意: 如果您的 IP 沒有被列入黑名單,那麼您的電子郵件将被發送到外部域,否則将被反彈,說 IP 在某某垃圾郵件資料庫中被列入黑名單。

檢視 Postfix 郵件隊列

使用 mailq 指令列出正在排隊的郵件

# mailq
Mail queue is empty
#           

我們希望本教程對您有所幫助,并希望您可以輕松地設定本地 Postfix 伺服器。

我的開源項目

如何在 CentOS 8 上配置郵件伺服器 ?

酷瓜雲課堂 - 開源線上教育解決方案

  • course-tencent-cloud(酷瓜雲課堂 - gitee 倉庫)
  • course-tencent-cloud(酷瓜雲課堂 - github 倉庫)

繼續閱讀