邮件服务系列postfix+sasl+mysql实现用户认证功能

 准备工作:

1、把本机的DNS指向自己并修改主机名

  
  
  
  
  1. [root@mail ~]# vim /etc/resolv.conf 
  2. nameserver 172.16.25.1 
  3. [root@mail ~]# vim /etc/sysconfig/network 
  4. HOSTNAME=mail.lsq.com 
  5. [root@mail ~]# hostname mail.lsq.com 
  6. [root@mail ~]# hostname 
  7. mail.lsq.com 

2、源码编译安装mysql-5.5.28         #后面的基于虚拟用户虚拟域要用到

  
  
  
  
  1. 一、编译安装cmake-2.8.8 
  2.  
  3. # tar xf cmake-2.8.8.tar.gz 
  4. # cd cmake-2.8.8 
  5. # ./bootstrap 
  6. # make  
  7. # make install 
  8.  
  9. cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源 
  10. 码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译, 
  11. 如针对于不同平台编译。 
  12.  
  13.   
  14. 二、源码编译安装mysql-5.5.28 
  15.  
  16. 1、创建mysql用户和mysql组 
  17. # groupadd -r -g 306 mysql 
  18. # useradd -g 306 -r -u 306 mysql 
  19.  
  20. 2、创建一个逻辑分区,用于存放mysql的数据 
  21.  
  22. # fdisk /dev/sda    #创建一个逻辑分区   格式为8e 
  23. # partprobe /dev/sda 
  24. # pvcreate /dev/sda5 
  25. # vgcreate myvg /dev/sda5 
  26. # lvcreate -n mydata -L 5G myvg 
  27. # mke2fs -j /dev/myvg/mydata 
  28. # mkdir /mydata 
  29. # vim /etc/fstab 
  30. /dev/myvg/mydata      /mydata     ext3     defaults    0  0 
  31. # mount -a 
  32. # mkdir /mydata/data                     #把这个目录作为数据目录 
  33. # chown -R mysql.mysql /mydata/data      #既然是数据目录,那属主属组就得是mysql 
  34. # chmod o-rx /mydata/data/               #既然是数据目录,其它用户就应该没有权限 
  35.  
  36. 3、编译安装mysql 
  37. # hwclock -s                #将系统时间同步为硬件时间 
  38. # tar xf mysql-5.5.28.tar.gz  
  39. # cd mysql-5.5.28 
  40. # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 
  41.           -DMYSQL_DATADIR=/mydata/data \ 
  42.           -DSYSCONFDIR=/etc \ 
  43.           -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 
  44.           -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 
  45.           -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 
  46.           -DWITH_READLINE=1 \ 
  47.           -DWITH_SSL=system \ 
  48.           -DWITH_ZLIB=system \ 
  49.           -DWITH_LIBWRAP=0 \ 
  50.           -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ 
  51.  
  52. # make  
  53. # make install 
  54.  
  55. 4、为mysql提供配置文件,初始化mysql,并为mysql提供服务脚本
  56. # cd /usr/local/mysql
  57. # cp support-files/my-large.cnf /etc/my.cnf 
  58. # vim /etc/my.cnf 
  59. 添加一行:datadir = /mydata/data           #指明数据目录 
  60. # chown -R mysql.mysql /usr/local/mysql/ 
  61. # cd /usr/local/mysql 
  62. # scripts/mysql_install_db --user=mysql --datadir=/mydata/data/      初始化mysql 
  63. # cp support-files/mysql.server /etc/init.d/mysqld      mysql提供的服务脚本 
  64. # service mysqld start 
  65.  
  66. 5、导出mysql的PATH环境变量,头文件,库文件,MAN文档,这些并不是必须的 
  67. 导出环境变量 
  68. # vim /etc/profile.d/mysql.sh 
  69. export PATH=$PATH:/usr/local/mysql/bin 
  70.  
  71. 导出man文档 
  72. # vim /etc/man.config  
  73. 定位至MANPATN 
  74. 添加一行:/usr/local/mysql/man 
  75.  
  76. 导出库文件 
  77. # vim /etc/ld.so.conf.d/mysql.conf 
  78. /usr/local/mysql/lib 
  79.  
  80. # ldconfig -v         #让操作系统重新读取库文件的 
  81.  
  82. 为头文件创建连接: 
  83. # ln -sv /usr/local/mysql/include /usr/include/mysql 
  84. # ls /usr/include/mysql/   
  85.  
  86.  
  87. 6、mysql-5.5.28编译成功 
  88. [root@mail ~]# mysql 
  89. Warning: mysql: ignoring option '--named-commands' due to invalid value 'root' 
  90. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  91. Your MySQL connection id is 17 
  92. Server version: 5.5.28-log Source distribution 
  93.  
  94. Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
  95.  
  96. mysql> show databases; 
  97. +--------------------+ 
  98. Database           | 
  99. +--------------------+ 
  100. | information_schema |  
  101. | mysql              |  
  102. | performance_schema |  
  103. | test               |  
  104. | wpdb               |  
  105. +--------------------+ 
  106. rows in set (0.54 sec) 
  107.  
  108. mysql>  

