15.2.1 配置Postfix服務(wù)程序

2022-06-01 09:27 更新

Postfix是一款由IBM資助研發(fā)的免費開源電子郵件服務(wù)程序,能夠很好地兼容Sendmail服務(wù)程序,可以方便Sendmail用戶遷移到Postfix服務(wù)上。Postfix服務(wù)程序的郵件收發(fā)能力強于Sendmail服務(wù),而且能自動增加、減少進程的數(shù)量來保證電子郵件系統(tǒng)的高性能與穩(wěn)定性。另外,Postfix服務(wù)程序由許多小模塊組成,每個小模塊都可以完成特定的功能,因此可在生產(chǎn)工作環(huán)境中根據(jù)需求靈活搭配它們。

第1步:安裝Postfix服務(wù)程序。這一步在RHEL7系統(tǒng)中是多余的。劉遄老師之所以還要寫上這一步驟,其目的是讓大家在學(xué)完本書之后不但能掌握RHEL系統(tǒng),還能立即上手Fedora、CentOS等主流Linux系統(tǒng)。這樣,既然這些系統(tǒng)沒有默認安裝Postfix服務(wù)程序,我們也可以自行搞定。在安裝完P(guān)ostfix服務(wù)程序后,需要禁用iptables防火墻,否則外部用戶無法訪問電子郵件系統(tǒng)。

    [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服務(wù)程序。大家如果是首次看到Postfix服務(wù)程序主配置文件(/etc/ postfix/main.cf),估計會被679行左右的內(nèi)容給嚇到。其實不用擔心,這里面絕大多數(shù)的內(nèi)容依然是注釋信息。劉遄老師在本書中一直強調(diào)正確學(xué)習(xí)Linux系統(tǒng)的方法,并堅信“負責任的好老師不應(yīng)該是書本的搬運工,而應(yīng)該一名優(yōu)質(zhì)內(nèi)容的提煉者”,因此在翻遍了配置參數(shù)的介紹,以及結(jié)合多年的運維經(jīng)驗后,最終總結(jié)出了7個最應(yīng)該掌握的參數(shù),如表15-1所示。

表15-1 Postfix服務(wù)程序主配置文件中的重要參數(shù)

參數(shù) 作用
myhostname 郵局系統(tǒng)的主機名
mydomain 郵局系統(tǒng)的域名
myorigin 從本機發(fā)出郵件的域名名稱
inet_interfaces 監(jiān)聽的網(wǎng)卡接口
mydestination 可接收郵件的主機名或域名
mynetworks 設(shè)置可轉(zhuǎn)發(fā)哪些主機的郵件
relay_domains 設(shè)置可轉(zhuǎn)發(fā)哪些網(wǎng)域的郵件

在Postfix服務(wù)程序的主配置文件中,總計需要修改5處。首先是在第76行定義一個名為myhostname的變量,用來保存服務(wù)器的主機名稱。請大家記住這個變量的名稱,下邊的參數(shù)需要調(diào)用它:

    [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的變量,用來保存郵件域的名稱。大家也要記住這個變量名稱,下面將調(diào)用它:

    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行調(diào)用前面的mydomain變量,用來定義發(fā)出郵件的域。調(diào)用變量的好處是避免重復(fù)寫入信息,以及便于日后統(tǒng)一修改:

    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行定義網(wǎng)卡監(jiān)聽地址??梢灾付ㄒ褂梅?wù)器的哪些IP地址對外提供電子郵件服務(wù);也可以干脆寫成all,代表所有IP地址都能提供電子郵件服務(wù):

    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行定義可接收郵件的主機名或域名列表。這里可以直接調(diào)用前面定義好的myhostname和mydomain變量(如果不想調(diào)用變量,也可以直接調(diào)用變量中的值):

    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步:創(chuàng)建電子郵件系統(tǒng)的登錄賬戶。Postfix與vsftpd服務(wù)程序一樣,都可以調(diào)用本地系統(tǒng)的賬戶和密碼,因此在本地系統(tǒng)創(chuàng)建常規(guī)賬戶即可。最后重啟配置妥當?shù)膒ostfix服務(wù)程序,并將其添加到開機啟動項中。大功告成!

    [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'
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號