1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC2にpostfix+dovecotで好きに使えるメールサーバーをサクッと作りたかった

Last updated at Posted at 2020-08-14

はじめに

自由に使えるメールサーバーが欲しかった
望んだのはそれだけだったんだ
それがまさかこんなことになるなんて・・・

何番煎じだよと言われてしまうが、
EC2にメールサーバーを建ててみた

似たような記事はたくさんあるが、
自分のやったことの記録として、
また新たに似たような記事を作っていく

どこかの部分がどこかの役に立てればと思う

下準備1

AWSのEC2に一先ず適当にインスタンスを立ち上げる
今回は無料枠のt2.microで建てた
OSはAMAZON LINUXを使用

建てたインスタンスのセキュリティグループでインバウンドの必要なポートを空ける

POP3	TCP 110	0.0.0.0/0	-
POP3S	TCP 995	0.0.0.0/0	-
IMAPS	TCP 993	0.0.0.0/0	-
SSH     TCP 22  0.0.0.0/0	-
SMTP	TCP 25	0.0.0.0/0	-
IMAP	TCP 143	0.0.0.0/0	-
SMTPS	TCP 465	0.0.0.0/0	-
カスタム TCP 587 0.0.0.0/0	-

※この設定はガバガバすぎるので、接続元のIPなどは各自調整してください
GIP確認ページ

下準備2

今回はお名前で取得したドメインを使ってメールサーバーに名前を付けてみた

お名前ドットコムのドメイン管理のDNSで、
今回作ったインスタンスのAレコードとMXレコードを追加する

Aレコード
例) A smtp.example.com 11.22.33.44

MXレコード
例)MX example.com 10 smtp.example.com

お名前でやらずにAWSのルート53でも可

ただメールを送るだけならドメインをわざわざ取得しなくても送れるは送れる

細かい内容などは未確認なのであしからず

postfixインストール

上記で作ったインスタンスにSSHで接続し、
とりあえずアップデートする

yum -y update

そしてpostfixインストール

yum -y install postfix

インストール成功後、設定ファイルを編集

# vi /etc/postfix/main.cf
下記部分を探して編集

myhostname = smtp.example.com

mydomain = exapmle.com

myorigin = $mydomain

inet_interfaces = all

inet_protocols = ipv4

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

smtpd_banner = $myhostname ESMTP unknown

home_mailbox = Maildir/

# 末尾に追記
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

message_size_limit = 10485760

# vi /etc/postfix/master.cf
submission inet n       -       n       -       -       smtpd #コメントアウトを外す
-o smtpd_sasl_auth_enable=yes #コメントアウトを外す

メール送信ユーザーを新規に作ったときにメール保存用ディレクトリを自動で作ってくれるようにする
# mkdir -p /etc/skel/Maildir/{new,cur,tmp}

# chmod -R 700 /etc/skel/Maildir/

smtp認証設定

# yum -y install cyrus-sasl

# chkconfig saslauthd on

# service saslauthd start

不明ユーザー宛メール対策

# vi /etc/postfix/main.cf

# 末尾に追記
local_recipient_maps =
luser_relay = unknown_user@localhost

# echo unknown_user: /dev/null >> /etc/aliases
# newaliases 

SMTP起動

sendmailがある場合はいったん止める
# chkconfig sendmail off

# service sendmail stop


# chkconfig postfix on

# service postfix start

dovecotインストール

# yum -y install dovecot
# vi /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir #コメントアウト部分を変更

# vi /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = no #noへ変更
auth_mechanisms = plain login #既存のものに追記

# vi /etc/dovecot/conf.d/10-master.conf

# 既存のものを編集
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

# vi /etc/dovecot/conf.d/10-ssl.conf

ssl = no #noへ変更

# chkconfig dovecot on

# service dovecot start

メールユーザー追加

# useradd user
# passwd user
ユーザーのパスワードを新規設定するがとりあえず「user」とかに設定しておく

# ls /home/user/Maildir/ #ディレクトリができていることを確認


メール送信テストで自分で自分に送ってみる
# sendmail user@smtp.example.com
To:user@smtp.example.com
From:user@smtp.example.com
Subject:test

myself test.

.

メールができていることを確認
# ls /home/user/Maildir/new/

この時点で必要なポートをリッスンしているかどうか確認する。

# netstat -nl | grep tcp
tcp        0      0 0.0.0.0:587                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:110                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:143                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN

メールクライアントソフトで受信してみる

thunderbirdなどの適当なメールソフトでメールを受信してみる

ユーザー:user
メールアドレス:user@example.com
パスワード:user

popサーバー:smtp.example.com 110ポート
smtpサーバー:smtp.example.com 587ポート

