RHCE培训笔记——Postfix

简介:

在RHEL 6中,Postfix默认已安装并随机启动。有两个文件很重要,如下:

主配文件 /etc/postfix/main.cf

进程文件 /etc/postfix/master.cf


在实际操作中,修改主配文件,主要涉及到的几个参数是(前面的数字是初始时的行号):


75 #myhostname = host.domain.tld

注:myhostname 参数指定运行postfix邮件系统的主机的主机名。缺省地,该值被设定为本地机器名。可以指定该值,需要注意的是,要指定完整的主机名。如:myhostname = zero.domain.com


83 #mydomain = domain.tld

注:mydomain参数指定域名,缺省地,postfix将myhostname的第一部分删除而作为mydomain的值。


99 #myorigin = $mydomain

注:myorigin参数指明发件人所在的域名。如果用户的邮件地址为user@domain.com,则该参数指定@后面的域名。缺省地,postfix使用本地主机名作为myorigin,但是建议最好使用域名,因为这样更具有可读性。比如:安装postfix的主机为zero.domain.com则我们可以这样指定myorigin: myorigin = domain.com。当然也可以引用其他参数,如:myorigin = $mydomain。


116 inet_interfaces = localhost

注:inet_interfaces 参数指定postfix系统监听的网络接口。缺省地,postfix监听所有的网络接口。如果postfix运行在一个虚拟的ip地址上,则必须指定其监听的地址。如:inet_interfaces = all, inet_interface = 192.168.1.1


164 mydestination = $myhostname, localhost.$mydomain, localhost

注:mydestination参数指定postfix接收邮件时收件人的域名,换句话说,也就是postfix系统要接收什么样的邮件。比如:用户的邮件地址为user@domain.com,也就是域为domain.com,则你就需要接收所有收件人为user_name@domain.com的邮件。与myorigin一样,缺省地,postfix使用本地主机名作为mydestination。如:mydestination = $mydomain,mydestination = domain.com


264 #mynetworks = 168.100.189.0/28, 127.0.0.0/8

mynetworks 参数指定所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问。可以用标准的A、B、C类网络地址,也可以用CIDR(无类域间路由)地址来表示,如:192.168.1.0/24, 192.168.1.0/26


546 #header_checks = regexp:/etc/postfix/header_checks

注:在postfix中,通过header_checks参数限制接收邮件的信头的格式,如果符合指定的格式,则拒绝接收该邮件。可以指定一个或多个查询列表,如果新邮件的信头符合列表中的某一项则拒绝该接收邮件。



一、准备工作


本实验平台为Centos 6.2。


先确定主机名是带域名的形式。如果不带,请使用hostname命令更名,并修改/etc/sysconfig/network文件中HOSTNAME的值。


查看主机名

[root@zero ~]# hostname

zero.sq.com



二、去掉邮件地址中的主机头


1.在不修改任何配置的情况下,管理员root向普通用户sqing发邮件test01。


[root@zero ~]# mail sqing

Subject: test01

00000000000001111111111111111

.

EOT


在另一个终端使用sqing登录查看

[sqing@zero ~]$ mail

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/sqing": 1 message 1 new

>N  1 root                  Wed May 22 02:41  18/560   "test01"

& 1

Message  1:

From root@zero.sq.com  Wed May 22 02:41:49 2013

Return-Path: <root@zero.sq.com>

X-Original-To: sqing

Delivered-To: sqing@zero.sq.com

Date: Wed, 22 May 2013 02:41:49 +0800

To: sqing@zero.sq.com

Subject: test01

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: root@zero.sq.com (root)

Status: R


00000000000001111111111111111


& q


可以看到,此时邮件的发件人和收件人的邮件地址,域名部分显示的是主机头加域名,如root@zero.sq.com,但在实际中,一船是不带主机名的,只使用主机所在域的域名,如root@sq.com


2.为了达到这个效果,需要进行以下修改,

将mydomain和myorigin开启,并将mydomain的值改为sq.com。

同时,还要修改,mydestination的值,否则,邮件发出后,对方收不到,而自己这边又没有退信邮件。

[root@zero ~]# cat /etc/postfix/main.cf

1
2
3
4
5
……其他内容略……
83 mydomain = sq.com   #改为主机所在的域,不加主机头
99 myorigin = $mydomain   #改为本地域名
116 inet_interfaces = all   #改为监听all,本实验环境是在locahost上操作,可以不改
164 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain   #增加本地域名


重启服务,restart或reload

[root@zero ~]# service postfix restart

Shutting down postfix:                                     [  OK  ]

Starting postfix:                                          [  OK  ]


再测试

管理员root向普通用户sqing发邮件test02。

[root@zero ~]# mail sqing

Subject: test02

000000000000000022222222222222222

.

EOT


在root用户登录的终端查看sqing的邮件

[root@zero ~]# mail –u sqing

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/sqing": 2 messages 1 new

   1 root                  Wed May 22 02:41  19/571   "test01"

>N  2 root                  Wed May 22 03:03  18/539   "test02"

& 2

Message  2:

From root@sq.com  Wed May 22 03:03:20 2013

Return-Path: <root@sq.com>

X-Original-To: sqing

Delivered-To: sqing@sq.com

Date: Wed, 22 May 2013 03:03:20 +0800

To: sqing@sq.com

Subject: test02

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: root@sq.com (root)

Status: R


000000000000000022222222222222222


& q



三、别名


修改/etc/aliases文件,在末行添加以下内容:

1
sq:     sqing

(注,格式为 用户别名:用户实名)