3、创建DNS服务器 #很重要,邮件服务必须用的

   
   
   
   
  1. 1、卸载bind 
  2. [root@mail ~]# rpm -e bind-utils 
  3.  
  4. 2、安装bind97-utils bind97-libs bind97
  5. [root@mail ~]# yum install bind97 bind97-utils   #bind97-libs依赖于utils 
  6.  
  7. 3、启动dns服务 
  8. [root@mail ~]# service named start 
  9.  
  10. 4、编写配置文件和区域数据文件 
  11. [root@mail ~]# vim /etc/named.conf 
  12. // 
  13. // named.conf 
  14. // 
  15. // Provided by Red Hat bind package to configure the ISC BIND named(8) DNS 
  16. // server as a caching only nameserver (as a localhost DNS resolver only). 
  17. // 
  18. // See /usr/share/doc/bind*/sample/ for example named configuration files. 
  19. // 
  20.  
  21. options { 
  22.         directory       "/var/named"
  23.         dump-file       "/var/named/data/cache_dump.db"
  24.         statistics-file "/var/named/data/named_stats.txt"
  25.         memstatistics-file "/var/named/data/named_mem_stats.txt"
  26.         allow-query     { localhost; }; 
  27.         recursion yes; 
  28.  
  29.         dnssec-enable yes; 
  30.         dnssec-validation yes; 
  31.         dnssec-lookaside auto; 
  32.  
  33.         /* Path to ISC DLV key */ 
  34.         bindkeys-file "/etc/named.iscdlv.key"
  35. }; 
  36.  
  37. logging { 
  38.         channel default_debug { 
  39.                 file "data/named.run"
  40.                 severity dynamic
  41.         }; 
  42. }; 
  43.  
  44. zone "." IN { 
  45.         type hint; 
  46.         file "named.ca"
  47. }; 
  48.  
  49. include "/etc/named.rfc1912.zones"
  50.  
  51. [root@mail ~]# vim /etc/named.rfc1912.zones 
  52. 在里面添加下面的内容: 
  53. zone "lsq.com" IN { 
  54.         type master; 
  55.         file "lsq.com.zone"
  56.         allow-update { none; }; 
  57.         allow-transfer { none; }; 
  58. }; 
  59. zone "25.16.172.in-addr.arpa" IN { 
  60.         type master; 
  61.         file "172.16.25.zone"
  62.         allow-update { none; }; 
  63.         allow-transfer { none; }; 
  64. }; 
  65.  
  66. 配置区域数据文件: 
  67. [root@mail ~]# cd /var/named 
  68. [root@mail named]# vim lsq.com.zone 
  69. $TTL 600 
  70. @       IN      SOA     ns.lsq.com.     admin.lsq.com. ( 
  71.                                 2013050401 
  72.                                 2H 
  73.                                 10M 
  74.                                 3D 
  75.                                 1D ) 
  76.                 IN      NS      ns 
  77.                 IN      MX 10   mail 
  78. ns              IN      A       172.16.25.1 
  79. mail            IN      A       172.16.25.1 
  80.  
  81. [root@mail named]# vim 172.16.25.zone 
  82. $TTL 600 
  83. @       IN      SOA     ns.lsq.com.     admin.lsq.com. ( 
  84.                                 2013050401 
  85.                                 2H 
  86.                                 10M 
  87.                                 3D 
  88.                                 1D ) 
  89.                 IN      NS      ns.lsq.com. 
  90. 1               IN      PTR     ns.lsq.com. 
  91. 1               IN      PTR     mail.lsq.com. 
  92.  
  93. 5、检查语法错误并重启服务 
  94. [root@mail named]# named-checkconf 
  95. [root@mail named]# named-checkzone "lsq.com" lsq.com.zone 
  96. zone lsq.com/IN: loaded serial 2013050401 
  97. OK 
  98. [root@mail named]# named-checkzone "25.16.172.in-addr.arpa" 172.16.25.zone  
  99. zone 25.16.172.in-addr.arpa/IN: loaded serial 2013050401 
  100. OK 
  101. [root@mail named]# chgrp named lsq.com.zone 172.16.25.zone [root@mail named]# chmod 640 lsq.com.zone 172.16.25.zone
  102. [root@mail named]# service named restart 
  103. Stopping named:                                            [  OK  ] 
  104. Starting named:                                            [  OK  ] 
  105.  
  106. 6、解析A记录 
  107. [root@mail named]# dig -t A mail.lsq.com @172.16.25.1 
  108.  
  109. ; <<>> DiG 9.7.0-P2-RedHat-9.7.0-6.P2.el5_7.4 <<>> -t A mail.lsq.com @172.16.25.1 
  110. ;; global options: +cmd 
  111. ;; Got answer: 
  112. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42803 
  113. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1 
  114.  
  115. ;; QUESTION SECTION
  116. ;mail.lsq.com.          IN  A 
  117.  
  118. ;; ANSWER SECTION
  119. mail.lsq.com.       600 IN  A   172.16.25.1 
  120.  
  121. ;; AUTHORITY SECTION
  122. lsq.com.        600 IN  NS  ns.lsq.com. 
  123.  
  124. ;; ADDITIONAL SECTION
  125. ns.lsq.com.     600 IN  A   172.16.25.1 
  126.  
  127. ;; Query time: 48 msec 
  128. ;; SERVER: 172.16.25.1#53(172.16.25.1) 
  129. ;; WHEN: Sun Mar 31 02:19:09 2013 
  130. ;; MSG SIZE  rcvd: 79 
  131.  
  132. 解析反向记录 
  133. [root@mail named]# dig -x 172.16.25.1 @172.16.25.1 
  134.  
  135. ; <<>> DiG 9.7.0-P2-RedHat-9.7.0-6.P2.el5_7.4 <<>> -x 172.16.25.1 @172.16.25.1 
  136. ;; global options: +cmd 
  137. ;; Got answer: 
  138. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47376 
  139. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 1 
  140.  
  141. ;; QUESTION SECTION
  142. ;1.25.16.172.in-addr.arpa.  IN  PTR 
  143.  
  144. ;; ANSWER SECTION
  145. 1.25.16.172.in-addr.arpa. 600   IN  PTR ns.lsq.com. 
  146. 1.25.16.172.in-addr.arpa. 600   IN  PTR mail.lsq.com. 
  147.  
  148. ;; AUTHORITY SECTION
  149. 25.16.172.in-addr.arpa. 600 IN  NS  ns.lsq.com. 
  150.  
  151. ;; ADDITIONAL SECTION
  152. ns.lsq.com.     600 IN  A   172.16.25.1 
  153.  
  154. ;; Query time: 1 msec 
  155. ;; SERVER: 172.16.25.1#53(172.16.25.1) 
  156. ;; WHEN: Sun Mar 31 02:20:08 2013 
  157. ;; MSG SIZE  rcvd: 115 
  158.  
  159. [root@mail named]#  

