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所示。
表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 # 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 #
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'
更多建议: