三、postfix标准配置示例

这篇文档的目的
这篇文档展现了postfix的典型配置参数。在你按照《BASIC_CONFIGURATION_README》设置完之后,应该回顾《postfix标准配置示例》这篇文档。需要注意的是,如果你的postfix还不能进行本地邮件发送和本地邮件传递,那么先安装好postfix,在阅读下面的内容。
这篇文档的第一部分展现标准的配置,每一个内容都会解决一个特定的问题
 •internet独立主机的postfix
 •无客户端的postfix(即只发送邮件,不接收邮件)
 •本地网络的postfix
 •postfix的邮件网关/防火墙
这篇文档的第二部分对特定的网络环境进行附加的配置
 •部分用户可以给internet发邮件
 •在防火墙后面运行postfix
 •为远程站点配置主/备份mx
 •拨号主机的postfix配置
 •没有公网地址的主机上配置postfix
internet独立主机的postfix
你能用"postconf -n"查看你的main.cf忽略了那些参数。
/etc/postfix/main.cf:
    #可选参数:邮件以user@domainname代替user@hostname
       #myorigin = $mydomain
     #可选参数:指定NAT/proxy的ip地址
   #proxy_interfaces = 1.2.3.4
   #二选一的参数1:不中继其他主机的邮件
       mynetworks_style = host
       relay_domains =
    #二选一的参数1:仅中继与postfix同一网段的客户端邮件
    # mynetworks = 192.168.1.0/28
    # relay_domains =
无客户端的postfix
无客户端的意思是只发送邮件不接收邮件。它不接收来自网络的任何邮件,也不把邮件投递到本地。空客户端的postfix典型应用环境是提供POP,IMAP或NFS。
在这个示例中,我们假设internet域名为:example.com,这个机器的名称为:nullclient.example.com。注:这个示例省略了默认的参数设置。
 1 /etc/postfix/main.cf:
 2     myorigin = $mydomain
 3     relayhost = $mydomain
 4     inet_interfaces = loopback-only
 5     local_transport = error:local delivery is disabled
 6
 7 /etc/postfix/master.cf:
 8     Comment out the local delivery agent entry
解释:
•第2行的意思:邮件以[email protected]发送,而不是以[email protected]发送。
•第3行的意思:转发所有example.com的邮件到这个邮件服务器。This prevents mail from getting stuck on the null client if it is turned off while some remote destination is unreachable.
•第4行的意思:不接收来自网络的邮件
•第5-8行的意思:关闭本地邮件投递。所有的邮件都到第三行指定的邮件服务器。
本地网络的postfix
这部分描述了一个主postfix服务器和其他邮件系统并存的局域网环境。我们仍然假设internet域名为:example.com。所有的系统都被配置为以[email protected]发送邮件;所有的系统接收[email protected]的邮件。这个主服务器也接收[email protected]的邮件。我们把这台机器命名为:mailhost.example.com。
 
[email protected]发送邮件的缺点是:root和其他系统帐户的邮件也发送到网关邮件服务器。这个缺点的解决办法参见“Delivering some but not all accounts locally”
这个示例仍展示除默认参数之外的所需参数
首先,我们展现没有mailhost的配置,因为这个相对简单点。这个邮件服务器以[email protected]发送邮件,接收[email protected]的邮件。
 1 /etc/postfix/main.cf:
 2     myorigin = $mydomain
 3     mynetworks = 127.0.0.0/8 10.0.0.0/24
 4     relay_domains =
 5     # Optional: forward all non-local mail to mailhost
 6     #relayhost = $mydomain
解释:
 •第2行:以[email protected]发送邮件
 •第3行:指定信任的网络
 •第4行: 这个邮件服务器不中继不受信任网络的邮件
 •第6行:如果邮件服务器没有公网的ip地址,参见下面的“防火墙后面的postfix”
