linux設定smtp伺服器

來源:互聯網
上載者:User

前  言

  在CentOS中,預設的郵件伺服器(SMTP方面)是sendmail,但sendmail有若干的缺點,比如,配置複雜、安全性漏洞曾被多次發現–並且依然存在隱患、郵件發送速度慢等等,這裡就不再一一敘述。而另一個被廣泛應用於郵件服務方面的“Postfix”的缺點就少得多,或者說它就是針對於sendmail的缺點,而被設計的。對應sendmail的短處,它在各方面也比較成熟。所以,無特殊要求,這裡不推薦用sendmail來構建郵件伺服器。本站介紹的郵件伺服器配置方法,也將基於Postfix。

添加MX記錄(這裡假設使用動態網域名稱)

  由於MX記錄添加後,可能生效要等待一段時間(通常為數分鐘或數十分鐘,也可能馬上生效),所以在安裝配置前,我們首先為動態網域名稱添加MX記錄。添加方法也會因網域名稱ISP的不同而不同,但大致資訊如下:

     mx mail.centospub.com. 10      a mail 伺服器的IP地址

  mail為別名,10為優先度。這個別名指向伺服器的IP地址。(如有疑問或需要幫忙請到 技術論壇 發貼。)

  確認MX記錄的添加是否生效的方法:

[root@sample ~]# host -t mx centospub.com centospub.com mail is handled by 10 mail.centospub.com.  ← 確認MX記錄生效

安裝Postfix

  然後,安裝Postfix。

[root@sample ~]# yum -y install postfix  ← 線上安裝Postfix Setting up Install Process Setting up repositories dag 100% |=========================| 1.1 kB 00:00 update 100% |=========================| 951 B 00:00 base 100% |=========================| 1.1 kB 00:00 addons 100% |=========================| 951 B 00:00 extras 100% |=========================| 1.1 kB 00:00 Reading repository metadata in from local files primary.xml.gz 100% |=========================| 28 kB 00:04 update : ################################################## 84/84 Added 84 new packages, deleted 1499 old in 3.44 seconds primary.xml.gz 100% |=========================| 157 B 00:00 Added 0 new packages, deleted 1499 old in 1.97 seconds primary.xml.gz 100% |=========================| 26 kB 00:00 extras : ################################################## 102/102 Added 102 new packages, deleted 1499 old in 2.73 seconds Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only Finished Parsing package install arguments Resolving Dependencies --> Populating transaction set with selected packages. Please wait. ---> Downloading header for postfix to pack into transaction set. postfix-2.2.10-1.RHEL4.2. 100% |=========================| 40 kB 00:00 ---> Package postfix.i386 2:2.2.10-1.RHEL4.2 set to be updated --> Running transaction check Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: postfix i386 2:2.2.10-1.RHEL4.2 base 3.0 M Transaction Summary ============================================================================= Install 1 Package(s) Update 0 Package(s) Remove 0 Package(s) Total download size: 3.0 M Downloading Packages: (1/1): postfix-2.2.10-1.R 100% |=========================| 3.0 MB 00:05 Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing: postfix ######################### [1/1] Installed: postfix.i386 2:2.2.10-1.RHEL4.2 Complete!

配置Postfix及相關組件

[1] 對Postfix進行配置。

[root@sample ~]# vi /etc/postfix/main.cf  ← 編輯Postfix的設定檔 #myhostname = host.domain.tld  ← 找到此行,將等號後面的部分改寫為主機名稱  ↓ myhostname = sample.centospub.com  ← 變為此狀態,設定系統的主機名稱 #mydomain = domain.tld  ← 找到此行,將等號後面的部分改寫為網域名稱  ↓ mydomain = centospub.com  ← 變為此狀態,設定網域名稱(我們將讓此處設定將成為E-mail地址“@”後面的部分) #myorigin = $mydomain  ← 找到此行,將行首的#去掉  ↓ myorigin = $mydomain  ← 變為此狀態,將發信地址“@”後面的部分設定為網域名稱(非系統主機名稱) inet_interfaces = localhost  ← 找到此行,將“localhost”改為“all”  ↓ inet_interfaces = all  ← 變為此狀態,接受來自所有網路的請求 mydestination = $myhostname, localhost.$mydomain, localhost  ← 找到此行,在行為添加“$mydomain”  ↓ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain  ← 變為此狀態,指定發給本地郵件的網域名稱 #relay_domains = $mydestination  ← 找到此行,將行首的#去掉  ↓ relay_domains = $mydestination  ← 變為此狀態,定義允許轉寄的網域名稱 #mynetworks = 168.100.189.0/28, 127.0.0.0/8  ← 找到此行,依照自己的內網情況修改  ↓ mynetworks = 168.100.189.0/28, 127.0.0.0/8  ← 變為此狀態,指定內網和本地的IP位址範圍 #home_mailbox = Maildir/  ← 找到這一行,去掉行首的#  ↓ home_mailbox = Maildir/  ← 變為此狀態,指定使用者郵箱目錄 # SHOW SOFTWARE VERSION OR NOT # # The smtpd_banner parameter specifies the text that follows the 220 # code in the SMTP server's greeting banner. Some people like to see # the mail version advertised. By default, Postfix shows no version. # # You MUST specify $myhostname at the start of the text. That is an # RFC requirement. Postfix itself does not care. # #smtpd_banner = $myhostname ESMTP $mail_name #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)  ← 找到這一行,接此行添加如下行: smtpd_banner = $myhostname ESMTP unknow  ← 添加這一行,不顯示SMTP伺服器的相關資訊 在設定檔的文尾,添加如下行: smtpd_sasl_auth_enable = yes  ← 伺服器使用SMTP認證 smtpd_sasl_local_domain = $myhostname  ← 指定SMTP認證的本地區名(主機名稱) smtpd_sasl_security_options = noanonymous   ← 不允許匿名的方式認證 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination message_size_limit = 15728640  ← 規定郵件最大尺寸為15MB

