架設空殼郵件服務器

搭建內部郵箱服務器,可以在員工離職回收郵箱,避免文件泄露。

配置postfix

# 設置主機名
[root@localhost ~]# hostnamectl --static set-hostname mail.hellopasswd.com

# 查看主機名
[root@mail ~]# hostname

CentOS 7默認情況下已安裝postfix

# 檢測系統是否安裝postfix
[root@mail ~]# rpm -q postfix

# 安裝postfix
[root@mail ~]# yum install -y postfix

# 檢查postfix是否支持devecot代理
[root@mail ~]# postconf -a
# 修改postfix的主配置文件main.cf
[root@mail ~]# vi /etc/postfix/main.cf
將#myhostname = host.domain.tld去除註釋並修改爲myhostname = mail.hellopasswd.com    #修改主機名
將#mydomain = domain.tld去除註釋並修改爲mydomain = hellopasswd.com    #設置域名
將#myorigin = $myhostname去除註釋並修改爲myorigin = $mydomain    #設置發送郵件時mail from的值
去除註釋#inet_interfaces = all爲inet_interfaces = all    #設置監聽所有服務器接口
並將inet_interfaces = localhost註釋#inet_interfaces = localhost
在mydestination = $myhostname, localhost.$mydomain, localhost後添加$mydomain爲mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain    #設置郵件服務器可以接受哪些郵件
將#mynetworks = 168.100.189.0/28, 127.0.0.0/8去除註釋並修改爲mynetworks = 192.168.37.0/24, 127.0.0.0/8    #設置該服務器可以轉發郵件的網絡
將#relay_domains = $mydestination去除註釋並修改爲relay_domains = $mydomain    #該郵件服務器可以轉發的郵件域名,表示該服務器可以轉發本域名內的所有郵件
去除註釋#home_mailbox = Maildir/爲home_mailbox = Maildir/    #設置郵件的存儲位置,爲每一個郵件保存成一個文件

# 檢查是否存在語法錯誤
[root@mail ~]# postfix check
# 開放smtp所使用的TCP端口
[root@mail ~]# firewall-cmd --permanent --add-port=25/tc

# 開放postfix所使用的smtp協議
[root@mail ~]# firewall-cmd --permanent --add-service=smtp

# 加載防火牆
[root@mail ~]# firewall-cmd --reload

# 啓動postfix服務
[root@mail ~]# systemctl start postfix

# 設置開啓自啓
[root@mail ~]# systemctl enable postfix

# 查看postfix運行狀態
[root@mail ~]# systemctl status postfix

配置devecot

# 安裝dovecot
[root@mail ~]# yum install -y dovecot

# 檢查dovecot是否安裝成功
[root@mail ~]# rpm -q dovecot
# 修改dovecot服務配置
[root@mail ~]# vi /etc/dovecot/dovecot.conf
去除註釋#protocols = imap pop3 lmtp爲protocols = imap pop3 lmtp    #指定支持的收件協議
去除註釋#listen = *, ::爲listen = *, ::    #監聽本機的所有網絡接口
將#login_trusted_networks =去除註釋並添加login_trusted_networks = 192.168.37.0/24    #指定允許登錄的網絡地址,表示與服務器同一網段都允許登錄

# 修改郵件存儲位置
[root@mail ~]# vi /etc/dovecot/conf.d/10-mail.conf
去除註釋#   mail_location = maildir:~/Maildir爲mail_location = maildir:~/Maildir    #表示存儲郵件時,每一個郵件存儲成一個文件
# 開放pop3協議
[root@mail ~]# firewall-cmd --permanent --add-service=pop3

# 開放pop3協議端口號
[root@mail ~]# firewall-cmd --permanent --add-port=110/tcp

# 開放imap協議
[root@mail ~]# firewall-cmd --permanent --add-service=imap

# 開放imap協議端口號
[root@mail ~]# firewall-cmd --permanent --add-port=143/tcp

# 加載防火牆
[root@mail ~]# firewall-cmd --reload
# 啓動dovecot服務
[root@mail ~]# systemctl start dovecot

# 加入開機自啓
[root@mail ~]# systemctl enable dovecot

# 查看狀態
[root@mail ~]# systemctl status dovecot

郵件服務器創建用戶以及使用telnet服務

