Linux就该这么学 20181011(第十五章邮件)

参考链接:https://www.linuxprobe.com、

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

电子邮箱系统

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 # [email protected].
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地址,以便能正常解析邮件域名。

电子邮箱地址 [email protected]

当使用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 [email protected] 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 [email protected] Sat Aug 15 19:02:06 2017 
Return-Path: 
X-Original-To: [email protected] 
Delivered-To: [email protected] 
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

猜你喜欢

转载自www.cnblogs.com/Liang-jc/p/9775856.html