一、编译安装postfix

1、卸载系统自带的sendmail(系统环境:redhat 5.8)

    
    
    
    
  1. [root@mail ~]# service sendmail stop 
  2. [root@mail ~]# chkconfig sendmail off 
  3. [root@mail ~]# rpm -e sendmail --nodeps 

2、创建用户

    
    
    
    
  1. [root@mail ~]# groupadd -g 2525 postfix  #用于postfix的运行
  2. [root@mail ~]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix 
  3. [root@mail ~]# groupadd -g 2526 postdrop  #用于邮件投递
  4. [root@mail ~]#  useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop 

3、编译安装postfix

要想使用sasl功能,先确保这两个包安装了,一般是默认安装

sasl的头文件路径和库文件路径要和编译postfix时的一致

4、编译步骤以及验证

     
     
     
     
  1. 1、编译安装  
  2. [root@mail ~]# tar xf postfix-2.9.6.tar.gz   
  3. [root@mail ~]# cd postfix-2.9.6  
  4. [root@mail postfix-2.9.6]# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include  
  5. -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS'  
  6. 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2   
  7. -lssl -lcrypto'  
  8. [root@mail postfix-2.9.6]# make  
  9. [root@mail postfix-2.9.6]# make install  
  10.   
  11. 2、使用postfix启动服务  
  12. [root@mail postfix-2.9.6]# postfix start  
  13. postfix/postfix-script: starting the Postfix mail system  
  14. [root@mail postfix-2.9.6]# netstat -tnlp  
  15.   
  16. 3、新建一个普通用户  
  17. [root@mail postfix-2.9.6]# useradd hadoop  
  18. [root@mail postfix-2.9.6]# newaliases     #定义别名 
  19. [root@mail postfix-2.9.6]# ls /etc | grep alias  
  20. aliases  
  21. aliases.db       #一定要有这个文件 
  22. [root@mail postfix-2.9.6]# cd  
  23. [root@mail ~]# telnet 172.16.25.1 25  
  24. Trying 172.16.25.1...  
  25. Connected to mail.lsq.com (172.16.25.1).  
  26. Escape character is '^]'.  
  27. 220 mail.lsq.com ESMTP Postfix  
  28. helo mail.lsq.com  
  29. 250 mail.lsq.com  
  30. mail from:[email protected]  
  31. 250 2.1.0 Ok  
  32. rcpt to:hadoop  
  33. 250 2.1.5 Ok  
  34. data  
  35. 354 End data with <CR><LF>.<CR><LF>  
  36. hello hadoop  
  37. .  
  38. 250 2.0.0 Ok: queued as 57B7C5ABC07  
  39. quit  
  40. 221 2.0.0 Bye  
  41. Connection closed by foreign host.  
  42. [root@mail ~]# su - hadoop  
  43. [hadoop@mail ~]$ mail  
  44. Mail version 8.1 6/6/93.  Type ? for help.  
  45. "/var/spool/mail/hadoop": 1 message 1 new  
  46. >N  1 [email protected]           Sat Mar 30 23:20  13/417    
  47. & 1  
  48. Message 1:  
  49. From [email protected]  Sat Mar 30 23:20:48 2013  
  50. X-Original-To: hadoop  
  51. Delivered-To[email protected]  
  52. Date: Sat, 30 Mar 2013 23:20:18 +0800 (CST)  
  53. From[email protected]  
  54.   
  55. hello hadoop          #不定义邮件别名,这封邮件发送不成功 
  56.   
  57. & q  
  58. Saved 1 message in mbox  
  59. [hadoop@mail ~]$   


