水澄net.com
Intelの趣味のHP
topサーバ・ネットワーク>postfixとdovecotでメールサーバを立てる
2024年03月21日作成

postfixとdovecotでメールサーバを立てる

1.はじめに
2.必要となるソフトウェアのインストール
3.各種設定
4.外部SMTPサーバへの転送設定
5.サーバの起動

1.はじめに

メールサーバソフトととして定番のpostfixとdovecotを利用して、通常のメーラでとりあえず使えるようにするところまでの手順を紹介します。
特に認証を設けないのであれば使えると思いますが、GmailやYahooメールあてに送る場合は、次の認証設定までやった方がよいと思います。
また、今回はメールを直接宛先サーバに飛ばすのではなく@nifty契約時についてきた、niftyのメールサーバへ飛ばして転送してもらうようにします。
これは後述するOP25Bの回避とある程度信頼されたサーバで認証を受けることで、受信拒否を防ぐために行います。

2.必要となるソフトウェアのインストール

今回必要となるメールサーバソフトウェアとして以下のものをインストールします。
・postfix - SMTPサーバ(メールの送信サーバ)
・dovecot-core - メール受信サーバの本体
・dovecot-imapd - IMAPを利用する場合のソフトウェア
・dovecot-pop3d - POP3を利用する場合のソフトウェア
利用を希望する受信メールの管理方式に応じてどちらか片方もしくは両方を選択してインストールしてください。

$ sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d

インストールの途中でpostfixのデフォルト設定を入れるか聞かれますが、いったん”設定なし”を選択しておき、
この後手動で設定を作ります。

3.各種設定

postfix側の設定

前述したとおり、postfix側は何も設定がない状態なので、以下のコマンドで新規作成します
$ sudo vi /etc/postfix/main.cf  //エディタはお好みで

設定内容は以下の通りで設定します。
各項目についてはそれぞれコメントを入れています。

#postfixのバージョンを定義します
compatibility_level = 3.6

#サーバ名を定義します
myhostname = mail.example.com
#ドメイン名を定義します
mydomain = example.com
#メール発信時のドメインを定義します
myorigin = $mydomain
#メール受信する際のドメインを定義します
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#メールの中継を許可するNWセグメントを定義します
mynetworks = 192.168.98.0/24 127.0.0.0/8

#メールボックスの場所を定義します※ホームディレクトリからの相対パス
home_mailbox = Maildir/

#TLS暗号化認証関係
smtp_tls_security_level = may
smtp_tls_loglevel = 1
smtp_tls_note_starttls_offer = yes

#SASLを有効化※SMTP認証に必要
smtpd_sasl_auth_enable = yes
#SASLでの認証方法
smtpd_sasl_type = dovecot
#認証デーモンソケットの場所※postfixキューディレクトリからの相対パス
smtpd_sasl_path = private/auth

#証明書ファイルの指定と諸設定
smtpd_tls_cert_file = /etc/letsencrypt/live/example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/example.com/privkey.pem
smtpd_tls_received_header = yes
smtpd_tls_security_level = may
smtpd_tls_loglevel = 1

#よくわからんがおまじない
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
ddd $daemon_directory/$process_name $process_id & sleep 5

#よくわからんがおまじない
mua_client_restrictions =
mua_helo_restrictions =
mua_sender_restrictions =

#メールログの書き込み場所の指定
maillog_file = /var/log/mail.log

設定の中で証明書ファイルを指定していますが、これはLet's Encryptの証明書を利用する想定で記述しています。 Let's Encryptの証明書を取得する方法については別記事にて書く予定です。

dovecot側の設定

dovecotの設定については以下のフォイルをそれぞれ設定します。
念のためデフォルト設定のファイルを別で取っておくといいかもしれません。

$ sudo vi /etc/dovecot/conf.d/10-mail.conf  //エディタはお好みで

設定内容※コメント内容は省略
mail_location = maildir:~/Maildir

namespace inbox {
inbox = yes
}

mail_privileged_group = mail

protocol !indexer-worker {
}



$ sudo vi /etc/dovecot/conf.d/10-master.conf  //エディタはおk(ry

設定内容※コメント内容は省略
service imap-login {
inet_listener imap {
port = 0
}
inet_listener imaps {
port = 993
ssl = yes
}
}

service pop3-login {
inet_listener pop3 {
port = 0
}
inet_listener pop3s {
port = 995
ssl = yes
}
}

service submission-login {
inet_listener submission {
}
}

service lmtp {
unix_listener lmtp {
}
}

service imap {
}

service pop3 {
}

service submission {
}

service auth {
unix_listener auth-userdb {
}
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
}

service auth-worker {
}

service dict {
unix_listener dict {
}
}



$ sudo vi /etc/dovecot/conf.d/10-auth.conf  //エディタh(ry

設定内容※コメント内容は省略
auth_mechanisms = plain