接下来,我们展现含mailhost的配置。邮件服务器以[email protected]发送邮件,接收[email protected][email protected]的邮件
  1 DNS:
  2     example.com    IN    MX  10 mailhost.example.com.
  3
  4 /etc/postfix/main.cf:
  5     myorigin = $mydomain
  6     mydestination = $myhostname localhost.$mydomain localhost $mydomain
  7     mynetworks = 127.0.0.0/8 10.0.0.0/24
  8     relay_domains =
  9     # Optional: forward all non-local mail to firewall
 10     #relayhost = [firewall.example.com]
解释:
 •第2行:发送给example.com域名的邮件全部传递到mailhost.example.com这台机器上。记住这行最后面的点号“.”
 •第5行: 以[email protected]发送邮件
 •第6行:这个主机是发给example.com域的邮件的终点。
 •第7行:指定信任的网络
 •第8行:这个主机不中继不信任网络的邮件
 •第10行:当mailhost需要通过防火墙上的邮件网关传递外部邮件时,需要设置这个参数。中括号“[]”是提示postfix不要进行mx查询。
在这样的环境中,用户以下面的某种方式访问他们的邮箱:
 •通过NFS访问邮箱
 •通过POP或者IMAP访问邮箱
 •在用户倾向的机器上访问邮箱
在后面的示例中,每一个用户都有一个别名,从而把邮件转发到他们倾向的机器上。
 /etc/aliases:
      joe:    [email protected]
      jane:   [email protected]
在某些系统上,alias数据库不是存放在/etc/aliases目录。使用"postconf alias_maps"命令查找alias数据库的位置。
在改变了aliases的文件后,使用"newaliases"命令重新生成alias数据库文件。
postfix邮件防火墙/网关
安装postfix防火墙/网关的目的是:把example.com的邮件转发到内部的网关,但是拒绝"anything.example.com"的邮件。这儿仅有一个问题:使用relay_domains=example.com参数,防火墙也会接收anything.example.com的邮件。
注意:这个示例要求postfix版本为2.0及以后的版本。使用"postconf mail_version"查看postfix的版本
这个解决方法出现在了许多地方。第一部分解决了在防火墙上的本地邮件投递,从而使得防火墙很难被攻破。
 1 /etc/postfix/main.cf:
 2     myorigin = example.com
 3     mydestination =
 4     local_recipient_maps =
 5     local_transport = error:local mail delivery is disabled
 6
 7 /etc/postfix/master.cf:
 8     Comment out the local delivery agent
解释:
•第2行:以[email protected]发送邮件
•第3-8行:关闭防火墙上的本地邮件传递功能
出于技术正确性的考虑,防火墙必须能够接收postmaster@[firewall ip address]的邮件。这个解决方法的第二部分增加了对postmaster@[firewall ip address]的支持,同时也增加了abuse@[firewall ip address]邮箱帐户。所有发给这两个地址的邮件全部被转发到内部的邮箱地址。
 1 /etc/postfix/main.cf:
 2     virtual_alias_maps = hash:/etc/postfix/virtual
 3
 4 /etc/postfix/virtual:
 5     postmaster      [email protected]
 6     abuse           [email protected]
解释:
 •由于mydestination参数的值为空(参见前文的设置),只有与$inet_interfaces或$proxy_interfaces匹配的地址才被认为是本地。所以localpart@[a.d.d.r]能够匹配为localpart in canonical(5) and virtual(5)。这样就不需要把防火墙的ip地址写到postfix配置文中去了。
这个解决方案的最后一部分没有邮件转发功能,而这是邮件防火墙的真正目的。
  1 /etc/postfix/main.cf:
   2     mynetworks = 127.0.0.0/8 12.34.56.0/24
  3     relay_domains = example.com
  4     parent_domain_matches_subdomains =
  5         debug_peer_list smtpd_access_maps
  6     smtpd_recipient_restrictions =
  7         permit_mynetworks reject_unauth_destination
  8
  9     relay_recipient_maps = hash:/etc/postfix/relay_recipients
 10     transport_maps = hash:/etc/postfix/transport
 11
 12 /etc/postfix/relay_recipients:
 13     [email protected]   x
 14     [email protected]   x
 15      . . .
 16
 17 /etc/postfix/transport:
 18     example.com   smtp:[inside-gateway.example.com]
