一、基於電子郵件發送:
電子郵件發送
(1)服務器使用SMTP協議將電子郵件提交至TCP端口25,或由本地客戶端通過/usr/bin/sendmail程序進行提交。如果該MTA是最終目標位置,郵件將傳遞至MDA。否則,將使用MX記錄在DNS中查找下一個MTA,並使用SMTP進行轉發。
(2)MDA:“郵件發送代理”。MDA將郵件發送至收件人的本地郵件存儲位置(默認情況下是/var/spool/mail/user)。Postfix提供自己的MDA,以發送至基於文件的本地默認郵件存儲位置/usr/libexec/postfix/local.
(3)轉發:電子郵件服務器(MTA)將提交的郵件轉發至另一個服務器,以進行發送
(4)排隊:失敗的發送或轉發嘗試排隊等待,並由MTA定義重試。(默認情況下,Postfix每小時執行此操作一次
(5)拒絕:在首次提交期間,電子郵件被電子郵件服務器拒絕
(6)退回:遠程服務器接受電子郵件以進行發送以後,又將該電子郵件退回給始發電子郵件服務器和/或用戶
(7)電子郵件以進行發送以後,又將該電子郵件退回給始發電子郵件服務器和/或用戶
Postfix由postfix RPM包提供,並通過postfix服務腳本控制。它是一個由多個協同操作程序構成的模塊
化程序,它的組件由master進程控制。
Postfix的主配置文件是/etc/postfix/main.cf,可以使用文本編輯器或postconf命令進行編輯。postconf
命令還可用於確定Postfix的所有當前和默認配置設置或逐項確定這些設置。
默認情況下,Postfix僅偵聽來自本地主機的傳入電子郵件。若要重新配置postfix以接收從遠程主機發
送的本地郵件,必須在/etc/postfix/main.cf中設置inet_interfaces = all
對電子郵件進行故障排除時,將在/var/log/maillog中保留所有與郵件相關的操作日誌,其中包括關於
被事件和成功事件的信息。mailq命令(或postqueue -p)顯示已排隊的所有傳出郵件的列表。若要嘗試
再次立即發送所有已排隊的郵件,可以運行postfix flush命令(或postqueue -f);否則,postfix將大
約每小時嘗試重新發送一次,直至郵件被接受或過期。
實驗環境:
服務器端server: willis.com 172.25.254.1
客戶端desktop: desktop.com 172.25.254.2
實驗內容: 1.郵件遠程發送
2.郵件別名
3.郵件羣發
4.空殼郵件服務搭建
5.不同服務器之間郵件傳送(DNS郵件解析)
6.出棧地址僞裝
7.客戶端主機名/地址限制 :(通過IP限制發件)
8.通過發件人地址進行限制 :(通過地址限制發件)
9.通過收件人地址進行過濾 :(限制收件)
1.郵件遠程發送 1.1服務端
1.1-1 systemctl stop firewalld
yum install postfix
1.1-2vim /etc/postfix/main.cf
mydomain = willis.com #收件方看到的方郵件方主機名
myorigin = $mydomain
inet_interfaces = all #開啓接口
#inet_interfaces = localhost #關閉只允許本地訪問
mydestination = $myhostname, $mydomain, localhost #
1.1-3.systemctl restart postfix
netstat antlpe |grep master #查看端口
[root@willis ~]# netstat -antple |grep master
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 0 43632 2497/master
tcp6 0 0 :::25 :::* LISTEN 0 43633 2497/master
1.2.遠程發送端(客戶端)
[root@desktop ~]# yum install telnet -y
[root@desktop ~]# telnet 172.25.254.1 25 Trying 172.25.254.1... Connected to 172.25.254.1. Escape character is '^]'. 220 linux.com ESMTP Postfixehlo hello250-linux.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
2.郵件別名
2.1.vim /etc/aliases
最後添加 admin: root ###給root一個別名爲admin
2.2.postalias /etc/aliases ###重讀別名文件
2.3.systemctl restart postfix
2.4.id admin ###可查看到admin用戶不存在
mail admin ###相當於給root用戶發送郵件
mail ###查看root用戶的郵件
[root@willis ~]# vim /etc/aliases
[root@willis ~]# postalias /etc/aliases
[root@willis ~]# systemctl restart postfix.service
[root@willis ~]# id willis
id: willis: no such user
[root@willis ~]# mail willis
Subject: hello
hello , my name is willis,how are you.
.
EOT
[root@willis ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Sep 9 10:36 18/567 "hello"
& 1
Message 1:
From [email protected] Fri Sep 9 10:36:54 2016
Return-Path: <[email protected]>
X-Original-To: willis
Delivered-To: [email protected]
Date: Fri, 09 Sep 2016 10:36:54 +0800
To: [email protected]
Subject: hello
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R
hello , my name is willis,how are you.
& q
Held 1 message in /var/spool/mail/root
3.郵件羣發
1.vim /etc/postfix/moreuser
root
student
2.vim /etc/aliases
最後添加 more: :include:/etc/postfix/moreuser
3.postalias /etc/aliases
4.systemctl restart postfix
5.mail more ###羣發郵件
mail ###查看root用戶的郵件
mail -u student ###查看student用戶的郵件
[root@willis ~]# >/var/spool/mail/root
[root@willis ~]# useradd student
[root@willis ~]# vim /etc/postfix/moreuser
root
student
[root@willis ~]# vim /etc/aliases
[root@willis ~]# postalias /etc/aliases
[root@willis ~]# systemctl restart postfix.service
[root@willis ~]# mail more
Subject: hello-world
hello world.
.
EOT
[root@willis ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Sep 9 10:42 21/661 "hello-world"
& q
Held 1 message in /var/spool/mail/root
[root@willis ~]# mail -u student
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/mail/student": 1 message 1 new
>N 1 root Fri Sep 9 10:42 21/664 "hello-world"
& q
Held 1 message in /var/mail/student
4.空殼郵件服務搭建
4.1.在desktop(另外一臺僅中轉郵件的服務器,空殼端)上
postconf -e "inet_interfaces = all"
postconf -e "mydomain = willis.com"
postconf -e 'myorigin = $mydomain'
postconf -e "mydestination = "
postconf -e "relayhost = 172.25.254.1"
postconf -e "local_transport = error:local delivery disabled "
systemctl restart postfix.service (附註:火牆要關閉systemctl stop firewalld selinux要置0 setenforce 0 )
[root@desktop ~]# postconf -e "mydomain = willis.com"
[root@desktop ~]# postconf -e 'myorigin = $mydomain'
[root@desktop ~]# postconf -e "mydestination = "
[root@desktop ~]# postconf -e "relayhost = 172.25.254.1"
[root@desktop ~]# postconf -e "local_transport = error:local delivery disabled "
[root@desktop ~]# systemctl restart postfix.service 4.2.在主服務器server上
postconf -e "mynetworks = 127.0.0.0/8 172.25.254.0/24"
#接受來自那個網段的郵件
systemctl restart postfix.service (附註:火牆要關閉systemctl stop firewalld selinux要置0 setenforce 0 )
[root@willis ~]# postconf -e "mynetworks = 127.0.0.0/8 172.25.254.0/24"
You have mail in /var/spool/mail/root
[root@willis ~]# systemctl restart postfix.service
4.3.實驗驗證 在desktop(另外一臺僅中轉郵件的服務器)上(> /var/log/maillog ###清空郵件日誌) mail root ###發送文件[root@desktop ~]# >/var/log/maillog
[root@desktop ~]# mail root
Subject: beautiful world
Threr are many beautiful things.
.
EOT
[root@desktop ~]# mail
No mail for root
在主服務器server上( > /var/spool/mail/root ###清空郵件) mail ###查看文件 [root@willis ~]# >/var/spool/mail/root
[root@willis ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Sep 9 10:54 21/756 "beautiful world"
& q
Held 1 message in /var/spool/mail/root
5.不同服務器之間的郵件傳送(DNS郵件解析記錄) 將前面做實驗的環境恢復到初始狀態。
vim /etc/postfix/main.cf
mydomain = willis.com
myorigin = $mydomain
inet_interfaces = all
#inet_interfaces = localhost
mydestination = $myhostname, $mydomain, localhost
5-1server端配置dns
hostnamectl set-hostname mail.willis.com
安裝DNS:yum install bind -y
1)vim /etc/resolv.conf
search willis.com
nameserver 172.25.254.12)vim /etc/named.rfc1912.zoneszone "willis.com" IN {
type master;
file "willis.com.zone";
allow-update { none; };
};
zone "redhat.com" IN {
type master;
file "redhat.com.zone";
allow-update { none; };
};3)cd /var/named/
cp -p named.localhost willis.com.zone4)vim willis.com.zone$TTL 1D
@ IN SOA dns.willis.com. root. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.willis.com.
dns A 172.25.254.1
willis.com. MX 1 172.25.254.1.5)cp -p willis.com.zone redhat.com.zone
vim redhat.com.zone$TTL 1D
@ IN SOA dns.redhat.com. root. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.redhat.com.
dns A 172.25.254.1
redhat MX 1 172.25.254.2. 6)systemctl start named 5-2 . desktop端配置
hostnamectl set-hostname mail.redhat.com
1)vim /etc/resolv.conf
search redhat.com
nameserver 172.25.254.1
2)測試
mail [email protected]
mail [email protected] [root@mail ~]# mail [email protected]
Subject: hello-willis
hello . you are a good boy.
.
EOT
[root@mail ~]# mail [email protected]
Subject: hello goog boy
Are you ok now?
.
EOT
You have new mail in /var/spool/mail/root
[root@mail ~]# mail
[root@mail ~]# mail [email protected]
Subject: hello good boy.
Are you OK now?
.
EOT
[root@mail ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Sep 9 12:24 18/569 "hello good boy."
& 1
Message 1:
From [email protected] Fri Sep 9 12:24:07 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Fri, 09 Sep 2016 12:24:07 +0800
To: [email protected]
Subject: hello good boy.
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R
Are you OK now?
6.出棧地址僞裝 6.1服務器端(僞裝端)
vim /etc/postfix/generic 增加 [email protected] [email protected] postmap generic ###生成generic.db文件 postconf -d | grep generic postconf -e "smtp_generic_maps = hash:/etc/postfix/generic" systemctl restart postfix.service
測試 mail [email protected]
[root@willis postfix]# mail [email protected]
Subject: hello.
你好呀
.
EOT
6.2 接受端
[root@mail ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Fri Sep 9 12:47 21/733 "hello."
& 1
Message 1:
From [email protected] Fri Sep 9 12:47:10 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Fri, 09 Sep 2016 12:47:11 +0800
To: [email protected]
Subject: hello.
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=utf-8
From: [email protected] (root)
Status: R
你好呀
7.客戶端主機名/地址限制 :(通過IP限制發件)
postconf -e "smtpd_client_restrictions
= check_client_access hash:/etc/postfix/access"
vim /etc/postfox/main.cf
smtpd_client_restrictions = check_client_access hash:/etc/postfix/access,
vim /etc/postfox/access
192.168.0.4 REJECT
192.168.0 REJECT
postmap access
systemctl restart postfix 8.通過發件人地址進行限制 :(通過地址限制發件)
postconf -e "smtpd_sender_restrictions
= check_sender_access hash:/etc/postfix/sender"
vim /etc/postfox/main.cf
smtpd_sender_restrictions =
check_sender_access hash:/etc/postfix/sender
vim /etc/postfix/sender
[email protected] REJECT(OK、RELAY)
postmap sender
systemctl restart postfix9.通過收件人地址進行過濾 :(限制收件)
postconf -e "smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient"
vim /etc/postfox/main.cf smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient
vim /etc/postfix/recipient [email protected] REJECT(OK、RELAY,DISCARDED)
postmap recipient