三、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"命令
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章