解释:
•第1-7行:接受来自$mynetworks的邮件,接受发给[email protected]的邮件,但是不接受来自[email protected]的邮件。关键在第4-5行。
•第9,12-14行:定义可以接收来自internet邮件的example.com域邮件帐户。这将阻止邮件队列被未投递邮件系统塞满。如果你没有维护有效的邮件接收者列表,那么你必须指定"relay_recipient_maps="(即,一个空值),或者你必须在relay_recipients表中指定一个"@example.com x"
•第10,17-18行:把发往example.com的邮件路由到内部的网关邮件服务器,用中括号[]提示postfix不要进行mx查询。
如果你的系统使用的是dbm文件,请指定dbm。使用"postconf -m"命令查看postfix支持哪些查询表
当relay_recipients的内容改变时,执行"postmap /etc/postfix/relay_recipients"命令
当传输表的内容改变时,执行"postmap /etc/postfix/transport"命令
在某些安装中,postfix的入站邮件和出站邮件处理是在两台机器上。入站的postfix实例有一个smtp服务--它用来监听外部防火墙的接口;出站的postfix实例有一个stmp服务--它用来监听内部的接口。在这样的环境中,需要在每一个实例中配置$inet_interfaces参数。
在大部分情况下,使用inet_interfaces将不会起作用,因为正如$inet_interfaces手册中所述的那样,smtp传输代理对出站的连接,将使用特定的接口地址作为源地址,smtp传输代理将不能到达防火墙“另一边”的主机。出现的症状是防火墙将无法连接到此主机。关于推荐的解决方法参见inet_interfaces参数。
部分用户可以给internet发邮件
[email protected](而不是[email protected])发送邮件的缺点是:发送给root和其他系统账号的邮件也被传递到了中心mailhost服务器上了。为了实现给这些账户的邮件在本机上传递,你可以按照下面的方法使用虚拟别名的办法:
 1 /etc/postfix/main.cf:
 2     virtual_alias_maps = hash:/etc/postfix/virtual
 3
 4 /etc/postfix/virtual:
 5     root     root@localhost
 6     . . .
解释:
 •第5行:正如virtual(5)手册中描述的那样:root匹配root@site,当site等于$myorigin时,或者当site在$mydestination的之中,或者他与$inet_interfaces/$proxy_interfaces值匹配。
在更改了virtual的内容后,执行"postmap /etc/postfix/virtual"命令。
在防火墙后面的postfix
在防火墙后面安装postfix的最简单方法是:把所有的邮件发送给网关服务器,让邮件网关负责内外部邮件的转发。示例参见上文的本地网络部分。防火墙后面安装postfix更好的方法是:仅把发送到公网的邮件传递给邮件网关,发送到内部的邮件直接进行投递。
注意:这个例子要求postfix2.0及以后的版本。使用"postconf mail_version"查看postfix的版本
下面的示例展现了额外的配置。你需要把这些配置与第一部分已讨论的基本配置结合起来。
  1 /etc/postfix/main.cf:
  2     transport_maps = hash:/etc/postfix/transport
  3     relayhost =
  4     # Optional for a machine that isn't "always on"
  5     #fallback_relay = [gateway.example.com]
  6
  7 /etc/postfix/transport:
  8     # Internal delivery.
  9     example.com      :
 10     .example.com     :
 11     # External delivery.
 12     *                smtp:[gateway.example.com]
