Linux就該這麼學 20181011(第十五章郵件)

參考連接:https://www.linuxprobe.com、html

https://www.linuxprobe.com/chapter-15.htmllinux

電子郵箱系統

foxmail

MUA 發送
MTA 轉發
MDA 保存

Postfix
vim /etc/postfix/main.cf  發送文件  
:48
systemctl restart postfix
 
yum -y install dovecot 收取郵件

vim /etc/dovecot/dovecot.conf

protocols = imap pop3 lmtp
disable_plaintext_auth = no
login_trusted_networks = 192.168.161.0/24



vim /etc/dovecot/conf.d/10-mail.conf  #文件保存路徑
mail_location = mbox:~/mail:INBOX=/var/mail/%u    #.imap  隱藏文件

子用戶
mkdir -p mail/.imap/INBOX

systemctl restart dovexot

pam模塊 本地用戶

mail命令 收取郵件


/etc/aliases 郵件別名  隱藏 轉發
newaliases
systemctl restart postfix


第1步:配置服務器主機名稱,須要保證服務器主機名稱與發信域名保持一致:

[root@linuxprobe ~]# vim /etc/hostname
mail.linuxprobe.com
[root@linuxprobe ~]# hostname
mail.linuxprobe.com

第2步:清空iptables防火牆默認策略,並保存策略狀態,避免因防火牆中默認存在的策略阻

止了客戶端DNS解析域名及收發郵件:

[root@localhost ~]# iptables -F
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

第3步:爲電子郵件系統提供域名解析。因爲第13章已經講解了bind-chroot服務程序的配置方

法,所以這裏只提供主配置文件、區域配置文件和域名數據文件的配置內容,其他配置步驟請