5、为postfix提供SysV风格的服务脚本

      
      
      
      
  1. [root@mail ~]# vim /etc/init.d/postfix 
  2. #!/bin/bash 
  3. # postfix      Postfix Mail Transfer Agent 
  4. # chkconfig: 2345 80 30 
  5. # description: Postfix is a Mail Transport Agent,  
  6.  
  7. which is the program \ 
  8. #              that moves mail from one machine to  
  9.  
  10. another. 
  11. # processname: master 
  12. # pidfile: /var/spool/postfix/pid/master.pid 
  13. # config: /etc/postfix/main.cf 
  14. # config: /etc/postfix/master.cf 
  15.  
  16. # Source function library. 
  17. . /etc/rc.d/init.d/functions 
  18.  
  19. # Source networking configuration. 
  20. . /etc/sysconfig/network 
  21.  
  22. Check that networking is up. 
  23. [ $NETWORKING = "no" ] && exit 3 
  24.  
  25. [ -x /usr/sbin/postfix ] || exit 4 
  26. [ -d /etc/postfix ] || exit 5 
  27. [ -d /var/spool/postfix ] || exit 6 
  28.  
  29. RETVAL=0 
  30. prog="postfix" 
  31.  
  32. start() { 
  33.     # Start daemons. 
  34.     echo -n $"Starting postfix: " 
  35.         /usr/bin/newaliases >/dev/null 2>&1 
  36.     /usr/sbin/postfix start 2>/dev/null 1>&2  
  37.  
  38. && success || failure $"$prog start" 
  39.     RETVAL=$? 
  40.     [ $RETVAL -eq 0 ] && touch  
  41.  
  42. /var/lock/subsys/postfix 
  43.         echo 
  44.     return $RETVAL 
  45.  
  46. stop() { 
  47.   # Stop daemons. 
  48.     echo -n $"Shutting down postfix: " 
  49.     /usr/sbin/postfix stop 2>/dev/null 1>&2 &&  
  50.  
  51. success || failure $"$prog stop" 
  52.     RETVAL=$? 
  53.     [ $RETVAL -eq 0 ] && rm -f  
  54.  
  55. /var/lock/subsys/postfix 
  56.     echo 
  57.     return $RETVAL 
  58.  
  59. reload() { 
  60.     echo -n $"Reloading postfix: " 
  61.     /usr/sbin/postfix reload 2>/dev/null 1>&2  
  62.  
  63. && success || failure $"$prog reload" 
  64.     RETVAL=$? 
  65.     echo 
  66.     return $RETVAL 
  67.  
  68. abort() { 
  69.     /usr/sbin/postfix abort 2>/dev/null 1>&2  
  70.  
  71. && success || failure $"$prog abort" 
  72.     return $? 
  73.  
  74. flush() { 
  75.     /usr/sbin/postfix flush 2>/dev/null 1>&2  
  76.  
  77. && success || failure $"$prog flush" 
  78.     return $? 
  79.  
  80. check() { 
  81.     /usr/sbin/postfix check 2>/dev/null 1>&2  
  82.  
  83. && success || failure $"$prog check" 
  84.     return $? 
  85.  
  86. restart() { 
  87.     stop 
  88.     start 
  89.  
  90. # See how we were called. 
  91. case "$1" in 
  92.   start) 
  93.     start 
  94.     ;; 
  95.   stop) 
  96.     stop 
  97.     ;; 
  98.   restart) 
  99.     stop 
  100.     start 
  101.     ;; 
  102.   reload) 
  103.     reload 
  104.     ;; 
  105.   abort) 
  106.     abort 
  107.     ;; 
  108.   flush) 
  109.     flush 
  110.     ;; 
  111.   check
  112.     check 
  113.     ;; 
  114.   status) 
  115.     status master 
  116.     ;; 
  117.   condrestart) 
  118.     [ -f /var/lock/subsys/postfix ] && restart  
  119.  
  120. || : 
  121.     ;; 
  122.   *) 
  123.     echo $"Usage: $0 {start|stop|restart| 
  124.  
  125. reload|abort|flush|check|status|condrestart}" 
  126.     exit 1 
  127. esac 
  128.  
  129. exit $? 
  130.  
  131. END 
  132.  
  133.   为脚本提供权限,并启动服务:
  134. [root@mail ~]# chmod +x /etc/init.d/postfix 
  135. [root@mail ~]# chkconfig --add postfix 
  136. [root@mail ~]# chkconfig --list postfix 
  137. postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off 
  138. [root@mail ~]# service postfix restart 
  139. Shutting down postfix:                                     [  OK  ] 
  140. Starting postfix:                                          [  OK  ]


