日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux搭建邮件服务器

發布時間:2024/8/1 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux搭建邮件服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Postfix 配置并設置賬戶認證(Postfix+Sasl)

記錄一下,比較簡單的使用,郵件日志在?/var/log/maillog


安裝Postfix

系統一般都自帶了Postfix,如果不清楚可以使用下面的指令檢查
rpm -qa | grep postfix
如果沒用安裝Postfix則使用以下指令安裝
yum -y install postfix

安裝sasldb、saslauthd
用來提供smtp的虛擬賬戶和密碼服務 sasldb2包含在saslauthd當中
使用指令?yum -y install cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl-devel安裝

Postfix配置并啟用smtp
Postfix配置文件在/etc/postfix目錄下,主要修改的配置文件為:/etc/postfix/main.cf
使用指令?vi /etc/postfix/main.cf?進行編輯或者使用Xftp等工具本地編輯

附上完整的文件:

#修改以下配置 #郵件服務器的主機名 myhostname=mail.zk.com #郵件域 mydomain=zk.com #往外發郵件的郵件域 myorigin=$mydomain inet_interfaces=all #監聽的網卡 inet_protocols=all #服務的對象 mydestination=$myhostname,$mydomain home_mailbox=Maildir/#郵件存放的目錄 #新添加以下配置 #--------自定義(下面可以復制粘貼到文件最后面,用于設置服務器驗為主等) #規定郵件最大尺寸為10M message_size_limit=10485760 #規定收件箱最大容量為1G mailbox_size_limit=1073741824 #SMTP認證 #指定可以向postfix發起SMTP連接的客戶端的主機名或ip地址 smtpd_client_restrictions = permit_sasl_authenticated smtpd_sasl_auth_enable=yes smtpd_sasl_security_options=noanonymous mynetworks=127.0.0.0/8 smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination #解決發送html郵件報User unknown in local recipient table local_recipient_maps=

?

###主要修改

#設置Postfix主機名,修改成你的域名 此項需要添加A記錄并指向postfix所在主機公網IP myhostname = mail.applesa.cn #設置域名 mydomain = applesa.cn #本機postfix的郵箱域名后綴 此項默認值使用myhostname #此處使用了前項mydomain 也就是說本機postfix郵箱后綴為:@applesa.cn myorigin = $mydomain#指定Postfix系統監聽的網絡接口 此處必須是localhost或127.0.0.1或內網IP #若此項被注釋或填入公網IP Postfix服務器的端口都會將對公網開放 #默認值為all 即監聽所有網絡接口 inet_interfaces = all #網絡協議 這里ipv4即可,也可以為all,則支持ipv4,ipv6 inet_protocols = ipv4

SMTP配置,默認配置內沒有,需要手動添加到配置文件內(位置隨意)