!include auth-system.conf.ext



$ sudo vi /etc/dovecot/conf.d/10-ssl.conf  //エディt(ry

設定内容※コメント内容は省略
ssl = yes

ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/example.com/privkey.pem

ssl_client_ca_dir = /etc/ssl/certs
ssl_dh = </usr/share/dovecot/dh.pem

”/etc/aliases.db”を以下のコマンドで作成します。
$ sudo newaliases

4.外部SMTPサーバへの転送設定

私の場合、@nifty回線を契約しているので@niftyのメールサーバが利用可能となっています。
なので、@niftyのメールサーバへ転送するものとして以下の設定を追記します。

$ sudo vi /etc/postfix/main.cf

#転送先のメールサーバアドレスとポート番号を指定
relayhost = [smtp.nifty.com]:587
#SASLの有効化
smtp_sasl_auth_enable = yes
#ユーザ認証ファイルの指定
smtp_sasl_password_maps = hash:/etc/postfix/smtp_pass
#認証設定のセキュリティオプションの指定
smtp_sasl_tls_security_options = noanonymous
#認証方式の指定
smtp_sasl_mechanism_filter = CRAM-MD5

次に設定で指定したユーザ認証ファイルを作成します。
$ sudo vi /etc/postfix/smtp_pass

設定内容は以下の通りです。
[smtp.nifty.com]:587 userid:password

次にファイルを他のユーザからアクセスできないように権限を変更します。
$ sudo chmod 600 /etc/postfix/smtp_pass

続いて作成したファイルをハッシュ化します。
$ sudo postmap /etc/postfix/smtp_pass

転送設定はこれで完了です。
なお、@nifty以外のサーバの場合は認証方式が異なる場合があるため以下のコマンドで確認できるようです。
$ telnet [smtpサーバアドレス] [ポート番号:587]
ehlo localhost //接続情報確認コマンド
quit //終了

実際の実行例
nori@e-mail:~$ telnet smtp.nifty.com 587
Trying 106.153.226.2...
Connected to smtp.nifty.com.
Escape character is '^]'.
220 nifty.com ESMTP server ready Wed, 20 Mar 2024 23:44:06 +0900
ehlo localhost
250-nifty.com
250-HELP
250-AUTH=LOGIN PLAIN CRAM-MD5
250-AUTH LOGIN PLAIN CRAM-MD5
250-PIPELINING
250-DSN
250-8BITMIME
250 SIZE 20971520
quit
221 nifty.com ESMTP server closing connection
Connection closed by foreign host.
nori@e-mail:~$

nori@e-mail:~$ telnet smtp.ocn.ne.jp 587
Trying 153.128.116.241...
Connected to smtp.ocn.ne.jp.
Escape character is '^]'.
220 ocn.ne.jp ESMTP mail service ready
ehlo localhost
250-ocn.ne.jp
250-SIZE 10485760
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250 CHUNKING
quit
221 2.0.0 Bye
Connection closed by foreign host.
nori@e-mail:~$

nori@e-mail:~$ telnet mail.so-net.ne.jp 587
Trying 202.238.84.33...
Connected to mail.so-net.ne.jp.
Escape character is '^]'.
220 ms-mxout02.so-net.ne.jp ms-mxout02.so-net.ne.jp ESMTP server ready
ehlo localhost
250-ms-mxout02.so-net.ne.jp hello [117.102.221.177], pleased to meet you
250-AUTH LOGIN PLAIN
250-SIZE 30000000
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 OK
quit
221 2.0.0 ms-mxout02.so-net.ne.jp ms-mxout02.so-net.ne.jp closing connection
Connection closed by foreign host.
nori@e-mail:~$

OCNやSo-netはプレーンなID/PW認証っぽいですね...

5.サーバの起動

次のコマンドでpostfixとdovecotを起動します。
$ sudo systemctl restart postfix.service
$ sudo systemctl restart dovecot.service

次のコマンドで起動状態を確認します。
$ sudo systemctl status postfix.service
$ sudo systemctl status dovecot.service

無事に起動が確認できればこれで完了です。
キャリアメール宛てとかであればこれで問題なく使えるかと思いますが、
GmailやYahooメールなどはセキュリティ要件が厳しく受信拒否されてしまいます。
今後、ドメイン認証周りについても記事にしていく予定です。

以上
<前へ戻る

《参考ページ》

・Zenn - Chapter 07 メールサーバ
https://zenn.dev/uchidaryo/books/ubuntu-2204-server-book/viewer/postfix-dovecot
・ベアメール - 人気のMTAとは?Postfixの設定まで一挙解説 Part2 (SMTP認証×メールリレーサーバ編)
https://baremail.jp/blog/2020/06/18/778/
・てっきんの具。 - 『Varying Vagrant Vagrants』から外部へのメール送信を設定したよ
https://www.tecking.org/archives/3663