配置Postfix MTA和IMAP/POP3



為了從我們的CentOS 7伺服器傳送電子郵件,我們需要設定一個現代郵件傳輸代理(MTA)。郵件傳輸代理是負責透過SMTP為系統使用者或公司網際網路域名傳送出站郵件的守護程序。

值得注意的是,本教程只講解了為本地使用設定守護程序的過程。我們不會詳細介紹為業務運營設定MTA的高階配置。這需要許多技能,包括但不限於:DNS、獲取未列入黑名單的靜態可路由IP地址以及配置高階安全和服務設定。簡而言之,本教程旨在使您熟悉基本配置。請勿將本教程用於面向網際網路主機的MTA配置。

由於其同時注重安全性和易於管理,我們選擇**Postfix**作為本教程的MTA。CentOS舊版本中安裝的預設MTA是*Sendmail*。**Sendmail**是一個很棒的MTA。然而,據作者拙見,Postfix在解決MTA的以下注意事項時達到了一個最佳點。在CentOS的最新版本中,Postfix已取代Sendmail成為預設MTA。

Postfix是一個廣泛使用且文件齊全的MTA。它得到積極維護和開發。它只需要最少的配置(這只是電子郵件),並且系統資源效率高(同樣,這只是電子郵件)。

**步驟1** - 從YUM包管理器安裝Postfix。

[root@centos]# yum -y install postfix

**步驟2** - 配置Postfix配置檔案。

Postfix配置檔案位於:/etc/postfix/main.cf

在簡單的Postfix配置中,必須為特定主機配置以下內容:主機名、域名、來源、inet_interfaces和目標。

**配置主機名** - 主機名是Postfix主機的完全限定域名。在OpenLDAP章節中,我們將CentOS主機命名為:centos,域名是vmnet.local。讓我們在本節中繼續使用它。

# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
myhostname = centos.vmnet.local

**配置域名** - 如上所述,本教程中將使用的域名是vmnet.local

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
mydomain = vmnet.local

**配置來源** - 對於單伺服器和域名設定,我們只需要取消以下部分的註釋並保留預設的Postfix變數。

# SENDING MAIL
#
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to append $myhostname,
# which is fine for small sites.  If you run a domain with multiple
# machines, you should (1) change this to $mydomain and (2) set up
# a domain-wide alias database that aliases each user to
# user@that.users.mailhost.
#
# For the sake of consistency between sender and recipient addresses,
# myorigin also specifies the default domain name that is appended
# to recipient addresses that have no @domain part. 
#
myorigin = $myhostname
myorigin = $mydomain

**配置網路介面** - 我們將讓Postfix監聽我們的單個網路介面以及與該介面關聯的所有協議和IP地址。這隻需保留Postfix的預設設定即可。

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes. 
#
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost
# Enable IPv4, and IPv6 if supported
inet_protocols = all

**步驟3** - 為Postfix配置SASL支援。

如果沒有SASL身份驗證支援,Postfix將只允許從本地使用者傳送電子郵件。或者,當用戶從本地域名之外發送電子郵件時,它會顯示*relaying denied*錯誤。

**注意** - **SASL**或**簡單應用安全層框架**是一個旨在支援不同應用層協議之間不同技術的身份驗證框架。SASL的開發人員(和使用者)沒有將身份驗證機制留給應用層協議,而是利用當前的身份驗證協議來實現更高級別的協議,這些協議可能沒有方便或更安全身份驗證(在談論對安全服務的訪問時)內建的功能。

安裝“cyrus-sasl*”包

[root@centos]# yum -y install  cyrus-sasl 
Loaded plugins: fastestmirror, langpacks 
Loading mirror speeds from cached hostfile 
 * base: repos.forethought.net 
 * extras: repos.dfw.quadranet.com 
 * updates: mirrors.tummy.com 
Package cyrus-sasl-2.1.26-20.el7_2.x86_64 already installed and latest version
Nothing to do

為SASL Auth配置/etc/postfix/main.cf

smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_sasl_security_options = noanonymous
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

