郵件服務器配置

郵件服務器
postfix 僅提供 smtp 服務,不提供 pop3 和 imap 服務,主要是用發送和接收郵件的(接收到的郵件後,一般轉交 dovecot 處理,dovecot 負責將 postfix 轉發過來的郵件保存到服務器硬盤上)
dovecot 僅提供 pop3 和 imap 服務,不提供 smtp 服務(Foxmail之類的郵箱客戶端,都是通過pop3 和 imap 來收發郵件的。發郵件時,dovecot 會將郵件轉交給 postfix 來發送)
Postfix是一個由IBM資助下由Wietse Venema 負責開發的一個自由軟件工程產物
郵件服務器
三個協議:
SMTP:Simple Mail Transfer Protocol/簡單郵件傳送協議 定義郵件傳送,基於TCP服務的應用層, 明文傳送,SMTP協議使用25端口
POP3:Post Office Protocol 3/郵局協議第三版,是從郵件服務器中下載郵件存起來支持不在線用戶,基於TCP/IP,明文,使用110端口
IMAP:Internet Message Access Protocol/英特網信息存取協議 也叫郵件同步協議,將郵件留在服務器端直接對郵件進行管理、操作,比POP3更先進支持郵件頭部預覽主題來源,基於TCP/IP,使用143端口,

POP3協議允許電子郵件客戶端下載服務器上的郵件,但是在客戶端的操作(如移動郵件、標記已讀等),不會反饋到服務器上,比如通過客戶端收取了郵箱中的3封郵件並移動到其他文件夾,郵箱服務器上的這些郵件是沒有同時被移動的 。
IMAP提供webmail與電子郵件客戶端之間的雙向通信,客戶端的操作都會反饋到服務器上,對郵件進行的操作,服務器上的郵件也會做相應的動作。
postfix管郵件收發,也就是SMTP服務器
dovecot負責郵件管理,上傳,下載,刪除等。也就是POP3/IMAP服務器
Dovecot: 是一個非常優秀的IMAP/POP服務器用以接收外界發送到本機的郵件。通常,Dovecot的工作內容包括:驗證用戶身份以確保郵件不會被泄露。

MUA:Mail User Agent,郵件用戶代理
MTA:Mail Transfer Agent,郵件傳輸代理代爲傳遞,Sendmail和Postfix就是扮演MTA的角色。
MDA:Mail Delivery Agent,郵件投遞代理
郵件到達MDA後,就存放在某個文件或特殊的數據庫裏,我們將這個長期保存郵件的地方稱之爲郵箱。
一旦郵件到達郵箱,就原地不動了,等用戶再通過MUA將其取走,就是用Outlook,Foxmail等軟件收信的過程。

一封郵件的流程是:
發件人:MUA –發送–> MTA -> 若干個MTA… -> MTA -> MDA <–收取– MUA:收件人
MUA到MTA,以及MTA到MTA之間使用的協議就是SMTP協議,而收郵件時,MUA到MDA之間使用的協議最常用的是POP3或IMAP
專業郵件服務商都有大量的機器來爲用戶服務,所以通常MTA和MDA並不是同一臺服務器,因此,在Outlook等軟件裏,我們需要分別填寫SMTP發送服務器的地址和POP3接收服務器的地址

默認安裝郵件服務,並啓動
[root@localhost ~]# vim /etc/postfix/main.cf
76 myhostname = mail.baidu.com配置郵件服務器主機名
83 mydomain = baidu.com配置域名
99myorigin=$mydomain
113 inet_interfaces = all指定網絡接口

116#inet_interfaces = localhost
164mydestination=myhostname,localhost. mydomain, localhost,$mydomain指定服務器的目標區域

264 mynetworks = 172.16.50.0/24, 127.0.0.0/8
296 Relay_domains = $mydestination 轉發域