# 服務器端創建測試用戶組mail
[root@mail ~]# groupadd mail

# 創建測試用戶user1
[root@mail ~]# useradd -g mail -s /sbin/nologin user1

# 創建測試用戶user2
[root@mail ~]# useradd -g mail -s /sbin/nologin user2

# 設置測試用戶密碼
[root@mail ~]# passwd user1
123
[root@mail ~]# passwd user2
123
# 服務器安裝telnet服務器
[root@mail ~]# yum install -y telnet-server

# 啓動telnet服務
[root@mail ~]# systemctl start telnet.socket

# 將telnet服務設置爲開啓自啓
[root@mail ~]# systemctl enable telnet.socket

# 開放telnet服務
[root@mail ~]# firewall-cmd --permanent --add-service=telnet

# 開放telnet端口
[root@mail ~]# firewall-cmd --permanent --add-port=23/tcp

# 重新加載防火牆
[root@mail ~]# firewall-cmd --reload

客戶端收發郵件測試

# 客戶端安裝telnet軟件
[root@localhost ~]# yum install telnet.x86_64

# 硬解析
[root@localhost ~]# vi /etc/hosts
添加192.168.37.137    mail.hellopasswd.com    #添加服務器IP以及郵箱解析域名

# 連接郵件服務器的25端口,進行客戶端發送郵件測試
[root@localhost ~]# telnet mail.hellopasswd.com 25
mail from:[email protected]    #告知發件人
rcpt to:[email protected]    #告知收件人
DATA    #告知服務器要開始傳送數據
subject:The first mail    #郵件主題
Hello World!    #內容
.    #郵件已點結束
quit    #退出郵件服務器
# 客戶端連接服務器的110端口,進行客戶端接收郵件測試
[root@localhost ~]# telnet mail.hellopasswd.com 110
user user2    #收件人用戶名user2
pass 123    #user2的密碼
list    #列出郵箱中的所有郵件
retr 1    #檢索第一封郵件
quit    #退出並結束telnet會話

常見故障

# 接收郵件填寫用戶名時出現報錯信息
-ERR [AUTH] Plaintext authentication disallowed on non-secure (SSL/TLS) connections.

# 解決方法
[root@mail ~]# vi /etc/dovecot/conf.d/10-auth.conf
將#disable_plaintext_auth = yes去除註釋並修改disable_plaintext_auth = no

[root@mail ~]# vi /etc/dovecot/conf.d/10-ssl.conf
將ssl = required修改爲ssl = no

[root@mail ~]# systemctl restart dovecot
# 完整發送郵件內容
[root@localhost ~]# telnet 192.168.37.137 25
Trying 192.168.37.137...
Connected to 192.168.37.137.
Escape character is '^]'.
220 mail.hellopasswd.com ESMTP Postfix
mail from:[email protected]
250 2.1.0 Ok
rcpt to:[email protected]
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
subject:The first mail
Hello World!
.
250 2.0.0 Ok: queued as 9CE072019CD6
quit
221 2.0.0 Bye
Connection closed by foreign host.

# 完整接收郵件內容
[root@localhost ~]# telnet 192.168.37.137 110
Trying 192.168.37.137...
Connected to 192.168.37.137.
Escape character is '^]'.
+OK Dovecot ready.
user user2
+OK
pass 123
+OK Logged in.
list
+OK 1 messages:
1 335
.
retr 1
+OK 335 octets
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: from unknown (unknown [192.168.37.110])
	by mail.hellopasswd.com (Postfix) with SMTP id 9CE072019CD6
	for <[email protected]>; Thu, 30 Jul 2020 10:00:40 +0800 (CST)
subject:The first mail

Hello World!
.
quit
+OK Logging out.
Connection closed by foreign host.

配置空殼郵件服務器作爲郵件代理

# 空殼郵件服務的主機設置爲Null
[root@localhost ~]# hostnamectl --static set-hostname Null.hellopasswd.com

# 查看主機名
[root@Null ~]# hostname
# 修改postfix的主配置文件main.cf
[root@Null ~]# vi /etc/postfix/main.cf
將#myhostname = host.domain.tld去除註釋並修改爲myhostname = Null.hellopasswd.com    #修改郵件服務器主機名
將#mydomain = domain.tld去除註釋並修改爲mydomain = hellopasswd.com    #修改空殼服務器所在的域
將#myorigin = $myhostname去除註釋並修改爲myorigin = Null.com    #設置郵件服務器發送郵件是mail from的值
去除註釋#inet_interfaces = all爲inet_interfaces = all    #修改服務器的監聽接口
並將註釋inet_interfaces = localhost爲#inet_interfaces = localhost
將mydestination = $myhostname, localhost.$mydomain, localhost修改爲mydestination =    #由於空殼郵件服務器不接收任何郵件,因此將mydestination的值設置爲空
將#mynetworks = 168.100.189.0/28, 127.0.0.0/8去除註釋並修改爲mynetworks = 192.168.37.0/24, 127.0.0.0/8    #修改郵件服務器可以轉發郵件的網絡ip地址
將#relayhost = [an.ip.add.ress]去除註釋並修改爲relayhost = 192.168.37.137    #修改郵件可以轉發到指定的服務器

# 檢查是否存在語法錯誤
[root@Null ~]# postfix check
# 開放smtp協議服務
[root@Null ~]# firewall-cmd --permanent --add-service=smtp

# 開放smtp協議的TCP的25端口
[root@Null ~]# firewall-cmd --permanent --add-port=25/tcp

# 重新加載防護牆
[root@Null ~]# firewall-cmd --reload

# 啓動postfix服務
[root@Null ~]# systemctl start postfix

# 加入開機自啓
[root@Null ~]# systemctl enable postfix

# 查看postfix運行狀態
[root@Null ~]# systemctl status postfix

空殼郵件服務器發送郵件測試

# 通過安裝mailx使用mail命令在空殼郵件服務器進行發送郵件測試
[root@Null ~]# yum install -y mailx

[root@Null ~]# mail [email protected]    #發送郵件給user2
Subject: The last mail    #郵件主題
Goodbye!    #郵件內容
.    #郵件內容已點結束

# 查看日誌是否發送郵件成功
[root@Null ~]# cat /var/log/maillog
Jul 28 02:32:19 localhost postfix/postfix-script[2567]: starting the Postfix mail system
Jul 28 02:32:19 localhost postfix/master[2584]: daemon started -- version 2.10.1, configuration /etc/postfix
Jul 28 06:07:11 localhost postfix/pickup[19974]: 9234A20E472E: uid=0 from=<root>
Jul 28 06:07:11 localhost postfix/cleanup[20036]: 9234A20E472E: message-id=<[email protected]>
Jul 28 06:07:11 localhost postfix/qmgr[2602]: 9234A20E472E: from=<[email protected]>, size=447, nrcpt=1 (queue active)
Jul 28 06:07:13 localhost postfix/smtp[20038]: 9234A20E472E: to=<[email protected]>, relay=192.168.37.137[192.168.37.137]:25, delay=2.4, delays=0.09/0.05/2.2/0.03, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 7BFB92019CD6)
Jul 28 06:07:13 localhost postfix/qmgr[2602]: 9234A20E472E: removed

客戶端接收郵件測試

# 客戶端硬解析
[root@localhost ~]# vi /etc/hosts
添加192.168.37.137    mail.hellopasswd.com    #添加服務器IP以及郵箱解析域名

# 客戶端在通過110端口連接dovecot服務接收郵件測試
[root@localhost ~]# telnet mail.hellopasswd.com 110
Trying 192.168.37.137...
Connected to mail.hellopasswd.com.
Escape character is '^]'.
+OK Dovecot ready.
user user2
+OK
pass 123
+OK Logged in.
list
+OK 2 messages:
1 335
2 748
.
retr 2
+OK 748 octets
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: from Null.hellopasswd.com (unknown [192.168.37.110])
	by mail.hellopasswd.com (Postfix) with ESMTP id 7BFB92019CD6
	for <[email protected]>; Thu, 30 Jul 2020 11:03:51 +0800 (CST)
Received: by Null.hellopasswd.com (Postfix, from userid 0)
	id 9234A20E472E; Tue, 28 Jul 2020 06:07:11 +0800 (CST)
Date: Tue, 28 Jul 2020 06:07:11 +0800
To: [email protected]
Subject: The last mail
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (root)

Goodbye!
.
quit
+OK Logging out.
Connection closed by foreign host.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章