Linux就该这么学_第15章 使用 Postfix 与 Dovecot 部署邮件系统

本章讲解了如下内容:
➢ 电子邮件系统;
➢ 部署基础的电子邮件系统;
➢ 设置用户别名信箱。
电子邮件系统是我们在日常工作、生活中最常用的一个网络服务,本章将首先介绍电子
邮件系统的起源,然后介绍 SMTP、POP3、IMAP4 等常见的电子邮件协议,以及 MUA、MTA、
MDA 这三种服务角色的作用。本章将完整地演示在 Linux 系统中使用 Postfix 和 Dovecot 服
务程序配置电子邮件系统服务的方法,并重点讲解常用的配置参数,此外还将结合 BIND 服
务程序提供的 DNS 域名解析服务来验证客户端主机与服务器之间的邮件收发功能。本章最后
还介绍了如何在电子邮件系统中设置用户别名,以帮助大家在生产环境中更好地控制、管理
电子邮件账户以及信箱地址。

15.1 电子邮件系统

20 世纪 60 年代,美苏两国正处于冷战时期。美国军方认为应该在科学技术上保持其领
先的地位,这样有助于在未来的战争中取得优势。美国国防部由此发起了一项名为
ARPANET 的科研项目,即大家现在所熟知的阿帕网计划。阿帕网是当今互联网的雏形,它
也是世界上第一个运营的封包交换网络。但是很快在 1971 年阿帕网遇到了严峻的问题,如
图 15-1 所示,参与阿帕网科研项目的科学家分布在美国不同的地区,甚至还会因为时差的
影响而不能及时分享各自的研究成果,因此科学家们迫切需要一种能够借助于网络在计算
机之间传输数据的方法。
尽管本书第 10 章和第 11 章介绍的 Web 服务和 FTP 文件传输服务也能实现数据交换,但
是这些服务的数据传输方式就像“打电话”那样,需要双方同时在线才能完成传输工作。如果
对方的主机宕机或者科研人员因故离开,就有可能错过某些科研成果了。好在当时麻省理工
学院的 Ray Tomlinson 博士也参与到了阿帕网计划的科研项目中,他觉得有必要设计一种类似
于“信件”的传输服务,并为信件准备一个“信箱”,这样即便对方临时离线也能完成数据的
接收,等上线后再进行处理即可。于是,Ray Tomlinson 博士用了近一年的时间完成了电子邮
件(Email)的设计,并在 1971 年秋天使用 SNDMSG 软件向自己的另一台计算机发送出了人
类历史上第一封电子邮件—电子邮件系统在互联网中由此诞生!

既然要在互联网中给他人发送电子邮件,那么对方用户用于接收电子邮件的名称必
须是唯一的,否则电子邮件可能会同时发给多个重名的用户,也或者干脆大家都收不到
邮件了。因此,Ray Tomlinson 博士决定选择使用“姓名@计算机主机名称”的格式来规
范 电子信 箱的 名称。 选择 使用 @符 号作 为间隔符 的原因 其实 也很简 单, 因为 Ray 
Tomlinson 博士觉得人类的名字和计算机主机名称中应该不会有这么一个@符号,所以就
选择了这个符号。

电子邮件系统基于邮件协议来完成电子邮件的传输,常见的邮件协议有下面这些。

 在电子邮件系统中,为用户收发邮件的服务器名为邮件用户代理(Mail User Agent,
MUA)。另外,既然电子邮件系统能够让用户在离线的情况下依然可以完成数据的接收,
肯定得有一个用于保存用户邮件的“信箱”服务器,这个服务器的名字为邮件投递代理
(Mail Delivery Agent,MDA),其工作职责是把来自于邮件传输代理(Mail Transfer Agent,
MTA)的邮件保存到本地的收件箱中。其中,这个 MTA 的工作职责是转发处理不同电
子邮件服务供应商之间的邮件,把来自于 MUA 的邮件转发到合适的 MTA 服务器。例
如,我们从新浪信箱向谷歌信箱发送一封电子邮件,这封电子邮件的传输过程如图 15-
2 所示。
总的来说,一般的网络服务程序在传输信息时就像拨打电话,需要双方同时保持在线,
而在电子邮件系统中,当用户发送邮件后不必等待投递工作完成即可下线。如果对方邮件服
务器(MTA)宕机或对方临时离线,则发件服务器(MTA)就会把要发送的内容自动的暂时
保存到本地,等检测到对方邮件服务器恢复后会立即再次投递,期间一般无需运维人员维护
处理,随后收信人(MUA)就能在自己的信箱中找到这封邮件了。

大家在生产环境中部署企业级的电子邮件系统时,有 4 个注意事项请留意。
➢ 添加反垃圾与反病毒模块:它能够很有效地阻止垃圾邮件或病毒邮件对企业信箱
的干扰。
➢ 对邮件加密:可有效保护邮件内容不被黑客盗取和篡改。
➢ 添加邮件监控审核模块:可有效地监控企业全体员工的邮件中是否有敏感词、是否有
透露企业资料等违规行为。
➢ 保障稳定性:电子邮件系统的稳定性至关重要,运维人员应做到保证电子邮件系统的
稳定运行,并及时做好防范分布式拒绝服务(Distributed Denial of Service,DDoS)攻
击的准备。

15.2 部署基础的电子邮件系统 

一个最基础的电子邮件系统肯定要能提供发件服务和收件服务,为此需要使用基于
SMTP 协议的 Postfix 服务程序提供发件服务功能,并使用基于 POP3 协议的 Dovecot 服务程
序提供收件服务功能。这样一来,用户就可以使用 Outlook Express 或 Foxmail 等客户端服务
程序正常收发邮件了。电子邮件系统的工作流程如图 15-3 所示。

 在 RHEL 5、RHEL 6 以及诸多早期的 Linux 系统中,默认使用的发件服务是由 Sendmail
服务程序提供的,而在 RHEL 7 系统中已经替换为 Postfix 服务程序。相较于 Sendmail 服务
程序,Postfix 服务程序减少了很多不必要的配置步骤,而且在稳定性、并发性方面也有很大
改进。
一般而言,我们的信箱地址类似于“root@linuxprobe.com”这样,也就是按照“用户名@
主机地址(域名)”格式来规范的。如果您给我一串“root@192.168.10.10”的信息,我可能猜
不到这是一个信箱地址,没准会将它当作 SSH 协议的连接信息。因此,要想更好地检验电子
邮件系统的配置效果,需要先部署 bind 服务程序,为电子邮件服务器和客户端提供 DNS 域
名解析服务。

第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