解释:
•第2,7-12行:要求内部的邮件直接进行投递,外部的邮件传递给邮件网关。这个例子假设使用内部DNS的MX记录。中括号[]提示postfix不要进行mx查找。
•第3行:重要:不要在main.cf中指定relayhost
•第5行:这组织邮件堵在队列中,当邮件服务器关机时。postfix将尽可能的直接传递邮件,然后把未送达邮件传递给邮件网关。
当系统使用dbm文件时,指定dbm。使用"postconf -m"查看postfix支持哪些查询表。
当改变了transport表的内容时,执行"postmap /etc/postfix/transport"命令。
配置postfix作为主/备份mx主机
这部分展现了额外的配置参数。你需要把它与该文档上半部分的基本配置参数结合起来。
 
当你的系统是备份的MX邮件主机时,参见下面的设置:
  1 DNS:
  2     the.backed-up.domain.tld        IN      MX 100 your.machine.tld.
  3
  4 /etc/postfix/main.cf:
  5     relay_domains = . . . the.backed-up.domain.tld
  6     smtpd_recipient_restrictions =
  7         permit_mynetworks reject_unauth_destination
  8
  9     # You must specify your NAT/proxy external address.
 10     #proxy_interfaces = 1.2.3.4
 11
 12     relay_recipient_maps = hash:/etc/postfix/relay_recipients
 13
 14 /etc/postfix/relay_recipients:
 15     [email protected]   x
 16     [email protected]   x
 17      . . .
当你的系统是主MX的邮件主机时,你还需要接下来的设置(即1-22;如果是备份mx主机,只需设置到1-17即可)
 18 /etc/postfix/main.cf:
 19     transport_maps = hash:/etc/postfix/transport
 20
 21 /etc/postfix/transport:
 22     the.backed-up.domain.tld       relay:[their.mail.host.tld]
重要提示:
不要在mydestination,virtual_alias_domains和virtual_mailbox_domains中the.backed-up.domain.tld写进来。
• 第1-7行:把来自internet且发送给the.backed-up.domain.tld域的邮件转发给主MX记录的主机
•第10行:如果postfix通过NAT中继或proxy接收邮件时,这个设置是必须的。
•第12-16行:在the.backed-up.domain.tld域中定义有效的地址。这将保护你的邮件主机免于被未送达消息塞满。如果你没有维护有效的收件人,那么你必须relay_recipient_maps的值设为空,或者在relay_recipients表中指定一个@the.backed-up.domain.tld x
•第22行:中括号[]提示postfix不要进行mx查找
如果你的系统使用dbm,那么指定dbm
如果改变了transport的内容,执行"postmap /etc/postfix/transport"命令。
注意:当postfix为2.2之前的版本时,当为备份/主MX域中继邮件时,不要使用fallback_relay.否则,当目的服务器不可用时,邮件会在postfix的mx主机和fallback_relay主机间重复投递邮件。
 •在main.cf中指定relay_transport = relay
 •在master.cf中的relay入口的结尾指定"-o fallback_relay ="(不包括引号)
 •在传输表中,指定"relay:nexthop..."(不包括引号)作为右边的内容。
在postfix 2.2及以后的版本中,上面的设置时默认的。
拨号主机上的postfix
这部分内容应用于拨号的主机(也指该主机经常不在线)。
这部分的配置参数要与该文档的上半部分基本配置参数相结合。
如果你没有公网的主机名和ip地址,那么你应该学习《Postfix on hosts without a real Internet hostname》这部分内容。
•把所有出站的邮件路由到你的ISP邮件网关。
 如果你的邮件主机经常关机,postfix没有太多的机会把邮件投递到难以到达的某个internet角落。把这个邮件传递给那些一直在线的邮件服务器是一个很好的选择。在下面的例子中,中括号[]是为了告诉postfix不要进行mx查找。
 /etc/postfix/main.cf:
      relayhost = [smtprelay.someprovider.com]