5、编辑postfix的配置文件

       
       
       
       
  1. [root@mail ~]# cd /etc/postfix 
  2. [root@mail postfix]# vim main.cf 
  3. 定位至mynetworks 
  4. mynetworks = 172.16.0.0/16, 127.0.0.0/8 
  5. 定位至myhostname 
  6. myhostname = mail.lsq.com 
  7. 定位至myorigin 
  8. myorigin = $mydomain 
  9. 定位至mydomin 
  10. mydomain = lsq.com 
  11. 定位至mydestination 
  12. mydestination = $myhostname, $mydomain, localhost, ns.$mydomain 
  13. 定位至innet_interfaces 
  14. inet_interfaces = all  定义postfix进程监听的IP地址,默认是所有地址 
  15.  
  16. [root@mail postfix]# postfix -n        #可以查看配置的选项 
  17.  
  18. 没问题后重启服务 
  19.  
  20. [root@mail postfix]# service postfix restart 
  21. Shutting down postfix:                                     [  OK  ] 
  22. Starting postfix:                                          [  OK  ] 
  23.  
  24. 重启完后,一定要查看日志,查看是否报错 
  25. [root@mail postfix]# tail /var/log/maillog 
  26.  
  27.  
  28. 验证能否发送邮件 
  29. [root@mail postfix]# telnet mail.lsq.com 25 
  30. Trying 172.16.25.1... 
  31. Connected to mail.lsq.com (172.16.25.1). 
  32. Escape character is '^]'
  33. 220 mail.lsq.com ESMTP Postfix 
  34. helo mail.lsq.com 
  35. 250 mail.lsq.com 
  36. mail from:[email protected]       
  37. 250 2.1.0 Ok 
  38. rcpt to:[email protected] 
  39. 250 2.1.5 Ok 
  40. data  
  41. 354 End data with <CR><LF>.<CR><LF> 
  42. hello lsq 
  43. 250 2.0.0 Ok: queued as 63E915ABC07 
  44. quit 
  45. 221 2.0.0 Bye 
  46. Connection closed by foreign host. 
  47. [root@mail postfix]# su - hadoop 
  48. [hadoop@mail ~]$ mail 
  49. Mail version 8.1 6/6/93.  Type ? for help. 
  50. "/var/spool/mail/hadoop": 1 message 1 new 
  51. >N  1 [email protected]           Sat Mar 30 23:58  13/427   
  52. & 1 
  53. Message 1: 
  54. From [email protected]  Sat Mar 30 23:58:03 2013 
  55. X-Original-To[email protected] 
  56. Delivered-To[email protected] 
  57. Date: Sat, 30 Mar 2013 23:57:35 +0800 (CST) 
  58. From[email protected] 
  59.  
  60. hello lsq 
  61.  
  62. & q 
  63. Saved 1 message in mbox 

