
Postfix 是一套除了 Sendmail 之外,在 Linux 中常使用的电子邮件伺服器,sendmail 虽然是长久以来被广泛使用,但也因为它的安全漏洞较多,必须经常更新版本以加强安全性,而 postfix 易于设定的优点是在这领域占有一席之地的主要原因。邮件伺服器支援邮件传送通讯协定 SMTP ( Simple Mail Transfer Protocol ),postfix 在整个电子邮件的架构中扮演着传送者 ( MTA , Mail Transfer Agent ) 的角色,利用 SMTP 协定来达成电子邮件的传送与接收。MTA 预设使用 25 port 为通讯埠,因此要设定防火墙允许 25 port 的 TCP 协定通过。
Postfix 除了支援最常见的 SMTP 协定外,它也支援扩充型传递协定 ESMTP 与 UUCP 等协定,在 Linux 的系统中,除了 postfix 外,也可以选择 sendmail、Qmail、Exim 等电子邮件伺服器。
☛ 安装必要套件
电子邮件伺服器的运作需要以下二个套件,CentOS 的套件名称如下:
① postfix
postfix 伺服器的主要套件,提供电子邮件传送功能。
② dovecot
负责 POP3、IMAP 等提供使用者取信的通讯协定伺服器。
CentOS 提供的 postfix 套件版本为 2.10,可以使用 yum 安装至最新版本,yum 安装相关套件指令如下:
[root@localhost ~ ]# yum -y install postfix dovecot
☛ 套件的目录与档案
postfix 套件有几个重要的目录与档案,说明如下:
① /etc/postfix/ 目录
伺服器的主要设定档都放置在这目录下。
② /var/log/maillog 档案
电子邮件伺服器的日志 ( 记录档 )。
③ /var/spool/postfix/ 目录
当伺服器为使用者收取电子邮件后,在使用者还未连线上来读取信件之前,会将信件放置于此目录下的子目录。
④ /etc/postfix/main.cf
伺服器的主要设定档。
⑤ /etc/postfix/access
邮件伺服器的转寄权限设定档,当有外来的 IP 连线至伺服器时,将以此档案内的资料判定是否要为该 IP address 转寄电子邮件。
☛ 设定防火墙与启动服务
设定防火墙时,利用防火墙设定指令 firewall-cmd 加入允许 stmp、pop3 与 imap 服务,并使用 –permanent 写入设定档,使其永久有效:
[root@localhost ~ ]# firewall-cmd --permanent --add-service=smtp success [root@localhost ~ ]# firewall-cmd --permanent --add-port=110/tcp success [root@localhost ~ ]# firewall-cmd --permanent --add-port=143/tcp success [root@localhost ~ ]# firewall-cmd --reload
启动 postfix 服务及设定开机自动启动服务,如下:
[root@localhost ~ ]# systemctl start postfix [root@localhost ~ ]# systemctl enable postfix
☛ 设定 postfix
Postfix 伺服器的主要设定档为 /etc/postfix/main.cf ,因为 postfix 套件安装完成后,预设只允许提供本机内部送信,并未开放所有外部连线。常用的设定值如下:
① myhostname
邮件伺服器的主机名称,应该设定为此 Linux 的 HostName,如:
# The myhostname parameter specifies the internet hostname of this # mail system. The default is to use the fully-qualified domain name # from gethostname(). $myhostname is used as a default value for many # other configuration parameters. # #myhostname = host.domain.tld #myhostname = virtual.domain.tld myhostname = ms.x-family.com.tw
② mydomain
邮件伺服器负责的网域名称,如 dom.snpy.com,与 DNS 的 MX 设定有关,如:
# The mydomain parameter specifies the local internet domain name. # The default is to use $myhostname minus the first component. # $mydomain is used as a default value for many other configuration # parameters. # #mydomain = domain.tld mydomain = x-family.com.tw
③ myorigin
由 postfix 寄出的邮件中,其寄信人的栏位要显示的网域名称,如:
# The myorigin parameter specifies the domain that locally-posted # mail appears to come from. The default is to append $myhostname, # which is fine for small sites. If you run a domain with multiple # machines, you should (1) change this to $mydomain and (2) set up # a domain-wide alias database that aliases each user to # user@that.users.mailhost. # # For the sake of consistency between sender and recipient addresses, # myorigin also specifies the default domain name that is appended # to recipient addresses that have no @domain part. # #myorigin = $myhostname #myorigin = $mydomain myorigin = $mydomain
④ inet_interfaces
若主机具有多个网路卡介面时,此时可设定与接收主机里那些网路介面传来的邮件,若全部网路介面都接收时,可用 all 设定之,如:
# The inet_interfaces parameter specifies the network interface # addresses that this mail system receives mail on. By default, # the software claims all active interfaces on the machine. The # parameter also controls delivery of mail to user@[ip.address]. # # See also the proxy_interfaces parameter, for network addresses that # are forwarded to us via a proxy or network address translator. # # Note: you need to stop/start Postfix when this parameter changes. # inet_interfaces = all #inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost #inet_interfaces = localhost
⑤ inet_protocols
邮件主机所支援的通讯协定,如:
# Enable IPv4, and IPv6 if supported #inet_protocols = all inet_protocols = ipv4
⑥ mydestination
定义这台邮件伺服器自家人的网域,如:
# The local machine is always the final destination for mail addressed # to user@[the.net.work.address] of an interface that the mail system # receives mail on (see the inet_interfaces parameter). # # Specify a list of host or domain names, /file/name or type:table # patterns, separated by commas and/or whitespace. A /file/name # pattern is replaced by its contents; a type:table is matched when # a name matches a lookup key (the right-hand side is ignored). # Continue long lines by starting the next line with whitespace. # # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS". # #mydestination = $myhostname, localhost.$mydomain, localhost #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, # mail.$mydomain, www.$mydomain, ftp.$mydomain mydestination = $myhostname, localhost.$mydomain, localhost,ms.x-family.idv.tw,ms.x-family.com.cn, ms.x-family.asia
上述的例子,是指邮件内的收信人若为 tom@ms.x-family.com.tw 、tom@ms.x-family.idv.tw、tom@ms.x-family.asia 或 tom@ms.x-family.com.cn 皆会被接收并分发到本机帐号 tom 的收信匣中。
⑦ mynetworks 与 relay_domains
mynetworks 是定义有哪些主机或网段可以为其转寄邮件,若想要允许本机与 192.168.1.* 的网段主机时,可以使用以下设定:
mynetworks = 127.0.0.1/8,192.168.1.0/24
relay_domains 是设定有哪些网域是我们这台邮件伺服器所信任,而且愿意为其转寄信件,如:
relay_domains = msa.hinet.net
☛ SMTP 帐号密码认证
以 IP address 或网域名称虽然可以限定特定来源主机,适合公司内部或是网路服务商,但如果是公司的人员出差或在外连线上网要寄信时,就难以来源 IP address 的方式辨认。此时,可以使用 SMTP 认证方式,使用者连上邮件伺服器时,提供帐号密码给伺服器进行验证,成功时即帮其转寄电子邮件。postfix 伺服器本身并未提供 SMTP 认证功能,而是由另一个专门的函式库配合而做到。
Cyrus-SASL 是一套专门的认证函式库,可以提供 postfix 作为认证时呼叫其代为认证帐号与密码是否符合,而它也是一个网路服务器,因此必需要事先启动该服务。Cyrus-SASL 在 CentOS 的套件名称是 cyrus-sasl,可以使用 rpm 指令确认其是否已安装完成,如:
[root@localhost ~ ]# rpm -qa|grep cyrus-sasl cyrus-sasl-plain-2.1.26-l7.el7.x86_64 cyrus-sasl-lib-2.1.26-l7.el7.x86_64 cyrus-sasl-2.1.26-l7.el7.x86_64 cyrus-sasl-md5-2.1.26-l7.el7.x86_64 cyrus-sasl-scram-2.1.26-l7.el7.x86_64
若未看到上述的套件清单,可使用 yum 指令进行安装:
[root@localhost ~ ]# yum -y install cyrus-sasl
☛ 设定 postfix
使用文字编辑器 gedit 修改 postfix 的设定档 /etc/postfic/main.cf,在档案的最尾端加入以下设定值:
# readme_directory: The location of the Postfix README files.
#
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination
☛ 测试
完成上述所有的安装、设定后,启动 postfix 及 cyrus-sasl 服务:
[root@localhost ~ ]# systemctl start saslauthd [root@localhost ~ ]# systemctl start postfix
使用 telnet 连线至主机的 25 port 后,以 EHLO localhost 指令进行自我介绍,主机会回应其支援的指令清单,观察是否有 LOGIN PLAIN 两个指令,若有则代表 postfix 及 Cyrus-SASL 的安装、设定成功:
[root@localhost ~ ]# telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^['. 220 ms.x-family.com.tw ESMTP Postfix EHLO localhost 250-ms.x-family.com.tw 250-PIPELINING 250-SIZE 10240000 250-ETRN 250-AUTH PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
设定每次开机时,自动启动服务:
[root@localhost ~ ]# systemctl enable saslauthd [root@localhost ~ ]# systemctl enable postfix



