Ubuntu 16.04でメールサーバーを動かす。(中・上級者向き)

  • PCサーバーにメールサーバー導入して、”自分専用のメールアドレス”を作ります。
  • メールサーバーはパラメータの設定が難しい上に、様々な機能の組み合わせで動くので、まともに送受信が出来るようになるまでに試行錯誤が必要です。それでも、”自分専用のメールアドレス”は魅力的です。

  1. Postfix送信サーバーの導入
    導入の前に、以下のサイトを参照し内容を理解しておきます。
    server-worldさん、webのゴミさん
    https://www.server-world.info/query?os=Ubuntu_16.04&p=mail
    https://blog.dshimizu.jp/article/76
  2. Postfixパッケージのインストール(postfix本体と認証用にsaslを導入します。)
    apt-get -y install postfix sasl2-bin
  3. Postfixの設定
    main.cfの設定
    cp /usr/share/postfix/main.cf.dist /etc/postfix/main.cf ←見本からコピー。
    nano /etc/postfix/main.cf
    ※main.cfは非常に長いので、必要な項目を探して設定します。
    コメント解除
    mail_owner = postfix
    コメント解除しホスト名指定
    myhostname = mail.my_server.net
    コメント解除しドメイン名指定
    mydomain = my_server.net
    コメント解除
    myorigin = $mydomain
    コメント解除
    inet_interfaces = all
    コメント解除
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
    コメント解除
    local_recipient_maps = unix:passwd.byname $alias_maps
    コメント解除
    mynetworks_style = subnet
    自ネットワーク追記
    mynetworks = 127.0.0.0/8, 192.168.1.0/24
    コメント解除
    alias_maps = hash:/etc/aliases
    コメント解除
    alias_database = hash:/etc/aliases
    コメント解除
    home_mailbox = Maildir/
    コメントにしてその下に追記
    #smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
    smtpd_banner = $myhostname ESMTP ←抽象的に記述します。
    追記
    sendmail_path = /usr/sbin/postfix
    追記
    newaliases_path = /usr/bin/newaliases
    追記
    mailq_path = /usr/bin/mailq
    追記
    setgid_group = postdrop
    コメント化
    #html_directory =
    コメント化
    #manpage_directory =
    コメント化
    #sample_directory =
    コメント化
    #readme_directory =
    最終行へ追記:送受信メールサイズを10Mに制限
    message_size_limit = 10485760
    メールボックスサイズを1Gに制限
    mailbox_size_limit = 1073741824
    ①SMTP-Auth 設定(dovecot認証の場合:送信時に受信用のID・パスワードで認証)
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/auth
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_security_options = noanonymous
    smtpd_sasl_local_domain = $myhostname
    smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject
    ②SMTP-Auth 設定(cyrus-saslの場合:送信時にMD5で暗号化されたID・パスワードで認証)
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/auth
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_security_options = noanonymous
    smtpd_sasl_local_domain = $myhostname
    broken_sasl_auth_clients = yes
    設定の反映
    newaliases
    systemctl restart postfix
    ※これで、Port 25を使用した平文メールの送信が出来るようになりました。
  4. サブミッションポート(587)の利用設定
    nano /etc/postfix/master.conf
    #sasl認証を有効にする(コメント解除)
    submission inet n – – – – smtpd
    -o smtpd_sasl_auth_enable=yes
    #smtpsを有効にする(後述の暗号化通信の設定をすれば利用可能)
    #TLSのラッパーモードを有効にする
    #smtps inet n – – – – smtpd
    # -o smtpd_tls_wrappermode=yes
    ※サブミッションポート(587)を使用したsasl認証が利用できるようになりました。
  5. saslauthdの設定とsaslパスワードの登録

    http://uayeb.com/?page_id=183
    saslauthdの設定
    nano /etc/default/saslauthd
    start = no → yes に変更します。
    touch etc/postfix/sasl/smtpd.conf
    nano smtpd.conf
    pwcheck_method: auxprop
  6. saslパスワードを登録(ユーザー数分)
    saslpasswd2 -c -u mail.my_server.net username
    パスワードを聞かれるので2回入力すればOKです。
    sasldb(パスワード格納済)の設定
    chgrp postfix /etc/sasldb2
    chmod 640 /etc/sasldb2
    ln /etc/sasldb2 /var/spool/postfix/etc ←最後のリンクを張ります。
    設定を反映します。
    /etc/init.d/saslauthd restart
  7. 暗号化パスワードの生成
    printf “%s\0%s\0%s” webmaster@mail.my_server.net webmaster@mail.my_server.net password | openssl base64 -e | tr -d ‘\n’; echo
    表示された内容をコピーしておきます。
  8. メール送信テスト(cyrus sasl認証を利用した場合)
    telnet localhost 587
    Trying 127.0.0.1…
    Connected to localhost.
    Escape character is ‘^]’.
    220 mail.my_server ESMTP
    EHLO mail.my_server
    250-mail.my_server.net
    250-PIPELINING
    250-SIZE 10485760
    250-VRFY
    250-ETRN
    250-AUTH DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN ←暗号化されたパスワードで認証するとの表示。
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250-DSN
    250 SMTPUTF8
    AUTH PLAIN d2VdaRlbWxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ←生成した暗号パスワードを貼り付けます。
    235 2.7.0 Authentication successful
    MAIL FROM:webmaster@mail.my_server.net ←webmasterはuseraddで作成しておきます。
    250 2.1.0 Ok
    MAIL FROM:webmaster@mail.my_server.net
    503 5.5.1 Error: nested MAIL command
    RCPT TO:target_user@gmail.com
    250 2.1.5 Ok
    DATA
    354 End data with <CR><LF>.<CR><LF>
    Subject: test
    From: webmaster@mail.my_sever.net
    test送信
    .
    250 2.0.0 Ok: queued as 3F457DE2ECB
    quit
    221 2.0.0 Bye
    ※target_user@gmail.comにtest送信が届いていれば成功です。←一度で成功することはまずありませんので、根気よく試します。
  9. Dovecot受信サーバーの導入

    Dovecotは、メーロボックの中身を取り出すサーバーなので、設定は簡単です。
    apt-get install dovecot-common dovecot-pop3d dovecot-imapd ←imapもインストールしておきます。
    nano etc/dovecot/dovecot.conf
    protocols = none → protocols = imap imaps pop3 pop3s
    #disable_plaintext_auth = yes
    コメント解除、noに変更
    disable_plaintext_auth = no
    設定を反映
    /etc/init.d/dovecot restart
    ※これでsaslパスワードを用いたサブミッションポート(587)によるメール送信、popポート(110)を利用したメール受信が可能になりました。
  10. 暗号化通信(SSL)
    こちらはメールを暗号化する方法です。
    暗号化通信では、SMTPSは465/TCP, POP3Sは995/TCP,IMAPSは993/TCP を使用します。←iptablesのポートを開放する必要があります。
    詳細は、以下のサイトに記載されています。サーバー証明書の取得が必要です。
    https://www.server-world.info/query?os=Ubuntu_16.04&p=mail&f=4
(Visited 1,803 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

PAGE TOP