编译选项介绍:

myorigin参数用来指明发件人所在的域名,即做发件地址伪装;

mydestination参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到

哪个域名的邮件;

myhostname 参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名

mydomain 参数指定您的域名,默认情况下,postfix将myhostname的第一部分删除而作为

mydomain的值;

mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的

还是本地的,如果是本地网络用户则允许其访问;

inet_interfaces 参数指定postfix系统监听的网络接口;

二、安装dovecot用来接收邮件

        
        
        
        
  1. 如何接受邮件呢,我们使用dovecot 
  2. [root@mail postfix]# yum install dovecot -y 
  3. [root@mail postfix]# vim /etc/dovecot.conf  
  4. 定位至protocols 
  5. protocols = imap pop3 
  6. [root@mail postfix]# service dovecot start 
  7. Starting Dovecot Imap:                                     [  OK  ] 
  8. [root@mail postfix]# chkconfig dovecot on 
        
        
        
        
  1. [root@mail postfix]# telnet 172.16.25.1 25 
  2. Trying 172.16.25.1... 
  3. Connected to mail.lsq.com (172.16.25.1). 
  4. Escape character is '^]'
  5. 220 mail.lsq.com ESMTP Postfix 
  6. helo mail.lsq.com 
  7. 250 mail.lsq.com 
  8. mail from:[email protected] 
  9. 250 2.1.0 Ok 
  10. rcpt to:[email protected] 
  11. 250 2.1.5 Ok 
  12. data 
  13. 354 End data with <CR><LF>.<CR><LF> 
  14. Subject hello 
  15. hadoop 
  16. 250 2.0.0 Ok: queued as 1A5095ABC10 
  17. quit 
  18. 221 2.0.0 Bye 
  19. Connection closed by foreign host. 
  20.  
  21.  
  22. [root@mail postfix]# echo "redhat" | passwd --stdin hadoop 
  23. Changing password for user hadoop. 
  24. passwd: all authentication tokens updated successfully. 
  25. [root@mail postfix]# telnet mail.lsq.com 110 
  26. Trying 172.16.25.1... 
  27. Connected to mail.lsq.com (172.16.25.1). 
  28. Escape character is '^]'
  29. +OK Dovecot ready. 
  30. USER hadoop                   #用户 
  31. +OK 
  32. PASS redhat                   #密码 
  33. +OK Logged in
  34. LIST                           #查显示邮件编码 
  35. +OK 1 messages: 
  36. 1 408 
  37. RETR 1                         #查看邮件 
  38. +OK 408 octets 
  39. Return-Path: <[email protected]
  40. X-Original-To[email protected] 
  41. Delivered-To[email protected] 
  42. Received: from mail.lsq.com (ns.lsq.com [172.16.25.1]) 
  43.     by mail.lsq.com (Postfix) with SMTP id 1A5095ABC10 
  44.     for <[email protected]>; Sun, 31 Mar 2013 00:09:37 +0800 (CST) 
  45. Message-Id: <[email protected]
  46. Date: Sun, 31 Mar 2013 00:09:37 +0800 (CST) 
  47. From[email protected] 
  48.  
  49. Subject hello 
  50. hadoop 
  51.  
  52.  
  53. 还可以使用mutt来接收邮件 
  54. [root@mail postfix]# mutt -f pop://[email protected] 

三、开启 postfix + SASL 实现用户认证

        
        
        
        
  1. 1、vim /etc/sysconfig/saslauthd 
  2.      MECH=shadow 
  3.  
  4. 2、启动服务 
  5. [root@mail postfix]# service saslauthd start 
  6. Starting saslauthd:                                        [  OK  ] 
  7. [root@mail postfix]# chkconfig saslauthd on 
  8.  
  9. 3、测试能否实现用户认证 
  10. [root@mail postfix]# testsaslauthd -uhadoop -predhat 
  11. 0: OK "Success." 
  12.  
  13.  
  14. [root@mail postfix]# vim /usr/lib/sasl2/smtpd.conf    #这个配置文件添加的内容才能使postfix支持sasl功能 
  15. pwcheck_method: saslauthd 
  16. mech_list: PLAIN LOGIN 
  17.  
  18. [root@mail postfix]# service saslauthd restart 
  19. Stopping saslauthd:                                        [  OK  ] 
  20. Starting saslauthd:                                        [  OK  ] 
  21.  
  22.  
  23.  
  24. 配置postfix,使以后所有用户只有通过认证才能发送邮件 
  25. [root@mail postfix]# pwd 
  26. /etc/postfix 
  27. [root@mail postfix]# vim main.cf 
  28. 定位至mynetworks 
  29. mynetworks = 127.0.0.0/8 
  30.  
  31. 在配置文件尾部添加如下内容: 
  32. ############################ CYRUS-SASL ############################ 
  33. broken_sasl_auth_clients = yes 
  34. smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,
  35. reject_invalid_hostname, reject_non_fqdn_hostname,reject_unknown_sender_domain,
  36. reject_non_fqdn_sender,reject_non_fqdn_recipient, reject_unknown_recipient_domain,
  37. reject_unauth_pipelining,reject_unauth_destination 
  38. smtpd_sasl_auth_enable = yes 
  39. smtpd_sasl_local_domain = $myhostname 
  40. smtpd_sasl_security_options = noanonymous 
  41. smtpd_sasl_path = smtpd 
  42. smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available! 
  43.  
  44. [root@mail ~]# echo -n "hadoop" | openssl base64  #账号和密码使用base64编码的格式
  45. aGFkb29w 
  46. [root@mail ~]# echo -n "redhat" | openssl base64 
  47. cmVkaGF0 
  48.  
  49. [root@mail postfix]# telnet 172.16.25.1 25 
  50. Trying 172.16.25.1... 
  51. Connected to mail.lsq.com (172.16.25.1). 
  52. Escape character is '^]'
  53. 220 Welcome to our mail.lsq.com ESMTP,Warning: Version not Available! 
  54. ehlo mail.lsq.com 
  55. 250-mail.lsq.com 
  56. 250-PIPELINING 
  57. 250-SIZE 10240000 
  58. 250-VRFY 
  59. 250-ETRN 
  60. 250-AUTH PLAIN LOGIN 
  61. 250-AUTH=PLAIN LOGIN  #只要出现这两项就表示认证功能已开启
  62. 250-ENHANCEDSTATUSCODES 
  63. 250-8BITMIME 
  64. 250 DSN 
  65. auth login 
  66. 334 VXNlcm5hbWU6 
  67. aGFkb29w 
  68. 334 UGFzc3dvcmQ6 
  69. cmVkaGF0 
  70. 235 2.7.0 Authentication successful 

 

这就实现了postfix+sasl实现用户认证,邮件服务系列未完待续!


 

 

本文出自 “非专业linux爱好者” 博客,转载请与作者联系!

你可能感兴趣的:(邮件服务系列)