[2] 配置SMTP認證的相關選項

  為了提高安全性,我們不將系統使用者的密碼作為相應使用者SMTP認證的密碼,而將在後面為使用者建立SMTP認證專用的密碼。

[root@sample ~]# vi /usr/lib/sasl2/smtpd.conf  ← 編輯SMTP認證的設定檔 pwcheck_method: saslauthd  ← 找到此行,將“saslauthd”改為“auxprop”  ↓ pwcheck_method: auxprop  ← 不使用系統使用者密碼作為使用者的SMTP認證密碼 [root@sample ~]# vi /etc/sysconfig/saslauthd MECH=shadow  ← 找到這一行,在前面加#  ↓ #MECH=shadow  ← 不使用shadow機制 FLAGS=  ← 找到此行,在等號後面添加“sasldb”  ↓ FLAGS=sasldb   ← 定義認證方式為sasldb2

[3] 建立使用者的郵箱目錄

  首先建立使用者模板下的郵箱目錄,以便於建立新使用者時,相應使用者的郵箱目錄自動被建立。

[root@sample ~]# mkdir /etc/skel/Maildir  ← 在使用者模板下建立使用者郵箱目錄 [root@sample ~]# chmod 700 /etc/skel/Maildir  ← 設定使用者郵箱目錄屬性為700

  然後再為已經存在的使用者建立相應郵箱目錄。

[root@sample ~]# mkdir /home/centospub/Maildir  ← 為使用者(這裡以centospub使用者為例)建立郵箱目錄 [root@sample ~]# chmod 700 /home/centospub/Maildir  ← 設定該使用者郵箱目錄屬性為700 [root@sample ~]# chown centospub. /home/centospub/Maildir  ← 設定該使用者郵箱目錄為該使用者所有

[4] 為使用者佈建SMTP認證密碼

[root@sample ~]# saslpasswd2 -u sample.centospub.com -c centospub  ← 為centospub使用者佈建SMTP認證密碼 Password:  ← 在這裡輸入密碼(不會顯示) Again (for verification):  ← 再次輸入密碼

[5] 改變SALS的屬性及歸屬

[root@sample ~]# chgrp postfix /etc/sasldb2  ← 將資料庫歸屬改為postfix, [root@sample ~]# chmod 640 /etc/sasldb2  ← 將資料庫屬性改為640

[6] 關閉sendmail服務及設定預設MTA

  因為在用Postfix作為SMTP伺服器的前提下,我們不準備再用sendmail,所以將sendmail服務關掉,以確保安全及節省系統資源。

[root@sample ~]# /etc/rc.d/init.d/sendmail stop  ← 關閉sendmail服務 Shutting down sendmail:         [ OK ] Shutting down sm-client:         [ OK ] [root@sample ~]# chkconfig sendmail off  ← 關閉sendmail自啟動 [root@sample ~]# chkconfig --list sendmail  ← 確認sendmail自啟動已被關閉(都為off就OK) sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

  然後再將預設的MTA設定為Postfix。

[root@sample ~]# alternatives --config mta  ← 設定預設MTA There are 2 programs which provide 'mta'. Selection Command ----------------------------------------------- *+ 1 /usr/sbin/sendmail.sendmail  ← 目前狀態:sendmail為預設MTA  2 /usr/sbin/sendmail.postfix Enter to keep the current selection[+], or type selection number: 2  ← 在這裡輸入2,使Postfix成為預設MTA

啟動相應服務

  最後,啟動SMTP認證及Postfix服務,並設定相應服務為自啟動。

[root@sample ~]# chkconfig saslauthd on  ← 將SMTP-Auth設定為自啟動 [root@sample ~]# chkconfig --list saslauthd  ← 確認SMTP-Auth服務狀態 saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 確認2~5為on的狀態就OK [root@sample ~]# /etc/rc.d/init.d/saslauthd start  ← 啟動SMTP-Auth Starting saslauthd:           [ OK ] [root@sample ~]# chkconfig postfix on  ← 將Postfix設定為自啟動 [root@sample ~]# chkconfig --list postfix  ← 確認Postfix服務狀態 postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 確認2~5為on的狀態就OK [root@sample ~]# /etc/rc.d/init.d/postfix start  ← 啟動Postfix Starting postfix:            [ OK ]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.