#指定可以向postfix發起SMTP連接的客戶端的主機名或ip地址 smtpd_client_restrictions = permit_sasl_authenticated#此處permit_sasl_authenticated意思是允許通過了sasl認證的所有用戶 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, #指定postfix使用sasl驗證 通俗的將就是啟用smtp并要求進行賬號、密碼校驗 smtpd_sasl_auth_enable = yes#取消smtp的匿名登錄 此項默認值為noanonymous 此項請務必指定為noanonymous smtpd_sasl_security_options = noanonymous
  • [root@mail ~]# postfix check ? //修改保存后檢查配置文件是否有錯

  • [root@mail ~]# systemctl restart postfix.service ?

  • [root@mail ~]# systemctl enable postfix.service

  • 使用?postconf?指令即可查看你的所有Postfix配置

    使用sasldb2建立SMTP用戶和密碼

    建立使用smtp服務的賬號,就類似你的郵箱賬號(root@applesa.cn)

    編輯sasl2配置

    位置 32位?/usr/lib/sasl2/smtpd.conf?64位的在?/etc/sasl2/smtpd.conf

    #編輯通過sasl啟用smtp賬號密碼效驗的配置 vi /etc/sasl2/smtpd.conf #vi寫入或編輯內容如下: ##### pwcheck_method: auxprop auxprop_plugin: sasldb mech_list: plain login CRAM-MD5 DIGEST-MD5 #####

    修改?cyrus-sasl主配置文件

    [root@mail ~]# vim /etc/sysconfig/saslauthd ?//修改下面配置項(本地用戶認證) MECH=shadow[root@mail ~]# systemctl restart saslauthd.service [root@mail ~]# systemctl enable saslauthd.service

    修改dovecot主配置文件,啟動服務

    [root@mail ~]# vim /etc/dovecot/dovecot.conf #修改以下配置 protocols = imap pop3 lmtp listen = *, :: !include conf.d/10-auth.conf#在最后面新添加以下配置 #-----------自定義------------ ssl = no disable_plaintext_auth = no mail_location = maildir:~/Maildir [root@mail ~]# systemctl restart dovecot.service [root@mail ~]# systemctl enable dovecot.service

    創建SMTP賬號

    使用指令

    saslpasswd2 -c -u `postconf -h mydomain` zk

    或者

    saslpasswd2 -c -u applesa.cn zk

    上面的?postconf -h mydomain?是指從postfix的配置里獲取賬戶的后綴(@之后)
    下面的即為制定了@后的后綴
    最后的test即為創建的賬號頭部 最后的賬號為(zk@applesa.cn)
    回車后輸入兩次密碼即可創建賬號

    使用?sasldblistusers2?指令即可查看所有的用戶
    會列出類似下列的列表

    admin@xxx.com: userPassword test@xxx.com: userPassword no-reply@xxx.com: userPassword

    userPassword?即為使用密碼登錄,每次添加SMTP用戶之后需重啟Postfix

    ###可選操作

    #沒有問題的話會返回著色[ok]字樣 #啟動postfix systemctl restart postfix #更改sasldb2數據的權限,讓postfix可以讀取 chmod 755 /etc/sasldb2

    到這里就結束了,可以去使用了 !!!

    使用示例

    mail:host: mail.zk.comport: 25username: zk@zk.compassword: 123456default-encoding: utf-8properties:mail:smtp:ssl:enable: false

    Postfix的端口修改

    vim /etc/postfix/master.cf
    可以發現會有一行默認的

    smtp inet n - n - - smtpd

    一般在除去注釋的第一行
    復制該行進行修改,根據個人習慣改成

    smtp2 inet n - n - - smtpd

    此時的配置文件為

    smtp inet n - n - - smtpd smtp2 inet n - n - - smtpd

    ##修改/etc/services文件
    增加上方?smtp2?的端口,此處以?465?為例
    使用?vi /etc/services
    找到默認的?smtp?的端口如下:

    smtp 25/tcp mail smtp 25/udp mail

    一般在文件內的50行左右
    之后向里面添加smtp2的端口

    smtp2 465/tcp mail2 smtp2 465/udp mail2

    此時的配置文件為

    smtp 25/tcp mail smtp 25/udp mail smtp2 465/tcp mail2 smtp2 465/udp mail2

    重啟Postfix服務即可,指令?service postfix restart

    防火墻設置可選項

    查看防火墻是否對端口465有限制,修改防火墻策略

    vi /etc/sysconfig/iptables

    添加服務器規則

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 465 -j ACCEPT

    重啟防火墻即可,指令?service iptables restart

    ?

    最后附上常見問題和解決辦法

    1、如果裝了sendmail的話,先卸載了。

    yum remove sendmail

    2、安裝 Postfix

    yum install postfix

    3、更改默認MTA為Postfix:

    /usr/sbin/alternatives --set mta /usr/sbin/sendmail.postfix

    4、完事兒,再次檢查下是否將MTA改為Postfix了:

    alternatives --display mta

    5、配置Postfix,vi編輯main.cf

    vi /etc/postfix/main.cf

    打開后分別找到以下幾項,將其前面的#去掉,并做配置:

    myhostname = mail.wpdaxue.commydomain = wpdaxue.commyorigin = $mydomaininet_interfaces = allinet_protocols = ipv4mydestination = $myhostname, localhost.$mydomain, localhost, $mydomainmynetworks = 192.168.100.10/28, 127.0.0.0/8relay_domains =home_mailbox = Maildir/

    以上幾個設置項不在同一個位置,需要分別尋找,并進行更改,其中相應的域名請改為自己的地址,mynetworks我是沒有做改變,自動就有的。我剛開始也是配置錯了導致安裝失敗,后來在?@朱茂海的博客找到該答案,以上幾項的含義解釋也來自該網站,感謝作者提供。

    配置文件解釋:
    mydomain:
    mydomain參數是指email服務器的域名,請確保為正式域名(如centos.bz)
    myhostname:
    myhostname參數是指系統的主機名稱(如我的服務器主機名稱是mail.centos.bz)
    myorigin:
    myorigin參數指定本地發送郵件中來源和傳遞顯示的域名。在我們的例子中,mydomain是centos.bz,也是我的域名。
    對于下面的一行,我們的郵件地址是user@centos.bz而不是user@mail.centos.bz。
    myorigin = $mydomain
    mynetworks:
    mynetworks參數指定受信任SMTP的列表,具體的說,受信任的SMTP客戶端允許通過Postfix傳遞郵件。
    mydestination:
    mydestination參數指定哪些郵件地址允許在本地發送郵件。這是一組被信任的允許通過服務器發送或傳遞郵件的IP地址。用戶試圖通過發送從此處未列出的IP地址的原始服務器的郵件將被拒絕。
    inet_interfaces:
    inet_interfaces參數設置網絡接口以便Postfix能接收到郵件。
    relay_domains:
    該參數是系統傳遞郵件的目的域名列表。如果留空,我們保證了我們的郵件服務器不對不信任的網絡開放。
    home_mailbox:
    該參數設置郵箱路徑與用戶目錄有關,也可以指定要使用的郵箱風格。

    6、檢測并啟動Postfix

    service postfix status //檢測,若已啟動會列出其PID,如下: master (pid 25579) is running...//若未啟動,執行以下命令: service postfix start

    7、設置開機啟動

    chkconfig postfix on

    我自己的安裝到此為止就結束了,因為我只需要的是mail功能,并非搭建郵件服務器。此時測試郵件發送,OK。

    參考資料:http://www.gongzi.org/centos-install-postfix-sendmail.html

    8、補充:如果你之前沒有配置過 sendmail 組件,那你很可能沒有修改 php.ini 的郵件發送路徑,那樣可能無法發送郵件。

    所以你需要配置php.ini,填寫sendmail的絕對路徑:

    使用命令打開編輯php.ini

    vi /usr/local/php/etc/php.ini

    輸入?sendmail_path 查找定位(或者手動跳轉找到sendmail_path),你會發現默認是下面的代碼

    ;sendmail_path =

    按 i 進入編輯,將這行修改為

    sendmail_path = /usr/sbin/sendmail -t -i

    按 Esc 鍵退出編輯,輸入 :wq 保存退出

    重啟php-fpm進程?/etc/init.d/php-fpm restart

    ?

    如果還沒安裝php當然就沒有/usr/local/php/etc/php.ini或者/etc/php.ini文件了,

    安裝:yum -y install php

    php-cgi restart

    ?

    9、補充2:通過以上配置,就可以正常發送郵件了,但是發件人郵箱會是 www@wpdaxue.com 這種類型,如果想修改 www 這個參數,比如修改為 admin@wpdaxue.com 需要按照下面的步驟操作:

    編輯 /etc/postfix/canonical:

    vi /etc/postfix/canonical

    添加下面類型的代碼:

    www admin@wpdaxue.com

    保存退出,然后進入到 /etc/postfix/ 目錄:

    cd /etc/postfix/

    執行命令:

    postmap canonical

    接著編輯 /etc/postfix/main.cf:

    vi /etc/postfix/main.cf

    一般情況下里面沒有canonical_maps這個參數,在空行處添加:

    canonical_maps = hash:/etc/postfix/canonical

    保存退出,然后重啟 postfix:

    postfix reload

    再次發送郵件測試,奇跡應該就發生啦!

    10、補充3:通過上面的設置,雖然可以正常發送郵件,但是建議添加 DNS 解析,以減少被判定為垃圾郵件的幾率。通常需要添加3個解析:A記錄、MX記錄、TXT記錄,比如這里的 wpdaxue.com 域名的解析如下:

    ?

    這樣就好多了,最后,推薦使用下面的網站來測試你的服務器發送的郵件所得分數,它會給出詳細的測試結果和改進方法:

    ?

    ?

    問題:

    1.postdrop: warning: unable to look up public/pickup: No such file or directory

    解決 mkfifo /var/spool/postfix/public/pickup

    2.?postqueue: warning: unable to look up public/qmgr: No such file or directory

    解決 mkfifo /var/spool/postfix/public/qmgr

    3.postqueue: fatal: Cannot flush mail queue - mail system is down

    原因:可能25端口被占用?

    lsof -i:25 如果有占用程序 kill -9 PID 殺了那個程序重啟 postfix

    4. 給騰訊郵箱發郵件報錯:status=bounced (host mx3.qq.com[183.57.48.35] said: 550 Ip frequency limited. http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000725 (in reply to end of DATA command))

    解決:在騰訊郵箱中設置白名單

    ?

    ?

    ?第一個設置個人,第二個設置域

    ?

    ?5. status=bounced (unknown user: "kefu.czj")

    解決:vim /etc/postfix/main.cf

    mydestination = $mynetworks, $myhostname, localhost.$mydomain, localhost, $mydomain 這一行中

    $mydomain和虛擬域名沖突了,當郵件發過來時會優先找mydestination的域名,而mydestination里的域名中是沒有這些用戶的,因而找不到用戶名。

    解決辦法就是把$mydomain和$myhostname刪除,或者改為不和虛擬域名一樣

    ?

    6.status=bounced (host mxn.mxhichina.com[42.120.219.27] said: 502 Mailfrom account is a local account root@520czj.com (in reply to MAIL FROM command))

    原因:對方的郵件地址,使用的是萬網的企業郵局,而發信人用的也是萬網注冊的域名,加上退信的內容是Mialfrom account is a local account,這樣就表明,發信人的域名存在了萬網的郵件服務器上,這樣發信人自架的郵件發信過去,就自然會提示這個錯誤了。

    ?辦法一:換一個域名

    辦法二(沒親測):就是刪除萬網上的企業郵件即可,即在萬網的郵件服務器上不要有你的那個郵件域。

    ?

    其它錯誤:

    1.status=bounced (host mx54.dns.com.cn[220.181.130.176]said: 550 Does not like recipient,your mail is rejected! (in reply to end of DATA command)); sender non-delivery notification: F20F2DD80FC;removed

    出錯原因:對方某一臺收件服務器當時正處在繁忙之中或者我們的服務器和對方鏈接并不是很順暢

    解決方法:有可能是暫時的情況,用別的地址測試一下,看看是否被退信,或者過一段時間再發送一下

    ?

    2.status=bounced (host 192.168.21.20[192.168.21.20] said: 552 5.2.2 Over quota (in reply to end of DATA command));

    出錯原因:對方郵箱空間已滿

    解決方法:對方清理郵箱重新發送

    ?

    3.1)status=bounced(hostmx3.qq.com[183.60.2.53]said:550Mailboxnotfound.http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000728 (in reply to RCPT TO command)); sender non-delivery notification: 9CC7FDD8084; removed

    3.2)status=bounced (host 163mx01.mxmail.netease.com[220.181.12.62] said: 550 User not found: cjld@163.com (in reply to RCPT TO command)); sender non-delivery notification: 8FD7CA00013A; removed

    出錯原因:收件人地址不存在或者收件人地址寫錯

    解決方法:核對地址后重新發送

    ?

    4.status=deferred(hostmxzwg4.mxhichina.com[58.68.254.104]said:450MI:IPBhttp://www.net.cn/service/faq/youx/mailsy/200905/3781.html (in reply to MAIL FROM command))

    出錯原因:450MI:IPB發信IP被暫時禁止,請檢查是否有用戶發送病毒或者垃圾郵件。

    解決方法:聯系您的企業郵箱運營商

    ?

    5.status=deferred(connecttoapllo-zm.com[60.191.124.236]:25:Connectiontimedout);from=<jinxiuren@hanshitx.com>, size=24500463, nrcpt=2 (queue active);

    出錯原因:與對方郵箱服務器連接超時

    解決方法:?首先確認對方郵箱地址中的域名部分是否寫錯,如果沒錯,telnet對方郵箱ip的25端口是否連接,如果連接正常,那么基本上是暫時的網絡問題。

    6.1)status=bounced (host sohumx1.sohu.com[220.181.26.202] said: 553 5.7.3 CONTENT REJECT:10.10.71.82.2011062016.XXD6ywq8:dspam check:5:http://mail.sohu.com/info/policy/12 (in reply to end of DATA command));

    6.2)Recipient address rejected: User unknown in local recipient table

    出錯原因:1)內容被拒絕,可能是收件人對所接收的郵件進行了設置,內容中含有特定詞匯等會被拒絕

    ??????????????2)收件人地址被拒絕

    解決方法:核實對方是否對內容及賬戶進行了設置之后重新發送

    ?

    7.Clienthostrejected: Access denied; from=<cici@ybxchina.com> to=<terancetan@lovelylace.com> proto=SMTP helo=<hjy1>

    出錯原因:客戶端主機被拒絕

    解決方法:看一下客戶端接收郵件服務器設置是否正確,“我的服務器需要身份驗證”是否打鉤,測試一下發信25口是否已連接

    ?

    8. status=bounced (host mx1.mail.cnb.yahoo.com[203.209.228.250] said:?554 delivery error: dd Sorry your message to weixing201314@yahoo.com.cn cannot be delivered. This account has been disabled or discontinued [#102]. - mta1004.mail.cnb.yahoo.com (in reply to end of DATA command)); sender non-delivery notification: 1C580DD80B4; removed

    出錯原因:對方賬號被停用或者不可用

    解決方法:正常退信,換賬號發送

    ?

    9.status=bounced (message size 24864357 exceeds size limit 10240000 of server cluster4.us.messagelabs.com[98.126.139.202]); sender non-delivery notification: C6A3BDD8078; removed

    出錯原因:郵件過大,超出對方單封郵件接收范圍(已解決)

    解決方法:對方換賬號,重新發送

    10.?status=bounced (host 163mx03.mxmail.netease.com[220.181.12.90] said: 552 Requested mail action aborted: exceeded mailsize limit (in reply to end of DATA command));

    出錯原因:發送的信件大小超過了網易郵箱允許接收的最大限制

    解決方法:把附件壓縮或者分次發送或者換其他郵箱接收

    ?

    11. <Eric.Goetzman@chp.edu>: host mail.messaging.microsoft.com[216.32.180.22] said:

    ??550 5.7.1 Service unavailable; Client host [98.126.139.202] blocked using Blocklist 2, mail from IP banned; To request removal from this list please forward this message to delist@messaging.microsoft.com. (in reply to RCPT TO command)

    Reporting-MTA: dns; hw.upanshi.com

    X-Postfix-Queue-ID: CC2D4681FD

    X-Postfix-Sender: rfc822; order@ptm-biolab.com

    Arrival-Date: Thu, 13 Oct 2011 19:48:18 -0700 (PDT)

    Final-Recipient: rfc822; Eric.Goetzman@chp.edu

    Original-Recipient: rfc822;Eric.Goetzman@chp.edu

    退信原因:ip被列入黑名單

    解決方法:根據退信提示解封ip

    ?

    12.postfix warning: dict_nis_init: NIS domain name not set - NIS lookups disabled 解決方法

    生成/etc/postfix/aliases別名數據庫:

    [root@mail postfix]#?postalias /etc/postfix/aliases

    在/etc/postfix/main.cf中加入下面配置信息

    ?

    alias_database = hash:/etc/postfix/aliases

    alias_maps = hash:/etc/postfix/aliases

    總結

    以上是生活随笔為你收集整理的linux搭建邮件服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。