[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。把服务器的 DNS 地址修改成本地 IP 地
址,如图 15-4 所示。

 15.2.1 配置 Postfix 服务程序

Postfix 是一款由 IBM 资助研发的免费开源电子邮件服务程序,能够很好地兼容 Sendmail
服务程序,可以方便 Sendmail 用户迁移到 Postfix 服务上。Postfix 服务程序的邮件收发能力强
于 Sendmail 服务,而且能自动增加、减少进程的数量来保证电子邮件系统的高性能与稳定性。
另外,Postfix 服务程序由许多小模块组成,每个小模块都可以完成特定的功能,因此可在生
产工作环境中根据需求灵活搭配它们。

第1步:

安装 Postfix 服务程序。这一步在 RHEL7 系统中是多余的。刘遄老师之所以还
要写上这一步骤,其目的是让大家在学完本书之后不但能掌握 RHEL 系统,还能立即上手
Fedora、CentOS 等主流 Linux 系统。这样,既然这些系统没有默认安装 Postfix 服务程序,我
们也可以自行搞定。在安装完 Postfix 服务程序后,需要禁用 iptables 防火墙,否则外部用户
无法访问电子邮件系统。

[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),估计会被 679 行左右的内容给吓到。其实不用担心,这里面绝大多数的内
容依然是注释信息。刘遄老师在本书中一直强调正确学习 Linux 系统的方法,并坚信“负责
任的好老师不应该是书本的搬运工,而应该一名优质内容的提炼者”,因此在翻遍了配置参
数的介绍,以及结合多年的运维经验后,最终总结出了 7 个最应该掌握的参数,如表 15-1
所示。

在 Postfix 服务程序的主配置文件中,总计需要修改 5 处。首先是在第 76 行定义一个名
为 myhostname 的变量,用来保存服务器的主机名称。请大家记住这个变量的名称,下边的参
数需要调用它: 

然后在第 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 #
《Linux 就该这么学》 - 必读的 Linux 系统与红帽 RHCE 认证免费自学书籍
316
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步: 在270中增加设置可转发哪些主机的邮件

mynetworks = 192.168.10.0/24

第4步:创建电子邮件系统的登录账户。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

第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 地址,以便能正常解析邮件域
名。设置后的结果如图 15-5 所示。

 第1步:在 Windows 7 系统中运行 Outlook 软件程序。由于各位读者使用的 Windows7 系统
版本不一定相同,因此刘遄老师决定采用 Outlook 2007 版本为对象进行实验。如果您想要与这里
的实验环境尽量保持一致,可在本书配套站点的软件资源库页面(http://www. linuxprobe.com/
tools)下载并安装。在初次运行该软件时会出现一个“Outlook 2007 启动”页面,引导大家完
成该软件的配置过程,如图 15-6 所示。

第2步: 配置电子邮件账户。在图 15-7 所示的“账户设置”页面中单击“是”单选按钮,
然后单击“下一步”按钮。

 

第3步: 选择电子邮件服务的协议类型。在图 15-8 所示的页面中接受默认设置,然后单
击“下一步”按钮。

 第4步:填写电子邮件账户信息,在图 15-9 所示的页面中,“您的姓名”文本框中可
以为自定义的任意名字,“电子邮件地址”文本框中则需要输入服务器系统内的账户名外
加发件域,“密码”文本框中要输入该账户在服务器内的登录密码。在填写完毕之后,单
击“下一步”按钮。

 第5步:进行电子邮件服务登录验证。由于当前没有可用的 SSL 加密服务,因此在 Dovecot
服务程序的主配置文件中写入了一条参数,让客户可以使用明文登录到电子邮件服务。Outlook
软件默认会通过 SSL 加密协议尝试登录电子邮件服务,所以在进行图 15-10 所示的“搜索
boss@linuxprobe.com 服务器设置”大约 30~60 秒后,系统会出现登录失败的报错信息。此时
只需再次单击“下一步”按钮,即可让 Outlook 软件通过非加密的方式验证登录,如图 15-11
所示。

 第6步:向其他信箱发送邮件。在成功登录 Outlook 软件后即可尝试编写并发送新邮件
了。只需在软件界面的空白处单击鼠标右键,在弹出的菜单中选择“新邮件”命令(见图 15-
12),然后在邮件界面中填写收件人的信箱地址以及完整的邮件内容后单击“发送”按钮,如
图 15-13 所示。

 当使用 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

 15.3 设置用户别名信箱

用户别名功能是一项简单实用的邮件账户伪装技术,可以用来设置多个虚拟信箱的账户
以接受发送的邮件,从而保证自身的邮件地址不被泄露,还可以用来接收自己的多个信箱中
的邮件。刚才我们已经顺利地向 root 账户送了邮件,下面再向 bin 账户发送一封邮件,如图
15-14 所示。

在邮件发送后登录到服务器,然后尝试以 bin 账户的身份登录。由于 bin 账户在 Linux 系
统中是系统账户,默认的 Shell 终端是/sbin/nologin,因此在以 bin 账户登录时,系统会提示当
前账户不可用。但是,在电子邮件服务器上使用 mail 命令后,却看到这封原本要发送给 bin
账户的邮件已经被存放到了 root 账户的信箱中。

[root@linuxprobe ~]# su - bin
This account is currently not available.
[root@linuxprobe ~]# mail
Heirloom Mail version 12.5 7/5/10.
Type ? for help.
"/var/mail/root": 4 messages 4 new >
U 1 user@localhost.com Fri Jul 10 09:58 1630/123103 "[abrt] full crash r"
U 2 Anacron Wed Aug 19 17:47 17/619 "Anacron job 'cron.dai"
U 3 boss Sat Aug 15 19:02 118/3604 "Hello~" U
4 boss Wed Aug 19 18:49 116/3231 " Bin "
&> 4
Message 4:
From boss@linuxprobe.com Wed Aug 19 18:49:05 2017
Return-Path: <boss@linuxprobe.com>
X-Original-To: bin@linuxprobe.com
Delivered-To: bin@linuxprobe.com
From: "boss" <boss@linuxprobe.com>
To: <bin@linuxprobe.com>
Subject: 你好,用户 Bin。
Date: Wed, 19 Aug 2017 18:49:05 +0800
Content-Type: multipart/alternative; boundary="----=_NextPart_000_0006_01D0DAAF.
B9104E90"
X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AdDabKrQzUHVBTgRQMaCtUs
VtqfL1Q== Content-Language: zh-cn Status: R Content-Type: text/plain; charset="gb2312"
………………省略部分输出信息………………
这是一封发给用户 Bin 的文件。
&> quit
Held 4 messages in /var/mail/root

太奇怪了!明明发送给 bin 账户的邮件怎么会被 root 账户收到了呢?其实,这就是使用
用户别名技术来实现的。在 aliases 邮件别名服务的配置文件中可以看到,里面定义了大量的
用户别名,这些用户别名大多数是 Linux 系统本地的系统账户,而在冒号(:)间隔符后面的
root 账户则是用来接收这些账户邮件的人。用户别名可以是 Linux 系统内的本地用户,也可以
是完全虚构的用户名字。

 

[root@linuxprobe ~]# cat /etc/aliases
#
# Aliases in this file will NOT be expanded in the header from
# Mail, but WILL be visible over networks or from /bin/mail.
#
# >>>>>>>>>> The program "newaliases" must be run after
# >> NOTE >> this file is updated for any changes to
# >>>>>>>>>> show through to sendmail.
#
# Basic system aliases -- these MUST be present.
mailer-daemon: postmaster
postmaster: root
# General redirections for pseudo accounts.
bin: root
daemon: root
adm: root
lp: root
sync: root
shutdown: root
halt: root
mail: root
www: webmaster
webmaster: root
noc: root
security: root
hostmaster: root
info: postmaster
marketing: postmaster
sales: postmaster
support: postmaster
# trap decode to catch security attacks
decode: root
# Person who should get root's mail
#root: marc

现在大家能猜出是怎么一回事了吧。原来 aliases 邮件别名服务的配置文件是专门用来定
义用户别名与邮件接收人的映射。除了使用本地系统中系统账户的名称外,我们还可以自行
定义一些别名来接收邮件。例如,创建一个名为 xxoo 的账户,而真正接收该账户邮件的应该
是 root 账户。

[root@linuxprobe ~]# cat /etc/aliases
#
# Aliases in this file will NOT be expanded in the header from
# Mail, but WILL be visible over networks or from /bin/mail.
#
# >>>>>>>>>> The program "newaliases" must be run after
# >> NOTE >> this file is updated for any changes to
# >>>>>>>>>> show through to sendmail.
#
# Basic system aliases -- these MUST be present.
mailer-daemon: postmaster
postmaster: root
# General redirections for pseudo accounts.
xxoo: root
bin: root
daemon: root
adm: root
lp: root
………………省略部分输出信息………………

保存并退出 aliases 邮件别名服务的配置文件后,需要再执行一下 newaliases 命令,其目
的是让新的用户别名配置文件立即生效。然后再次尝试发送邮件,如图 15-15 所示:
这时,使用 root 账户在服务器上执行 mail 命令后,就能看到这封原本要发送给 xxoo 账
户的邮件了。最后,刘遄老师再啰嗦一句,用户别名技术不仅应用广泛,而且配置也很简单。
所以更要提醒大家的是,今后千万不要看到有些网站上提供了很多客服信箱就轻易相信别人,
没准发往这些客服信箱的邮件会被同一个人收到。

[root@linuxprobe ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
《Linux 就该这么学》 - 必读的 Linux 系统与红帽 RHCE 认证免费自学书籍
328
"/var/mail/root": 5 messages 1 new 4 unread
U 1 user@localhost.com Fri Jul 10 09:58 1631/123113 "[abrt] full crash report"
U 2 Anacron Wed Aug 19 17:47 18/629 "Anacron job 'cron.daily' on mail.linuxprobe.com"
U 3 boss Wed Aug 19 18:44 114/2975 "hello"
4 boss Wed Aug 19 18:49 117/3242 "你好,用户 Bin。"
>N 5 boss Wed Aug 19 19:18 115/3254 "这是一封发送给 xxoo 用户的邮件。"

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值