你們自行完成。

 [root@linuxprobe ~]# cat /etc/named.conf
 1 //
 2 // named.conf
 3 //
 4 // Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
 5 // server as a caching only nameserver (as a localhost DNS resolver only).
 6 //
 7 // See /usr/share/doc/bind*/sample/ for example named configuration files.
 8 //
 9 
 10 options {
 11 listen-on port 53 { any; };
 12 listen-on-v6 port 53 { ::1; };
 13 directory "/var/named";
 14 dump-file "/var/named/data/cache_dump.db";
 15 statistics-file "/var/named/data/named_stats.txt";
 16 memstatistics-file "/var/named/data/named_mem_stats.txt";
 17 allow-query { any; };
 18 
 ………………省略部分輸出信息………………
[root@linuxprobe ~]# cat /etc/named.rfc1912.zones
zone "linuxprobe.com" IN {
type master;
file "linuxprobe.com.zone";
allow-update {none;};
};
[root@linuxprobe ~]# cat /var/named/linuxprobe.com.zone
$TTL 1D                
@    IN SOA    linuxprobe.com.    root.linuxprobe.com.    (
0;serial
1D;refresh
1H;retry
1W;expire
3H);minimum
NS    ns.linuxprobe.com.    
ns    IN A    192.168.10.10    
@    IN MX 10    mail.linuxprobe.com.    
mail    IN A    192.168.10.10

[root@linuxprobe ~]# systemctl restart named
[root@linuxprobe ~]# systemctl enable named
ln -s '/usr/lib/systemd/system/named.service' 
'/etc/systemd/system/multi-user.target.wants/named.service'
修改好配置文件後記得重啓bind服務程序,這樣電子郵件系統所對應的服務器主機名即爲

mail.linuxprobe.com,而郵件域爲@linuxprobe.com。

15.2.1 配置Postfix服務程序
Postfix是一款由IBM資助研發的免費開源電子郵件服務程序,可以很好地兼容Sendmail服務程

序,能夠方便Sendmail用戶遷移到Postfix服務上。Postfix服務程序的郵件收發能力強於

Sendmail服務,並且能自動增長、減小進程的數量來保證電子郵件系統的高性能與穩定性。另

外,Postfix服務程序由許多小模塊組成,每一個小模塊均可以完成特定的功能,所以可在生產

工做環境中根據需求靈活搭配它們。

[root@linuxprobe ~]# yum install postfix
Loaded plugins: langpacks, product-id, subscription-manager
rhel7 | 4.1 kB 00:00
(1/2): rhel7/group_gz | 134 kB 00:00
(2/2): rhel7/primary_db | 3.4 MB 00:00
Package 2:postfix-2.10.1-6.el7.x86_64 already installed and latest version
Nothing to do
[root@linuxprobe ~]# systemctl disable iptables

第2步:配置Postfix服務程序Postfix服務程序主配置文件(/etc/ postfix/main.cf)
表15-1                                Postfix服務程序主配置文件中的重要參數

參數    做用
myhostname    郵局系統的主機名
mydomain    郵局系統的域名
myorigin    從本機發出郵件的域名名稱
inet_interfaces    監聽的網卡接口
mydestination    可接收郵件的主機名或域名
mynetworks    設置可轉發哪些主機的郵件
relay_domains    設置可轉發哪些網域的郵件

在Postfix服務程序的主配置文件中,總計須要修改5處。首先是在第76行定義一個名爲

myhostname的變量,用來保存服務器的主機名稱。請你們記住這個變量的名稱,下邊的參數需

要調用它:

[root@linuxprobe ~]# 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.linuxprobe.com
………………省略部分輸出信息………………

而後在第83行定義一個名爲mydomain的變量,用來保存郵件域的名稱。你們也要記住這個變量

名稱,下面將調用它:

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 = linuxprobe.com

在第99行調用前面的mydomain變量,用來定義發出郵件的域。調用變量的好處是避免重複寫入

信息,以及便於往後統一修改:

85 # SENDING MAIL
86 # 
87 # The myorigin parameter specifies the domain that locally-posted
88 # mail appears to come from. The default is to append $myhostname,
89 # which is fine for small sites. If you run a domain with multiple
90 # machines, you should (1) change this to $mydomain and (2) set up
91 # a domain-wide alias database that aliases each user to
92 # user@that.users.mailhost.
93 #
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

第4處修改是在第116行定義網卡監聽地址。能夠指定要使用服務器的哪些IP地址對外提供電子

郵件服務;也能夠乾脆寫成all,表明全部IP地址都能提供電子郵件服務:

103 # The inet_interfaces parameter specifies the network interface
104 # addresses that this mail system receives mail on. By default,
105 # the software claims all active interfaces on the machine. The
106 # parameter also controls delivery of mail to user@[ip.address].
107 #
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
114 #inet_interfaces = $myhostname
115 #inet_interfaces = $myhostname, localhost
116 inet_interfaces = all

最後一處修改是在第164行定義可接收郵件的主機名或域名列表。這裏能夠直接調用前面定義

好的myhostname和mydomain變量(若是不想調用變量,也能夠直接調用變量中的值):

133 # The mydestination parameter specifies the list of domains that this
134 # machine considers itself the final destination for.
135 #
136 # These domains are routed to the delivery agent specified with the
137 # local_transport parameter setting. By default, that is the UNIX
138 # compatible delivery agent that lookups all recipients in /etc/passwd
139 # and /etc/aliases or their equivalent.
140 #
141 # The default is $myhostname + localhost.$mydomain. On a mail domain
142 # gateway, you should also include $mydomain.
143 #
144 # Do not specify the names of virtual domains - those domains are
145 # specified elsewhere (see VIRTUAL_README).
146 #
147 # Do not specify the names of domains that this machine is backup MX
148 # host for. Specify those names via the relay_domains settings for
149 # the SMTP server, or use permit_mx_backup if you are lazy (see
150 # STANDARD_CONFIGURATION_README).
151 #
152 # The local machine is always the final destination for mail addressed
153 # to user@[the.net.work.address] of an interface that the mail system
154 # receives mail on (see the inet_interfaces parameter).
155 #
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 , $mydomain
165 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
166 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
第3步:建立電子郵件系統的登陸帳戶。Postfix與vsftpd服務程序同樣,均可以調用本地系統

的帳戶和密碼,所以在本地系統建立常規帳戶便可。最後重啓配置穩當的postfix服務程序,

並將其添加到開機啓動項中。大功告成!

[root@linuxprobe ~]# useradd boss
[root@linuxprobe ~]# echo "linuxprobe" | passwd --stdin boss
Changing password for user boss. passwd: all authentication tokens updated 

successfully.
[root@linuxprobe ~]# systemctl restart postfix
[root@linuxprobe ~]# systemctl enable postfix
ln -s '/usr/lib/systemd/system/postfix.service' '/etc/systemd/system/multi-

user.target.wants/postfix.service'

15.2.2 配置Dovecot服務程序
Dovecot是一款可以爲Linux系統提供IMAP和POP3電子郵件服務的開源服務程序,安全性極高,

配置簡單,執行速度快,並且佔用的服務器硬件資源也較少,所以是一款值得推薦的收件服務

程序。

第1步:安裝Dovecot服務程序軟件包。你們可自行配置Yum軟件倉庫、掛載光盤鏡像到指定目

錄,而後輸入要安裝的dovecot軟件包名稱便可:
[root@linuxprobe ~]# yum install dovecot
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use 

subscription-manager to register.
rhel | 4.1 kB 00:00 
Resolving Dependencies
--> Running transaction check
---> Package dovecot.x86_64 1:2.2.10-4.el7 will be installed
--> Processing Dependency: libclucene-core.so.1()(64bit) for package: 1:dovecot-

2.2.10-4.el7.x86_64
--> Processing Dependency: libclucene-shared.so.1()(64bit) for package: 

1:dovecot-2.2.10-4.el7.x86_64
--> Running transaction check
---> Package clucene-core.x86_64 0:2.3.3.4-11.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
 Package Arch Version Repository Size
================================================================================
Installing:
 dovecot x86_64 1:2.2.10-4.el7 rhel 3.2 M
Installing for dependencies:
 clucene-core x86_64 2.3.3.4-11.el7 rhel 528 k
Transaction Summary
================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 3.7 M
Installed size: 12 M
Is this ok [y/d/N]: y
Downloading packages:
--------------------------------------------------------------------------------
Total 44 MB/s | 3.7 MB 00:00 
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing : clucene-core-2.3.3.4-11.el7.x86_64 1/2 
 Installing : 1:dovecot-2.2.10-4.el7.x86_64 2/2 
 Verifying : 1:dovecot-2.2.10-4.el7.x86_64 1/2 
 Verifying : clucene-core-2.3.3.4-11.el7.x86_64 2/2 
Installed:
 dovecot.x86_64 1:2.2.10-4.el7 
Dependency Installed:
 clucene-core.x86_64 0:2.3.3.4-11.el7 
Complete!

第2步:配置部署Dovecot服務程序。在Dovecot服務程序的主配置文件中進行以下修改。首先

是第24行,把Dovecot服務程序支持的電子郵件協議修改成imap、pop3和lmtp。而後在這一行

下面添加一行參數,容許用戶使用明文進行密碼驗證。之因此這樣操做,是由於Dovecot服務

程序爲了保證電子郵件系統的安全而默認強制用戶使用加密方式進行登陸,而因爲當前尚未

加密系統,所以須要添加該參數來容許用戶的明文登陸。

[root@linuxprobe ~]# vim /etc/dovecot/dovecot.conf
………………省略部分輸出信息………………
23 # Protocols we want to be serving.
24 protocols = imap pop3 lmtp
25 disable_plaintext_auth = no
………………省略部分輸出信息………………
在主配置文件中的第48行,設置容許登陸的網段地址,也就是說咱們能夠在這裏限制只有來自

於某個網段的用戶才能使用電子郵件系統。若是想容許全部人都能使用,則不用修改本參數:

44 # Space separated list of trusted network ranges. Connections from these
45 # IPs are allowed to override their IP addresses and ports (for logging and
46 # for authentication checks). disable_plaintext_auth is also ignored for
47 # these networks. Typically you'd specify your IMAP proxy servers here.
48 login_trusted_networks = 192.168.10.0/24
第3步:配置郵件格式與存儲路徑。在Dovecot服務程序單獨的子配置文件中,定義一個路徑,

用於指定要將收到的郵件存放到服務器本地的哪一個位置。這個路徑默認已經定義好了,咱們只

須要將該配置文件中第24行前面的井號(#)刪除便可。

[root@linuxprobe ~]# vim /etc/dovecot/conf.d/10-mail.conf
1 ##
2 ## Mailbox locations and namespaces
3 ##
4 # Location for users' mailboxes. The default is empty, which means that Dovecot
5 # tries to find the mailboxes automatically. This won't work if the user
6 # doesn't yet have any mail, so you should explicitly tell Dovecot the full
7 # location.
8 #
9 # If you're using mbox, giving a path to the INBOX file (eg. /var/mail/%u)
10 # isn't enough. You'll also need to tell Dovecot where the other mailboxes are
11 # kept. This is called the "root mail directory", and it must be the first
12 # path given in the mail_location setting.
13 #
14 # There are a few special variables you can use, eg.:
15 #
16 # %u - username
17 # %n - user part in user@domain, same as %u if there's no domain
18 # %d - domain part in user@domain, empty if there's no domain
19 # %h - home directory
20 #
21 # See doc/wiki/Variables.txt for full list. Some examples:
22 #
23 # mail_location = maildir:~/Maildir
24 mail_location = mbox:~/mail:INBOX=/var/mail/%u
25 # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
………………省略部分輸出信息………………
而後切換到配置Postfix服務程序時建立的boss帳戶,並在家目錄中創建用於保存郵件的目錄

。記得要重啓Dovecot服務並將其添加到開機啓動項中。至此,對Dovecot服務程序的配置部署

步驟所有結束。

[root@linuxprobe ~]# su - boss
Last login: Sat Aug 15 16:15:58 CST 2017 on pts/1
[boss@mail ~]$ mkdir -p mail/.imap/INBOX
[boss@mail ~]$ exit
[root@linuxprobe ~]# systemctl restart dovecot 
[root@linuxprobe ~]# systemctl enable dovecot 
ln -s '/usr/lib/systemd/system/dovecot.service' '/etc/systemd/system/multi-

user.target.wants/dovecot.service'
15.2.3 客戶使用電子郵件系統
如何得知電子郵件系統已經可以正常收發郵件了呢?可使用Windows操做系統中自帶的

Outlook軟件來進行測試(也可使用其餘電子郵件客戶端來測試,好比Foxmail)。請按照表

15-2來設置電子郵件系統及DNS服務器和客戶端主機的IP地址,以便能正常解析郵件域名。

電子郵箱地址 boss@linuxprobe.com

當使用Outlook軟件成功發送郵件後,即可以在電子郵件服務器上使用mail命令查看到新郵件

提醒了。若是想查看郵件的完整內容,只需輸入收件人姓名前面的編號便可。

[root@linuxprobe ~]# mail
Heirloom Mail version 12.5 7/5/10.Type ? for help.
"/var/mail/root": 3 messages 3 unread >
U 1 user@localhost.com Fri Jul 10 09:58 1631/123113 "[abrt] full crash r" 
U 2 Anacron Sat Aug 15 13:33 18/624 "Anacron job 'cron.dai" 
U 3 boss Sat Aug 15 19:02 118/3604 "Hello~" 
&> 3
Message 3:
From boss@linuxprobe.com Sat Aug 15 19:02:06 2017 
Return-Path: 
X-Original-To: root@linuxprobe.com 
Delivered-To: root@linuxprobe.com 
From: "boss" 
To: 
Subject: Hello~
Date: Sat, 15 Aug 2017 19:02:06 +0800
Content-Type: text/plain; charset="gb2312" 
………………省略部分輸出信息………………
當您收到這封郵件時,證實個人郵局系統實驗已經成功!
> quit 
Held 3 messages in /var/mail/root
相關文章
相關標籤/搜索