目次

Postfix でメールサーバ

メールサーバとして Postfix をインストール。

Postfixのインストール

apt-get でインストール。

$ sudo apt-get install postfix

インストール中にダイアログで Postfic の設定ができる。 最初にサーバの使用目的についての説明がでるので、読んだら OK を押す。

設定

インストール中の設定ではほとんど設定できていないので、追加で設定。

設定用のコマンドがあるのでそれを利用する。

$ sudo dpkg-reconfigure postfix

途中まではインストール中に設定した項目なので飛ばす。

メール保存形式をデフォルトの mbox から maildir に変更。

$ sudo postconf -e 'home_mailbox = Maildir/'

設定内容は /etc/postfix/main.cf に保存されている。

SMTP Authentication

SASLを使って暗号化された回線でSMTP接続できるようにする。

/etc/postfix/main.cf に以下を追加。

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth-client
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
inet_interfaces = all

サーバの鍵を用意する。自己署名サーバ証明書の作成

サーバの鍵を /etc/ssl/private/server.key に crtファイルを /etc/ssl/certs/server.crt に配置。 server.key はパスワードが解除してあるので、パーミッションとオーナーを変更しておく。

$ sudo chown root:ssl-cert /etc/ssl/private/server.key
$ sudo chmod 640 /etc/ssl/private/server.key

CAの証明書を /etc/ssl/certs/cacert.pem に配置。

/etc/postfix/main.cf に以下を追加。

smtpd_tls_auth_only = no
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/ssl/private/server.key
smtpd_tls_cert_file = /etc/ssl/certs/server.crt
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom
myhostname = mail.example.com

smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem

最後に Postfix を再起動。

$ sudo /etc/init.d/postfix restart

SASLの設定

AMTP Authentication で使用する SASL を設定する。

必要なパッケージをインストール。

$ sudo apt-get install dovecot-common

/etc/dovecot/dovecot.conf を編集する。socket listen を検索して以下のように変更。

  socket listen {
    #master {
      # Master socket provides access to userdb information. It's typically
      # used to give Dovecot's local delivery agent access to userdb so it
      # can find mailbox locations.
      #path = /var/run/dovecot/auth-master
      #mode = 0600
      # Default user/group is the one who started dovecot-auth (root)
      #user =
      #group =
    #}
    client {
      # The client socket is generally safe to export to everyone. Typical use
      # is to export it to your SMTP server so it can do SMTP AUTH lookups
      # using it.
      #path = /var/run/dovecot/auth-client
      path = /var/spool/postfix/private/auth-client
      mode = 0660
      user = postfix
      group = postfix
    }
  }

Outlook から接続する場合は mechanisms に login を追加。

$   mechanisms = plain login

テスト

設定が終わったら telnet でテストする。

$ telnet mail.example.com 25

telnet から以下のコマンドを実行。

ehlo mail.example.com

以下の出力が含まれていれば OK。“LOGIN” は Outlook のための設定を行った場合に表示される。

250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME

SSL/TLS接続を有効にする

STARTTLS ではなく直接465ポートにアクセスして暗号化接続を行えるようにする。

$ sudo vi /etc/postfix/master.cf

コメントを3行分はずす。

smtps     inet  n       -       -       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

その後再起動かリロード。

$ sudo /etc/init.d/postfix restart

スパムの踏み台チェック

外部からの不正リレーをきちんと防げているかをチェックするには、メールサーバから以下のコマンドを実行する。

$ telnet relay-test.mail-abuse.org

root 宛てのメールを転送

root にメールを送信されても受信する機会がないので、root 宛てのメールはシステム管理をするユーザに転送する。

$ sudo vi /etc/aliases

root 宛てのメッセージを他のユーザに転送する。

# See man 5 aliases for format
postmaster:    root

root:   foobar

/etc/aliases を編集した後に以下のコマンドで設定を適用する。

$ sudo newaliases

参考

公式ドキュメント Postfix インストール