我在main.conf中的SASL選項

##Configure SASL Options Entries:
smtpd_sasl_auth_enable = yes
smptd_recipient_restrictions =
permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtp_sasl_type = dovecot
smtp_sasl_path = private/auth/etc

**步驟4** - 配置FirewallD以允許傳入SMTP服務。

[root@centos]# firewall-cmd --permanent --add-service=smtp 
success

[root@centos]# firewall-cmd --reload 
success

[root@centos]#

現在讓我們檢查一下我們的CentOS主機是否允許並響應埠25(SMTP)上的請求。

Nmap scan report for 172.16.223.132 
Host is up (0.00035s latency). 
Not shown: 993 filtered ports 
PORT    STATE  SERVICE 
   20/tcp  closed ftp-data 
   21/tcp  open   ftp 
   22/tcp  open   ssh 
   25/tcp  open   smtp 
   80/tcp  open   http 
   389/tcp open   ldap 
   443/tcp open   https 
MAC Address: 00:0C:29:BE:DF:5F (VMware)

如您所見,SMTP正在監聽,守護程序正在響應我們內部LAN的請求。

安裝Dovecot IMAP和POP3伺服器

Dovecot是一個安全的IMAP和POP3伺服器,旨在處理中小型組織的傳入郵件需求。由於它在CentOS中廣泛使用,我們將使用Dovecot作為在CentOS上安裝和配置傳入郵件伺服器以及MTA SASL提供者的示例。

如前所述,我們不會為DNS配置MX記錄或建立安全規則以允許我們的服務處理域的郵件。因此,僅在面向網際網路的主機上設定這些服務可能會在沒有SPF記錄的情況下留下安全漏洞。

**步驟1** - 安裝Dovecot。

[root@centos]# yum -y install dovecot

**步驟2** - 配置dovecot。

dovecot的主要配置檔案位於:/etc/dovecot.conf。我們將首先備份主配置檔案。在進行編輯之前始終備份配置檔案是一個好習慣。這樣,如果(例如)換行符被文字編輯器破壞,多年的更改就不會丟失。恢復就像將當前備份複製到生產環境一樣簡單。

dovecot啟用協議和守護程式服務

# Protocols we want to be serving. 
protocols = imap imaps pop3 pop3s

現在,我們需要啟用dovecot守護程序以在啟動時監聽 -

[root@localhost]# systemctl start  dovecot 
[root@localhost]# systemctl enable dovecot

讓我們確保Dovecot在為imap、pop3、imap安全和pop3安全指定的埠上本地監聽。

[root@localhost]# netstat -antup | grep dovecot 
 tcp        0        0 0.0.0.0:110        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:143        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:993        0.0.0.0:*        LISTEN        4368/dovecot
 tcp        0        0 0.0.0.0:995        0.0.0.0:*        LISTEN        4368/dovecot
 tcp6       0        0 :::110                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::143                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::993                :::*          LISTEN        4368/dovecot
 tcp6       0        0 :::995                :::*          LISTEN        4368/dovecot

[root@localhost]#

如所見,dovecot正在為IPv4和IPv6監聽指定的埠。

POP3 110
POP3s 995
IMAP 143
IMAPs 993

現在,我們需要建立一些防火牆規則。

[root@localhost]# firewall-cmd --permanent --add-port=110/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=143/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=995/tcp 
success
 
[root@localhost]# firewall-cmd --permanent --add-port=993/tcp 
success
 
[root@localhost]# firewall-cmd --reload 
success
 
[root@localhost]#

我們的傳入郵件伺服器正在接受對LAN上主機的POP3POP3sIMAPIMAPs的請求。

Port Scanning host: 192.168.1.143

   Open TCP Port:   21          ftp 
   Open TCP Port:   22          ssh 
   Open TCP Port:   25          smtp 
   Open TCP Port:   80          http 
   Open TCP Port:   110         pop3 
   Open TCP Port:   143         imap 
   Open TCP Port:   443         https 
   Open TCP Port:   993         imaps 
   Open TCP Port:   995         pop3s 
廣告