•关闭自发的SMTP邮件传递
正常情况下,postfix将尝试以它便利的方式传递出站邮件。如果你的邮件主机使用使用了on-demand拨号IP,这将会导致你的邮件系统在发送新邮件时,自动拨号。为了阻止这样的拨号,禁用自发的SMTP邮件传递。
/etc/postfix/main.cf:
      defer_transports = smtp (Only for on-demand dialup IP hosts)
•禁用SMTP客户端的DNS查询(仅适用于拨号的局域网)
/etc/postfix/main.cf:
       disable_dns_lookups = yes (Only for on-demand dialup IP hosts)
•当internet连接建立时,清空邮件队列
把下面的命令复制到PPP或SLIP拨号脚本中:
/usr/sbin/sendmail -q (whenever the Internet link is up)
postfix的sendmail命令位置随linux版本的不同而不同。使用"postconf sendmail_path"命令查看postfix的sendmail命令的位置。
为了查看邮件队列是否被清空,使用下面的脚本:
#!/bin/sh
# Start mail deliveries.
/usr/sbin/sendmail -q
# Allow deliveries to start.
sleep 10
# Loop until all messages have been tried at least once.
while mailq | grep '^[^ ]*\*' >/dev/null
do 
    sleep 10
done
如果你禁用了自发的SMTP邮件传递,你也可以每次连接到internet网络时运行"sendmail -q"。
没有公网主机名的postfix主机
这部分适用于那些没有公网主机名的主机。一般情况是:这些主机通过ISP的DHCP获得动态的IP地址。postfix可以让你用假想(fantasy)的名字来收发邮件系统帐户间的邮件。但是你不能用假想(fantasy)名字来收发公网的邮件,因为这会导致公网用户无法回复你的邮件。实际上,越来越多的邮件系统会拒绝域名不存在的邮件。
注意:下面的内容将于postfix的版本相关。使用"postconf mail_version"来查看postfix的版本。
方案1:postfix 2.2及以后的版本
postfix 2.2使用通用的地址映射方法用internet有效地址来替换本地假想(fantasy)的邮件地址。这个地址映射仅当邮件出站时才会被使用;
下面的示例需要结合该文档的一部分基本参数设置。
 1 /etc/postfix/main.cf:
 2     smtp_generic_maps = hash:/etc/postfix/generic
 3
 4 /etc/postfix/generic:
 5     [email protected]             [email protected]
 6     [email protected]             [email protected]
 7     @localdomain.local                [email protected]
当邮件通过SMTP传递到远程的邮件主机时:
 •第5行:用ISP的邮件地址来代替[email protected]
如果你的系统使用的是dbm文件,请指定dbm。使用"postconf -m"命令查看postfix支持哪些查询表
当改变了generic的内容时,执行"postmap /etc/postfix/generic"命令。
方案2:postfix 2.1及以前的版本
老版本的postfix解决方案是使用一个有效的internet地址,让postfix把有效的internet地址转换为本地的假想(fantasy)地址。你能给内网和公网用户发送邮件。
   1 /etc/postfix/main.cf:
  2     myhostname = hostname.localdomain
  3     mydomain = localdomain
  4
  5     canonical_maps = hash:/etc/postfix/canonical
  6
  7     virtual_alias_maps = hash:/etc/postfix/virtual
  8
  9 /etc/postfix/canonical:
 10     your-login-name    [email protected]
 11
 12 /etc/postfix/virtual:
 13     [email protected]       your-login-name
解释:
•第2-3行:这里替换你的假想(fantasy)主机名。
•第5,9,10行:这三行把"[email protected]"转换成"[email protected]"。这部分是必须的。
•第7,12,13行:直接在本地传递给"[email protected]"的邮件,不需要把它发送给ISP。这部分不是必须的,但是方便的。
如果你的系统使用的是dbm文件,请指定dbm。使用"postconf -m"命令查看postfix支持哪些查询表
当改变了canonical表的内容时,执行"postmap /etc/postfix/canonical"命令
当改变了virtual表的内容时,执行"postmap /etc/postfix/virtual"命令
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章