該系列文章只是本人的學習筆記,文章中的文字描述提取自《Linux鳥哥私房菜》《Linux運維之道》等書中的重點內容,部分內容是在培訓Linux運維時總結的筆記,化繁為簡能夠在工作中快速復習掌握重點,並不代表個人立場,但轉載請加出處,並注明參考文獻。
Postfix 是一種電子郵件服務器,它是由任職於IBM華生研究中心 (T.J. Watson Research Center) 的荷蘭籍研究員Wietse Venema為了改良sendmail郵件服務器而產生的.最早在1990年代晚期出現,是一個開放源代碼的軟件.
Postfix 是Wietse Venema在IBM的GPL協議之下開發的MTA(郵件傳輸代理)軟件.postfix是Wietse Venema想要為使用最廣泛的sendmail提供替代品的一個嘗試,在Internet世界中,大部分的電子郵件都是通過sendmail來投遞的,大約有100萬用戶使用sendmail,每天投遞上億封郵件.這真是一個讓人吃驚的數字,Postfix試圖更快、更容易管理、更安全,同時還與sendmail保持足夠的兼容性.
生活中常用的郵件服務器
Sendmail: 資格最古老,運行穩定,但安全性欠佳.
Postfix: 采用模塊化設計,在投遞效率、穩定性、性能及安全性方面表現優秀,與sendmail保持足夠的兼容性.
Qmail: 采用模塊化設計,速度快、執行效率高,配置稍微復雜點.
Outook:是一個客戶端代理.
FoxMail:也是客戶端代理.
PostFix的特點
1.PostFix是免費,開源的,並且兼容sendmail
2.性能好,大約比sendmail快三倍,一台運行postfix的台式pc每天可收發上百萬郵件.
3.安全性,PostFix具有多層防御結構,可以有效地抵御惡意入侵者,可以運行在較低的權限之下.
4.靈活性好,postfix是由超過一打的小程序組成的,每個程序完成特定功能.可以通過配置設置每個程序的運行參數.
5.健壯穩定,postfix被設計成在重負荷之下仍然可以正常工作.當系統運行超出了可用的內存或磁盤空間時,postfix會自動減少運行進程的數目.當處理的郵件數目增長時,Postfix運行的進程不會跟着增加.
PostFix工作流程圖解
1.客戶端通過Outlook或網頁客戶端軟件,向郵件服務器發送一封郵件,首先郵件服務器會檢查是發往本域,還是發往其他域.
2.如果發送到本域直接存放到Mailbox中,另外一個用戶上線,需要收取這封郵件,借助dovecot軟件,通過dovecot進入數據庫驗證身份,驗證通過以后,就可以通過MRA調用POP3或者IMAP4的協議,進入郵箱收取郵件,並且將郵件轉發給用戶.
3.如果發送到其他域,轉發之前驗證用戶身份,是否要為其轉發郵件,驗證的時候是用到了SASL函數庫,用戶的信息是放到了數據庫中,此時我們需要一個中間層authlib,通過authlib替SASL函數庫向數據庫進行驗證,做"發信認證",認證通過允許轉發,驗證不通過拒絕轉發.
4.通過瀏覽器訪問郵件服務器(EXtmall/FoxMali),轉發到其他域,放到郵箱,到數據庫驗證.
關於郵件系統中的角色
MUA:郵件用戶代理 (sendmail qmail postfix)
MTA:郵件傳輸代理 (outlook)
MDA:郵件分發代理 (maildrop)
MRA:郵件檢索代理 (dovecot)
postfix: 實現郵件發送服務器的角色.
dovecot: 作為郵件接收服務器的角色.
extmail: 實現利用web界面收發信件.
extman: 用戶注冊同個extman的頁面.
mysql: 存放用戶的賬戶信息.
cyrus-sasl,courier-authlib: 實現用戶的身份驗證.
郵件應用使用協議
1.SMTP,簡單郵件傳輸協議,TCP 25端口,加密時使用TCP 465端口,(發送郵件),客戶端向其他服務器發送郵件或者服務器向其他服務器轉發郵件,發送方使用的都是SMTP協議.
2.POP3,第3版郵局協議,TCP 110端口,加密時使用TCP 995端口,(收郵件) 收取郵件的時候,先將服務器的郵件下載到本地,所有的操作都是在本地完成.
3.IMAP4,第4版互聯網消息訪問協議,TCP 143端口,加密時使用 993端口,(比POP3靈活)接收郵件,客戶端和服務器端交互了以后,在服務器端處理,好處能實現更多的功能,例如查看郵件的信息,但是消耗的系統的資越高.
服務器接收郵件的過程
在開始講述本例的內容之前,我們先來看看Postfix+Dovecot+MySQL是如何相互協作以實現郵件服務器的各項功能。我們假設 張三通過oschina.net的郵箱zhangsan@oschina.net發送一份郵件給 李四lisi@mydomain.com,則服務器接收郵件的過程大致如下圖所示:
1.zhangsan@oschina.net發送郵件到lisi@mydomain.com
2.oschina.net服務器會通過DNS查詢mydomain.com的MX記錄並找到Postfix所在的服務器
3.郵件被發送給Postfix
4.Postfix轉向MySQL求助,以查詢mydomain.com是不是需要處理的域名
5.MySQL返回確認信息給Postfix
6.Postfix將接受到的郵件投遞給Dovecot的LMTP服務以便做處理
7.Dovecot將郵件內容保存到lisi@mydomain.com用戶對應的磁盤路徑
用戶查收郵件的過程
上述例子中我們看到了服務器接收郵件的過程,接下來我們看看,當大家通過用戶名密碼登錄郵箱查看郵件時,會發生什么事情:
1.郵件客戶端 to Dovecot:尊敬的Dovecot大人,您好!我闊以建立一個IMAP加密連接嗎?
2.Dovecot to 郵件客戶端:當然闊以。這是我的SSL證書,請您告訴我帳號和密碼!
3.郵件客戶端 to Dovecot:好滴!這是我的用戶名和密碼,千萬不要告訴別人哦!
4.Dovecot to MySQL:Hi 美女!請問下,這個用戶名和密碼是正確的嘛?
5.MySQL to Dovecot:好的,請稍后!呃……這個用戶名和密碼是正確的哦!
6.Dovecot打開存放在本地磁盤/var/mail/mydomain.com/llisi的mailbox
7.Dovecot獲取到最新的郵件及其他相關信息
8.Dovecot將郵件及其相關的其他信息通過IMAP協議發送給客戶端
用戶發送郵件的過程
查收了最新的郵件之后,李四lisi@mydomain.com發現張三給他發來了郵件。現在,李四回復一封郵件給張三,會發生什么事情:
1.郵件客戶端 to Postfix:尊敬的Postfix大人,您好!我闊以建立一個安全的SMTP連接嘛?
2.Postfix to 郵件客戶端:說人話!可以就是可以,干嘛要說「闊以」啊!你丫賤啊,找抽啊!想建立SMTP連接可以,不過要加密。這是我的SSL證書,告訴我你的帳號和密碼!
3.郵件客戶端 to Postfix:對不起,大人,我錯鳥!這是我的帳號和密碼,不要告訴別人哦!
4.Postfix to Dovecot:Hi 帥哥,幫我查一下這個帳號和密碼!
5.Dovecot to MySQL:Hi 美女,這個帳號和密碼是正確的呢?!還是正確的呢?!還是……
6.MySQL to Dovecot:好啦,你乖啦!我查過啦,這個帳號密碼是正確的啦!表賣萌哦,Dovecot君!
7.Dovecot to Postfix:大鍋,則個帳號密碼斯正缺滴!
8.Postfix to 郵件客戶端:賤人,過來!你給的帳號密碼是正確的,允許你發送郵件!
9.郵件客戶端將編寫好的郵件通過SMTP協議發送給Postfix
10.Postfix將收到的郵件轉發給對方
面試技巧
SMTP 簡單郵件傳輸協議(發郵件)TCP 25端口,加密時使用TCP 465端口
POP3 第三版郵局協議(收郵件)TCP 110 端口,加密時使用 TCP 995端口
IMAP4 第四版互聯網郵件訪問協議(收郵件)TCP 143端口,加密時使用TCP 993端口
配置DNS域名解析
首先我們需要配置bind為客戶端提供dns域名解析
1.安裝bind,為客戶提供解析服務
[root@localhost ~]# yum install -y bind
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Server 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package bind.x86_64 32:9.9.4-61.el7 will be installed
--> Processing Dependency: bind-libs = 32:9.9.4-61.el7
......
Installed:
bind.x86_64 32:9.9.4-61.el7
Dependency Installed:
bind-libs.x86_64 32:9.9.4-61.el7
Complete!
[root@localhost ~]#
2.修改dns主配置文件,區域文件和域名配置文件等
[root@localhost ~]# vim /etc/named.conf
12 options {
13 listen-on port 53 { any; }; #修改IPV4的偵聽端口
14 listen-on-v6 port 53 { ::any; }; #修改IPV6的偵聽端口
15 directory "/var/named";
16 dump-file "/var/named/data/cache_dump.db";
17 statistics-file "/var/named/data/named_stats.txt";
18 memstatistics-file "/var/named/data/named_mem_stats.txt";
19 allow-query { any; }; #允許迭代
20
21 /*
3.修改named.rfc1912.zones區域配置文件,或者寫在named.conf的底部也可以
[root@localhost ~]# vim /etc/named.rfc1912.zones
42
43 zone "lyshark.com" IN { #添加此處區域文件
44 type master; #聲明為主dns
45 file "lyshark.com.zone"; #聲明區域配置文件名稱
46 allow-update {none;}; #是否迭代
47 };
4.拷貝默認區域配置文件,修改成lyshark.com.zone並做郵件交換記錄配置
[root@localhost ~]# cd /var/named/
[root@localhost named]#
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]#
[root@localhost named]# cp -a named.localhost lyshark.com.zone
[root@localhost named]#
[root@localhost named]# ls
data dynamic lyshark.com.zone named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]#
[root@localhost named]# vim lyshark.com.zone
1 $TTL 1D
2 @ IN SOA dns.lyshark.com. root.lyshark.com. (
3 0 ; serial
4 1D ; refresh
5 1H ; retry
6 1W ; expire
7 3H ) ; minimum
8 NS dns.lyshark.com.
9 MX 3 mail.lyshark.com.
10 dns A 127.0.0.1
11 mail A 192.168.1.20
12 AAAA ::1
5.重啟named解析服務,並設置成開機自啟動
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl enable named
6.此處在其他電腦配置好,dns解析地址,解析到本機,並測試是否能解析
C:\Users\LyShark>ping 192.168.1.20
正在 Ping 192.168.1.20 具有 32 字節的數據:
來自 192.168.1.20 的回復: 字節=32 時間<1ms TTL=64
C:\Users\LyShark>nslookup mail.lyshark.com
服務器: UnKnown
Address: 192.168.1.20
名稱: mail.lyshark.com
Addresses: ::1
192.168.1.20
C:\Users\LyShark>
配置PostFix服務程序
postfix是由IBM贊助研發的一款免費開源的電子郵件服務程序,它能夠很好的兼容sendmail,可以很方便的將sendmail用戶遷移到postfix服務上,postfix服務的收發郵件能力強於sendmail,而且能夠自動,增加,減少進程的數量來保證郵件服務器的高性能運轉,postfix有許多小模塊組成,方便了功能的添加,拓展和刪除,提高了可用性,用戶可以靈活搭配使用.
1.安裝postfix服務程序,rhel7系統默認集成,這里只做演示
[root@localhost ~]# yum install -y postfix
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 2:postfix-2.10.1-6.el7.x86_64 already installed and latest version
Nothing to do
2.配置郵局系統的主機名,在第76行,一個名為myhostname的變量,用來保存服務器主機名
[root@localhost ~]# vim /etc/postfix/main.cf
68 # INTERNET HOST AND DOMAIN NAMES
69 #
70 # The myhostname parameter specifies the internet hostname of this
71 # mail system. The default is to use the fully-qualified domain name
72 # from gethostname(). $myhostname is used as a default value for many
73 # other configuration parameters.
74 #
75 #myhostname = host.domain.tld
76 myhostname = mail.lyshark.com #解除注釋,定義服務器主機名
77
3.配置郵局系統的域名,在第83行,一個名為mydomain的變量,用來定義郵件域
[root@localhost ~]# vim /etc/postfix/main.cf
78 # The mydomain parameter specifies the local internet domain name.
79 # The default is to use $myhostname minus the first component.
80 # $mydomain is used as a default value for many other configuration
81 # parameters.
82 #
83 mydomain = lyshark.com #解除注釋,指定郵件域
84
4.配置本機發出的域名稱,在第99行,一個名為myorigin的變量,用來定義郵件域
[root@localhost ~]# vim /etc/postfix/main.cf
94 # For the sake of consistency between sender and recipient addresses,
95 # myorigin also specifies the default domain name that is appended
96 # to recipient addresses that have no @domain part.
97 #
98 myorigin = $myhostname #解除注釋,設置允許發信的用戶的郵件域
99 myorigin = $mydomain #解除注釋,例:admin@lyshark.com
100
5.配置偵聽網卡接口,在第116行,一個名為inet_interfaces的變量,用來定義網卡接口
[root@localhost ~]# vim /etc/postfix/main.cf
108 # See also the proxy_interfaces parameter, for network addresses that
109 # are forwarded to us via a proxy or network address translator.
110 #
111 # Note: you need to stop/start Postfix when this parameter changes.
112 #
113 inet_interfaces = all #解除注釋,設置所有IP都提供郵件服務
114 #inet_interfaces = $myhostname
115 #inet_interfaces = $myhostname, localhost
116 #inet_interfaces = localhost #注釋掉
117
6.配置接收郵件的主機名或域名,在第164行,一個名為mydestination的變量,用來定義可接收郵件的主機名或域名列表,這里可以直接調用前面的兩個變量myhostname和mydomain變量(如果不想調用變量,也可直接調用變量中的值)
[root@localhost ~]# vim /etc/postfix/main.cf
156 # Specify a list of host or domain names, /file/name or type:table
157 # patterns, separated by commas and/or whitespace. A /file/name
158 # pattern is replaced by its contents; a type:table is matched when
159 # a name matches a lookup key (the right-hand side is ignored).
160 # Continue long lines by starting the next line with whitespace.
161 #
162 # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
163 #
164 #mydestination = $myhostname, localhost.$mydomain, localhost #注釋掉,設置取出那些郵件
165 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain #解除注釋
166 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
167 # mail.$mydomain, www.$mydomain, ftp.$mydomain
7.配置郵件存儲位置,在第619行,一個名為home_mailbox的變量,用來定義郵件保存位置
[root@localhost ~]# vim /etc/postfix/main.cf
413 # The home_mailbox parameter specifies the optional pathname of a
414 # mailbox file relative to a user's home directory. The default
415 # mailbox file is /var/spool/mail/user or /var/mail/user. Specify
416 # "Maildir/" for qmail-style delivery (the / is required).
417 #
418 #home_mailbox = Mailbox
419 home_mailbox = Maildir/ #解除注釋
420
8.最后,創建一個郵件測試賬號admin,重啟postfix服務,並設置成開機自啟動
[root@localhost ~]# useradd -s /sbin/nologin admin
[root@localhost ~]# passwd admin
Changing password for user admin.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# systemctl enable postfix
配置Dovecot服務程序
dovecot是一款能夠為linux系統提供IMAP和POP3電子郵件服務的開源服務程序,安全性極高,配置簡單,執行效率高,而且占用服務器資源極低,是一款很好的收件服務器.
1.通過yum安裝dovecot服務程序的軟件包,和依賴
[root@localhost ~]# yum install -y dovecot dovecot-devel clucene-core
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager .
Package 1:dovecot-2.2.10-8.el7.x86_64 already installed and latest version
Package clucene-core-2.3.3.4-11.el7.x86_64 already installed and latest version
Nothing to do
2.配置dovecot服務,在第24行,把服務的電子郵件協議改為 imap,pop3和lmtp,在這一行下面添加 disable_plaintext_auth 允許用戶明文使用郵件系統.
[root@localhost ~]# vim /etc/dovecot/dovecot.conf
16 # Default values are shown for each setting, it's not required to uncomment
17 # those. These are exceptions to this though: No sections (e.g. namespace {})
18 # or plugin settings are added by default, they're listed only as examples.
19 # Paths are also just examples with the real defaults being based on configure
20 # options. The paths listed here are for configure --prefix=/usr
21 # --sysconfdir=/etc --localstatedir=/var
22
23 # Protocols we want to be serving.
24 protocols = imap pop3 lmtp #取消注釋,開啟驗證類型
25 disable_plaintext_auth = no #添加字段,開啟允許使用明文傳輸
3.配置允許登陸的網段,在第48行,設置允許來自於指定網段的主機使用,如果想任何人則無需修改本參數
[root@localhost ~]# vim /etc/dovecot/dovecot.conf
45 # Space separated list of trusted network ranges. Connections from these
46 # IPs are allowed to override their IP addresses and ports (for logging and
47 # for authentication checks). disable_plaintext_auth is also ignored for
48 # these networks. Typically you'd specify your IMAP proxy servers here.
49 login_trusted_networks = 192.168.1.0/24 #允許指定網段使用,屏蔽掉默認all
50
4.配置郵箱位置,子配置文件中,在第24行
[root@localhost ~]# vim /etc/dovecot/conf.d/10-mail.conf
22 # See doc/wiki/Variables.txt for full list. Some examples:
23 #
24 mail_location = maildir:~/Maildir #解除注釋
25 # mail_location = mbox:~/mail:INBOX=/var/mail/%u
26 # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
5.重啟收件服務器,並設置開機自啟動
[root@localhost ~]# systemctl restart dovecot
[root@localhost ~]# systemctl enable dovecot
配置SSL加密傳輸
由於上面的過程配置的郵件服務器使用的是明文傳輸,無法將其用於生產環境,下面的步驟將開啟郵件服務器的SSL加密模式,讓其可以在生產環境中使用
1.生成SSL密鑰文件,並自行放入合適的位置
[root@localhost ~]# openssl genrsa -des3 -out server.key 1024
[root@localhost ~]# openssl req -new -key server.key -out server.csr
[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt
[root@localhost ~]# ls
server.csr server.key servernew.crt
[root@localhost ~]# cp -a * /etc/pki/CA/certs/
[root@localhost ~]# ls /etc/pki/CA/certs/
server.csr server.key servernew.crt
2.修改PostFix配置文件,在文件中修改以下幾個位置,啟用SSL加密參數
[root@localhost ~]# vim /etc/postfix/main.cf
681 # SSL CRT 添加如下配置到末尾
682
683 smtpd_use_tls = yes
684 smtpd_tls_cert_file = /etc/pki/CA/certs/servernew.crt
685 smtpd_tls_key_file = /etc/pki/CA/certs/server.key
686 smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
[root@localhost ~]# vim /etc/postfix/master.cf
25 # -o milter_macro_daemon_name=ORIGINATING
26 smtps inet n - n - - smtpd
27 # -o syslog_name=postfix/smtps
28 -o smtpd_tls_wrappermode=yes
3.接着修改dovecot額配置文件,指定開啟SSL模式,並指定證書位置.
[root@localhost ~]# vim /etc/dovecot/conf.d/10-ssl.conf
5 # SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
6 # disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
7 # plain imap and pop3 are still allowed for local connections
8 ssl = yes
9
10 # PEM encoded X.509 SSL/TLS certificate and private key. They're opened before
11 # dropping root privileges, so keep the key file unreadable by anyone but
12 # root. Included doc/mkcert.sh can be used to easily generate self-signed
13 # certificate, just make sure to update the domains in dovecot-openssl.cnf
14 ssl_cert = </etc/pki/CA/certs/dovecot.pem
15 ssl_key = </etc/pki/CA/private/dovecot.pem
4.重啟PostFix和Dovecot服務,並設置開機自啟動.
[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# systemctl restart dovecot
[root@localhost ~]# systemctl enable postfix
[root@localhost ~]# systemctl enable dovecot
5.如果iptables在開啟狀態,那么設置iptables開放SMTPS/POP3S/IMAPS的端口.
[root@localhost ~]# iptables -I INPUT 1 -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT
[root@localhost ~]# iptables -I INPUT 2 -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT
[root@localhost ~]# iptables -I INPUT 3 -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT