• Linux系统下邮件服务器的搭建(Postfix+Dovecot+SSL)


    系统环境为 centos7.2 x64 mini

    1.首先安装编译环境包

    yum install nginx vim gcc gcc-c++ openssl openssl-devel db4-devel ntpdate mysql mysql-devel mysql-server bzip2 php-mysql cyrus-sasl-md5 perl-GD perl-DBD-MySQL perl-GD perl-CPAN perl-CGI perl-CGI-Session cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel libtool-ltdl-devel telnet mail libicu-devel  -y

     2.开始编译安装postfix

        a.创建相关用户

    1. yum remove postfix -y
    2. userdel postfix
    3. groupdel postdrop
    4. groupadd -g 2525 postfix
    5. useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
    6. groupadd -g 2526 postdrop
    7. useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop

        b.编译安装

    1. cd /usr/local/src/
    2. wget https://mirror.nju.edu.cn/macports/distfiles/postfix/postfix-3.3.1.tar.gz
    3. tar -zxvf postfix-3.3.1.tar.gz
    4. cd postfix-3.3.1
    5. make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lrt -lm -L/usr/lib64/sasl2 -lsasl2   -lssl -lcrypto'
    6. make && make install

        在make install环节的时候会有个交互式的界面,可以自定义一些目录,根据自己的需求填写,本人这里只更改了第二项临时文件目录,其他的都选择了默认目录:

        

        c. 更改授权

    1. chown -R postfix:postdrop /var/spool/postfix
    2. chown -R postfix:postdrop /var/lib/postfix/
    3. chown root /var/spool/postfix
    4. chown -R root /var/spool/postfix/pid

    3. 配置 postfix

    1. vi /etc/postfix/main.cf
    2. myhostname = mail.bt.dvcloud.xin
    3. mydomain = mail.bt.dvcloud.xin
    4. myorigin = $mydomain
    5. inet_interfaces = all
    6. # 推荐ipv4,如果支持ipv6,则可以为all
    7. inet_protocols = ipv4
    8. mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
    9. #mynetworks = 127.0.0.0/8, 10.0.0.0/24
    10. home_mailbox = Maildir/
    11. smtpd_banner = $myhostname ESMTP
    12. # 添加到最后
    13. # 规定邮件最大尺寸为10M
    14. message_size_limit = 10485760
    15. # 规定收件箱最大容量为1G
    16. mailbox_size_limit = 1073741824
    17. # SMTP认证
    18. smtpd_sasl_type = dovecot
    19. smtpd_sasl_path = private/auth
    20. smtpd_sasl_auth_enable = yes
    21. smtpd_sasl_security_options = noanonymous
    22. smtpd_sasl_local_domain = $myhostname
    23. smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject

    然后需要在/etc/init.d/目录下提供一个脚本来管理postfix的启动与停止

    1. vim /etc/init.d/postfix
    2. #!/bin/bash
    3. #
    4. # postfix      Postfix Mail Transfer Agent
    5. #
    6. # chkconfig: 2345 80 30
    7. # description: Postfix is a Mail Transport Agent, which is the program \
    8. #              that moves mail from one machine to another.
    9. # processname: master
    10. # pidfile: /var/spool/postfix/pid/master.pid
    11. # config: /etc/postfix/main.cf
    12. # config: /etc/postfix/master.cf
    13. # Source function library.
    14. . /etc/rc.d/init.d/functions
    15. # Source networking configuration.
    16. . /etc/sysconfig/network
    17. # Check that networking is up.
    18. $NETWORKING = "no" ] && exit 3
    19. [ -x /usr/sbin/postfix ] || exit 4
    20. [ -d /etc/postfix ] || exit 5
    21. [ -d /var/spool/postfix ] || exit 6
    22. RETVAL=0
    23. prog="postfix"
    24. start() {
    25.      # Start daemons.
    26.      echo -n $"Starting postfix: "
    27.         /usr/bin/newaliases >/dev/null 2>&1
    28.      /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start"
    29.      RETVAL=$?
    30.      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
    31.         echo
    32.      return $RETVAL
    33. }
    34. stop() {
    35.   # Stop daemons.
    36.      echo -n $"Shutting down postfix: "
    37.      /usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"
    38.      RETVAL=$?
    39.      [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
    40.      echo
    41.      return $RETVAL
    42. }
    43. reload() {
    44.      echo -n $"Reloading postfix: "
    45.      /usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload"
    46.      RETVAL=$?
    47.      echo
    48.      return $RETVAL
    49. }
    50. abort() {
    51.      /usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"
    52.      return $?
    53. }
    54. flush() {
    55.      /usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush"
    56.      return $?
    57. }
    58. check() {
    59.      /usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"
    60.      return $?
    61. }
    62. restart() {
    63.      stop
    64.      start
    65. }
    66. # See how we were called.
    67. case "$1" in
    68.   start)
    69.      start
    70.      ;;
    71.   stop)
    72.      stop
    73.      ;;
    74.   restart)
    75.      stop
    76.      start
    77.      ;;
    78.   reload)
    79.      reload
    80.      ;;
    81.   abort)
    82.      abort
    83.      ;;
    84.   flush)
    85.      flush
    86.      ;;
    87.   check)
    88.      check
    89.      ;;
    90.   status)
    91.        status master
    92.      ;;
    93.   condrestart)
    94.      [ -f /var/lock/subsys/postfix ] && restart || :
    95.      ;;
    96.   *)
    97.      echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}"
    98.      exit 1
    99. esac
    100. exit $?

    设置该脚本权限

    1. chmod +x /etc/init.d/postfix
    2. chkconfig --add postfix
    3. chkconfig postfix on
    4. chown postfix.postfix -R /var/lib/postfix/
    5. chown postfix.postfix /var/spool/ -R

    4.安装Dovecot

    yum -y install dovecot

    配置Dovecot

    1. vi /etc/dovecot/dovecot.conf
    2. #  如果不使用IPv6,请修改为*
    3. listen = *
    1. vi /etc/dovecot/conf.d/10-auth.conf
    2. disable_plaintext_auth = no
    3. auth_mechanisms = plain login
    1. vi /etc/dovecot/conf.d/10-mail.conf
    2. mail_location = maildir:~/Maildir
    1. vi /etc/dovecot/conf.d/10-master.conf
    2. # Postfix smtp验证
    3. unix_listener /var/spool/postfix/private/auth {
    4.     mode = 0666
    5.     user = postfix
    6.     group = postfix
    7. }

    然后启动服务

    1. /etc/init.d/postfix start
    2. systemctl start dovecot

    测试:就可以使用Foxmail等第三方软件来收发邮件了。

    系统用户就是邮件的用户,例如root,就是一个邮箱用户,邮箱是root@domain.com,密码就是root的密码,所以需要创建用户,只要使用useradd创建用户,再使用passwd设置密码。

    好了,假如我们创建一个admin的用户:

    1. # 创建用户
    2. useradd nineven
    3. #设置密码,会要求输入两次密码
    4. passwd nineven

    第二章,配置SSL

      

        1.创建自定义的ssl证书

    1. cd /etc/pki/tls/certs
    2. make server.key
    3. umask 77 && /usr/bin/openssl genrsa -aes128 2048 > server.key
    4. openssl rsa -in server.key -out server.key
    5. make server.csr
    6. umask 77 && /usr/bin/openssl req -utf8 -new -key server.key -out server.csr
    7. chmod 400 server.*

    2.配置

    1. vi /etc/postfix/main.cf
    2. # 添加到最后
    3. smtpd_use_tls = yes
    4. smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt
    5. smtpd_tls_key_file = /etc/pki/tls/certs/server.key
    6. smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
    7. smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    8. smtpd_tls_loglevel = 0
    9. smtpd_tls_auth_only = yes
    1. vi /etc/postfix/master.cf
    2. smtps       inet   n       -       n       -       -       smtpd
    3.   -o smtpd_tls_wrappermode=yes
    1. vi /etc/dovecot/conf.d/10-ssl.conf
    2. ssl = yes
    3. # 指定证书
    4. ssl_cert = 
    5. ssl_key = 

    然后重新启动服务

    1. /etc/init.d/postfix restart
    2. systemctl restart dovecot

  • 相关阅读:
    分享从零开始学习网络设备配置(华为ensp版本)------任务1.2 使用eNSP搭建和配置网络
    【12-商品子模块整合MyBatisPlus技术&其它模块通过generator的自动生成与补充完善】
    PyG学习 - Dataset, DataLoader, Tranforms
    文件包含漏洞利用的几种方法
    【C++】C++ 语言对 C 语言的加强 ④ ( C 语言中的三目运算符 - 不能作为左值 | C++ 语言中的三目运算符增强 | C 语言中三目运算符作为左值使用 )
    何为量子计算机?
    vue考试系统后台管理项目-接口封装调用
    hiredis在vs2010上编译不通过及解决方法
    沙盘游戏咨询感悟
    Thymeleaf学习(3)—— 内置对象
  • 原文地址:https://blog.csdn.net/ly1358152944/article/details/126436384