「sslの保護なし」で「通常のパスワード認証」

※thunderbirdから、「サーバーに接続できません。ユーザーアカウントだけ作成しますか?」と聞かれる場合もあるが、とりあえずアカウントだけ作成してみるとメールが受信できたりする。

Gmailなどに対してメールを送信できるようにする。

この段階では自分宛へメールは送れるが、外部に対しては送れないはずである。

そこでgoogleのsmtpサーバーへリレーしてメールを送信できるようにする

# vi /etc/postfix/main.cf

relayhost = [smtp.gmail.com]:587 #既存部分を編集

# 末尾に更に追記
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes

自身のgmailアカウントを利用して、gmail認証をする

# vi /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 example@gmail.com:password

ハッシュファイルを作成する
# postmap /etc/postfix/sasl_passwd

自身のgoogleアカウントで安全性の低いアプリを許可する設定をする
※アカウント認証に2段階認証など実施しているとダメらしいので、いったんそれらのセキュリティも外しておく

テストメールを送信してみる

# sendmail example@gmail.com
To:example@gmail.com
From:example@gmail.com
Subject:test

myself test.

.

送信成功したら完了

はまったところ

アカウントが同期できない

原因1 ルーターのFWにより止められていた

会社のルーターにて外向けの通信(25,110,143など)を止めるポリシーが書かれていた
これに気づくのに数時間かかり、無駄な調整ばかりしてしまった

原因2 セキュリティグループで止められていた

EC2インスタンスのセキュリティグループで必要な空けるべきポートが足りなかった

ここでもまた数時間無駄にしてしまった

原因2 クライアントソフトの相性?

今回アカウント同期をするためのソフトはoutlookでやろうとしていたが、認証はできているっぽいがアカウントは作れないといった状況になった
outlook上でのメッセージはsmtpサーバーに問題があるためアカウントが作られませんでした、といったもの
その時のサーバーログは以下のようなものである

Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: auth client connected (pid=22036)
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: client in:AUTH#0111#011PLAIN#011service=pop3#011session=OZ2Cl9esN+lqmoDo#011lip=172.31.43.165#011rip=106.154.128.232#011lport=110#011rport=59703
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: client passdb out: CONT#0111#011
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: client in: CONT<hidden>
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: Loading modules from directory: /usr/lib64/dovecot/auth
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: Module loaded: /usr/lib64/dovecot/auth/lib20_auth_var_expand_crypt.so
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: Module loaded: /usr/lib64/dovecot/auth/libdriver_sqlite.so
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: pam(user,106.154.128.232,<OZ2Cl9esN+lqmoDo>): lookup service=dovecot
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: pam(user,106.154.128.232,<OZ2Cl9esN+lqmoDo>): #1/1 style=1 msg=Password:
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: client passdb out: OK#0111#011user=user#011
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: master in: REQUEST#0111203240961#01122036#0111#011f2ecb8ac5535457503d22d0e493b0733#011session_pid=22038
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth-worker(22037): Debug: passwd(user,106.154.128.232,<OZ2Cl9esN+lqmoDo>): lookup
Aug 14 14:54:03 ip-172-31-43-165 dovecot: auth: Debug: master userdb out: USER#0111203240961#011user#011system_groups_user=user#011uid=502#011gid=502#011home=/home/user
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3-login: Login: user=<user>, method=PLAIN, rip=106.154.128.232, lip=172.31.43.165, mpid=22038, session=<OZ2Cl9esN+lqmoDo>
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3(user): Debug: Effective uid=502, gid=502, home=/home/user
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3(user): Debug: Namespace inbox: type=private, prefix=, sep=, inbox=yes, hidden=no, list=yes, subscriptions=yes location=maildir:~/Maildir
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3(user): Debug: maildir++: root=/home/user/Maildir, index=, indexpvt=, control=, inbox=/home/user/Maildir, alt=
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3(user): Debug: INBOX: Mailbox opened because: POP3 INBOX
Aug 14 14:54:03 ip-172-31-43-165 dovecot: pop3(user): Disconnected: Logged out top=0/0, retr=0/0, del=0/13, size=15260

上手くいってそうなのになんでだーと思ってググり、
outlookだとダメだけどthunderbirdだとうまくいったという人がいるようで、
PCにthunderbirdを入れて試してみたところ、
上手くいった

outlook用の設定はまた別のようである

ここでもまた(略)

参考ページ

【EC2】メールサーバ構築(Postfix + Dovecot)
メールサーバー構築(Postfix+Dovecot)
SMTPサーバとPOPサーバの実験を交えた詳解
Postfixでgmailにメール送信

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?