[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# mail to [email protected]
Subject: jhdafds
jhdpweiyrqpuvhfe
EOT
按下CTRL+d按鍵保存發送,回顯一個標記
[root@localhost ~]# su - redhat
Last login: Mon Aug 6 07:24:48 EDT 2018 on :0
[redhat@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/redhat”: 1 message 1 new
>N 1 root Mon Aug 6 10:48 18/572 “jhdafds”
& 1
Message 1:
From [email protected] Mon Aug 6 10:48:59 2018
Return-Path: [email protected]
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Mon, 06 Aug 2018 10:48:58 -0400
To: [email protected], [email protected]
Subject: jhdafds
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

jhdpweiyrqpuvhfe
& Held 1 message in /var/spool/mail/redhat

郵件羣發:
[root@localhost ~]# vim /etc/aliases
nfsnobody: root
ingres: root
system: root
toor: root
manager: root
dumper: root
abuse: root
newsadm: news
newsadmin: news
usenet: news
ftpadm: ftp
ftpadmin: ftp
ftp-adm: ftp
ftp-admin: ftp
www: webmaster
webmaster: root
noc: root
security: root
hostmaster: root
info: postmaster
marketing: postmaster
sales: postmaster
support: postmaster
# trap decode t catch security attacks
decode: root
# Person who should get root’s mail
#root: marc
workgroup: maomao,redhat,xixi
workgroup1: xix,maomao
“/etc/aliases” 98L, 1571C
讀取/etc/aliases.db文件
使用postmail命令將別名文件轉化爲數據庫
[root@localhost ~]# postalias /etc/aliases
[root@localhost ~]# mail to [email protected]
Subject: test
ddjafhoiahva;k
EOT
[root@localhost ~]# su - maomao
[maomao@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/maomao”: 1 message 1 new
>N 1 root Mon Aug 6 11:10 18/576 “test”
& 1
Message 1:
From [email protected] Mon Aug 6 11:10:58 2018
Return-Path: [email protected]
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Mon, 06 Aug 2018 11:10:57 -0400
To: [email protected], [email protected]
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R
ddjafhoiahva;k
& Held 1 message in /var/spool/mail/maomao
[root@localhost ~]# mail to [email protected]
Subject: test24
lalalalal
EOT
[root@localhost ~]# su - redhat
Last login: Mon Aug 6 10:57:14 EDT 2018 on pts/0
[redhat@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/redhat”: 3 messages 1 new
1 root Mon Aug 6 10:48 19/583 “jhdafds”
2 root Mon Aug 6 10:57 19/579 “hfslak”
>N 3 root Mon Aug 6 11:10 18/576 “test”
& Held 3 messages in /var/spool/mail/redhat

[redhat@localhost ~]$ exit
logout

[root@localhost ~]# su - maomao
Last login: Mon Aug 6 11:11:05 EDT 2018 on pts/0
[maomao@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/maomao”: 2 messages 1 new
1 root Mon Aug 6 11:10 19/587 “test”
>N 2 root Mon Aug 6 11:16 18/576 “test24”
& Held 2 messages in /var/spool/mail/maomao

配置dovecot服務
dovecot: 郵件接收服務
Dovecot是一款能夠爲Linux系統提供IMAP和POP3電子郵件服務的開源軟件程序,

[maomao@localhost ~]$ yum install dovecot -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
You need to be root to perform this command.

[root@localhost ~]# vim /etc/dovecot/dovecot.conf
24 protocols = imap pop3 lmtp
48 login_trusted_networks = 172.16.50.0/24

[root@localhost ~]# vim /etc/dovecot/conf.d/10-mail.conf
把25行復制並去掉#號
mail_location = mbox:~/mail:INBOX=/var/mail/%u

[root@localhost ~]# chmod 0600 /var/mail/*
如果不執行上面這個命令,可能會出現在系統中可以收到郵件,但是客戶端無法收到郵件,注意查看系統/var/log/maillog日誌
配置sasl服務
開啓SASL認證
[root@localhost ~]# vim /etc/postfix/main.cf
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination

[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# systemctl restart dovecot
[root@localhost ~]# systemctl restart saslauthd
[root@localhost ~]# systemctl stop firewalld
這裏寫圖片描述

間域收發

一、配置baidu.com域
配置postfix服務
[root@localhost ~]# vi /etc/postfix/main.cf
76 myhostname = mail.baidu.com
83 mydomain = baidu.com
99 myorigin = mydomain113inetinterfaces=all164mydestination= mydomain,myhostname264mynetworks=172.16.50.0/24296relaydomains= mydestination
開啓SASL認證
編輯postfix配置文件
[root@localhost ~]# vi /etc/postfix/main.cf
追加如下內容:
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
配置dovecot服務
配置dovecot主配置文件
[root@localhost ~]# vi /etc/dovecot/dovecot.conf
24 protocols = imap pop3 lmtp
48 login_trusted_networks = 172.16.50.0/24
配置dovecot子配置文件
[root@localhost ~]# vi /etc/dovecot/conf.d/10-mail.conf
30 mail_location = mbox:~/mail:INBOX=/var/mail/%
注意:有的系統可能沒有權限;
chmod 0600 /var/mail/*
這裏寫圖片描述
二、配置qq.com域
配置postfix服務
[root@localhost ~]# vi /etc/postfix/main.cf
76 myhostname = mail.qq.com
83 mydomain = qq.com
99myorigin=mydomain113inetinterfaces=all164mydestination= mydomain,myhostname264mynetworks=172.16.50.0/24296relaydomains= mydestination
開啓SASL認證
編輯postfix配置文件
[root@localhost ~]# vi /etc/postfix/main.cf
追加如下內容:
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
配置dovecot服務
配置dovecot主配置文件
[root@localhost ~]# vi /etc/dovecot/dovecot.conf
24 protocols = imap pop3 lmtp
48 login_trusted_networks = 172.16.50.0/24
配置dovecot子配置文件
[root@localhost ~]# vi /etc/dovecot/conf.d/10-mail.conf
30 mail_location = mbox:~/mail:INBOX=/var/mail/%n
這裏寫圖片描述

配置DNS服務器
兩邊手動設置DNS服務器地址(網卡必須指定dns服務器的地址)
[root@localhost ~]# nmcli connection modify ens33 ipv4.dns 172.16.50.37
[root@localhost ~]# nmcli connection up ens33
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/5)
[root@localhost postfix]# vim /etc/named.conf
options {
listen-on port 53 { 172.16.50.37; };
directory “/var/named”;
allow-transfer { 172.16.50.60; };
};
zone “baidu.com” IN {
type master;
file “baidu.com.zone”;
};
zone “50.16.172.in-addr.arpa” IN {
type master;
file “hehe.com.zone”;
};
zone “qq.com” IN {
type master;
file “qq.com.zone”;
};

“/etc/named.conf” 30L, 707C
配置baidu.com域
[root@localhost postfix]# vim /var/named/baidu.com.zone
$TTL 1D
@ IN SOA ns.baidu.com. admin.baidu.com. (
0
1D
1H
1W
3H )
IN NS ns.baidu.com.
IN MX 10 mail.baidu.com.
ns IN A 172.16.50.37
mail IN A 172.16.50.37

配置qq.com域
~[root@localhost postfix]# vim /var/named/qq.com.zone
$TTL 1D
@ IN SOA ns.qq.com. admin.qq.com. (
0
1D
1H
1W
3H )
IN NS ns.qq.com.
IN MX 10 mail.qq.com.
ns IN A 172.16.50.37
mail IN A 172.16.50.60

反向區域配置文件
[root@localhost postfix]# vim /var/named/hehe.com.zone
$TTL 1D
@ IN SOA ns.baidu.com. admin.baidu.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.baidu.com.
37 IN PTR ns.baidu.com.
37 IN PTR mail.baidu.com.
60 IN PTR mail.qq.com.

支持SSL訪問
[root@localhost ~]# cd /etc/postfix/
[root@localhost postfix]# openssl req -new -x509 -nodes -out smtpd.pem -keyout smtpd.pem -days 3650
Generating a 2048 bit RSA private key
…………………..+++
………………………………………..+++
**writing new private key to ‘smtpd.pem’
—–**
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
**If you enter ‘.’, the field will be left blank.
—–**
Country Name (2 letter code) [XX]:86
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xian
Organization Name (eg, company) [Default Company Ltd]:openlab
Organizational Unit Name (eg, section) []:ce
Common Name (eg, your name or your server’s hostname) []:mail.baidu.com
Email Address []:ping

編輯/etc/postfix/main.cf文件
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/smtpd.pem
smtpd_tls_cert_file = /etc/postfix/smtpd.pem
smtpd_tls_CAfile = /etc/postfix/smtpd.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

編輯/etc/postfix/master.cf文件,去掉以下內容的註釋:
#smtps inet n - n - - smtpd
# -o smtpd_tls_wrappermode=yes
# -o smtpd_sasl_auth_enable=yes
重啓服務
[root@localhost postfix]# systemctl restart postfix

虛擬別名區
在兩個域的主配置文件中添加如下內容:
[root@mail ~]# vi /etc/postfix/main.cf
virtual_alias_domains = group.com, work.com
virtual_alias_maps = hash:/etc/postfix/virtual

在虛擬別名域中添加如下內容

baidu域的虛擬別名
[root@localhost postfix]# vi /etc/postfix/virtual
@group.com @baidu.com
@work.com @qq.com
[email protected] maomao,xixi
[email protected] [email protected],[email protected]
[email protected] maomao,xixi,redhat,[email protected],[email protected],[email protected]

qq域的虛擬別名
[root@localhost postfix]# vi /etc/postfix/virtual
@group.com @baidu.com
@work.com @qq.com
[email protected] [email protected],[email protected]
[email protected] doudou,xiaodou
[email protected] [email protected],[email protected],[email protected],dadou,doudou,xiaodou
每次改變該文件,需要執行以下兩個命令
重新生成虛擬別名域數據庫
[root@mail ~]# postmap /etc/postfix/virtual
重新加載虛擬別名域數據文件
[root@mail ~]# systemctl reload postfix

發佈了27 篇原創文章 · 獲贊 16 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章