保存修改后,执行newaliases命令,newaliases 的功能传是将 /etc/aliases 转换成一个 postfix(还有sendmai)l 所能了解的数据库。也可以不执行newaliases命令,但需重启postfix服务。

[root@zero ~]# newaliases


root发测试邮件,

[root@zero ~]# mail sq

Subject: test03


0000000000000000000333333333333333333333

.

EOT


查看sqing邮件,

[root@zero ~]# mail –u sqing

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/sqing": 3 messages 1 new

   1 root                  Wed May 22 02:41  19/571   "test01"

   2 root                  Wed May 22 03:03  19/550   "test02"

>N  3 root                  Wed May 22 03:15  18/537   "test03"

& 3

Message  3:

From root@sq.com  Wed May 22 03:15:56 2013

Return-Path: <root@sq.com>

X-Original-To: sq

Delivered-To: sq@sq.com

Date: Wed, 22 May 2013 03:15:56 +0800

To: sq@sq.com

Subject: test03

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: root@sq.com (root)

Status: R


0000000000000000000333333333333333333333


& q


注,这个别名的功能,还可以当用户组用,如上述的改为:

sq:     sqing,oping,alex

则向sq发邮件时,用户sqing和oping都能收到邮件。



四、过虑


header_checks参数可以扫描邮件头,header_checks查找表规则表达式为: parttern action 注:parttern是对字段头进行匹配的规则表达式,action是当parttern符合时候采取的动作,包括REJECT(将该信件退回给发信者)、WARN(将信件收下来,但是将该信件的基本资料记录在登录档内)、DISCARD(将该信件丢弃,并不给予原发信者回应)。


1.在/etc/postfix/main.cf中启用header_checks文件头(将注释符#去掉)

1
546 header_checks = regexp: /etc/postfix/header_checks


2.同时,在/etc/postfix/header_checks文件开始处添加以下一行内容:

1
/sex/  REJECT Sexy information is disabled.

注:正则表达式 动作 返回信息(可不写)。表示邮件标题里带“sexy”这个词的邮件都被拒绝发送,系统管理员向发件人返回的信息提示是“Sexy information is disabled.”


3.测试

Root向sqing发送一封邮件标题为sexy girl的邮件,

[root@zero ~]# mail sqing

Subject: sexy girl

ssssssssssssssssssssssssss    

.

EOT


Root收到退信邮件,

[root@zero ~]# mail

Heirloom Mail version 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/root": 1 message 1 new

>N  1 Mail Delivery System  Wed May 22 05:36  70/2166  "Undelivered Mail Retu"

& 1

Message  1:

From MAILER-DAEMON  Wed May 22 05:36:50 2013

Return-Path: <>

X-Original-To: root@sq.com

Delivered-To: root@sq.com

Date: Wed, 22 May 2013 05:36:50 +0800 (CST)

From: MAILER-DAEMON@sq.com (Mail Delivery System)

Subject: Undelivered Mail Returned to Sender

To: root@sq.com

Auto-Submitted: auto-replied

Content-Type: multipart/report; report-type=delivery-status;

       boundary="4947A60D7B.1369172210/zero.sq.com"

Status: R


Part 1:

Content-Description: Notification

Content-Type: text/plain; charset=us-ascii


This is the mail system at host zero.sq.com.


I'm sorry to have to inform you that your message could not

be delivered to one or more recipients. It's attached below.


For further assistance, please send mail to postmaster.


If you do so, please include this problem report. You can

delete your own text from the attached returned message.


                  The mail system


<sqing@sq.com> (expanded from <sqing>): Sexy information is disabled.


Part 2:

Content-Description: Delivery report

Content-Type: message/delivery-status



Part 3:

Content-Description: Undelivered Message Headers

Content-Type: text/rfc822-headers


Return-Path: <root@sq.com>

Received: by zero.sq.com (Postfix, from userid 0)

       id 4947A60D7B; Wed, 22 May 2013 05:36:50 +0800 (CST)

Date: Wed, 22 May 2013 05:36:50 +0800

To: sqing@sq.com

Subject: sexy girl

User-Agent: Heirloom mailx 12.4 7/29/08

MIME-Version: 1.0

Content-Type: text/plain; charset=us-ascii

Content-Transfer-Encoding: 7bit

Message-Id: <20130521213650.4947A60D7B@zero.sq.com>

From: root@sq.com (root)

&


从邮箱系统管理员返回的结果显示,该邮件由于存在敏感字“sexy”而被拒绝了。

邮件标题为“asexya”的邮件同样也被拦截。


如果是要针对邮件内容过滤,在主配文件中,增加一个body_checks文件头(这里加在header_checks文件头下面)

1
2
546 header_checks = regexp: /etc/postfix/header_checks
547 body_checks = regexp: /etc/postfix/body_checks


同时,在etc/postfix中以header_checks为模块,创建一个body_checks文件。

[root@zero ~]# cp -p /etc/postfix/header_checks /etc/postfix/body_checks


编辑body_checks内容时,其过虑关键字的格式与header_checks一样。



本文转自Sunshyfangtian 51CTO博客,原文链接:http://blog.51cto.com/sunshyfangtian/1217101,如需转载请自行联系原作者

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
网络协议 网络安全
|
网络协议 测试技术 Linux
RHCE 系列(九):如何使用无客户端配置 Postfix 邮件服务器(SMTP)
RHCE 系列(九):如何使用无客户端配置 Postfix 邮件服务器(SMTP) 尽管现在有很多在线联系方式,电子邮件仍然是一个人传递信息给远在世界尽头或办公室里坐在我们旁边的另一个人的有效方式。
2735 0