LoginSignup
3
8

More than 5 years have passed since last update.

centos7 postfix dovecotでメールサーバ構築(Gmailからメール送信できるまで)

Last updated at Posted at 2016-12-23

centos version

cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

今回VPSで設定した箇所について備忘として記載する
あくまで個人設定なので参考にしたりしなかったりしてください

修正した箇所

修正ファイル
【postfix】
/etc/postfix/main.cf
/etc/postfix/master.cf
【dovecot】
/etc/dovecot/dovecot.conf
/etc/dovecot/conf.d/10-auth.conf
/etc/dovecot/conf.d/10-mail.conf
/etc/dovecot/conf.d/10-master.conf

postfix

以下を前提に進める
postfix,dovecotが入っていること
ホスト名:example.com
メールサーバ:mail.example.com
(メールサーバ名をあらかじめDNSのMXレコードに登録する必要あり)

gmailのアカウント:587ポートで接続する(TLS)

また、letsencryptを使用して証明書を取得する
letsencryptの使い方

/etc/postfix/main.cf

コメントアウトされていたりするので以下に変更

myhostname = mail.example.com
mydomain = example.com
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
local_recipient_maps = proxy:unix:passwd.byname
relay_domains = $mydestination
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP unknown

以下を追加記載する

#SSL用の設定
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/example.com/privkey.pem
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
smtpd_use_tls = yes
smtpd_tls_session_cache_timeout = 3600s

#SMTP認証用の設定
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination
smtpd_sasl_path = private/auth 
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtp_sasl_mechanism_filter = plain

#メールの最大サイズの設定
message_size_limit = 10485760

/etc/postfix/master.cf

以下コメントを外す
smtps      inet  n       -       n       -       -       smtpd
smtp      inet  n       -       n       -       1       postscreen
smtpd     pass  -       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=$mua_client_restrictions
-o smtpd_tls_wrappermode=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject

dovecot

/etc/dovecot/dovecot.conf

protocols = pop3
listen = *, 

Gmailの方ではPOPしか使えない?のでpop3のみ指定
ipv4のみ許可

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

disable_plaintext_auth = no
auth_mechanisms = plain login

/etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir
location = maildir:~/Maildir

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

以下コメント外す
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

サービス再起動

systemctl restart postfix
systemctl restart dovecot

ここまででサーバの設定は完了

つまづいた箇所

port

Gmailにアカウント追加できない。。。

ファイアーウォールで指定のポートがあるか確認
firewall-cmd --list-all

ここで表示されてるけどpublicがactiveになっていないとfirewall自体有効でない?
このサイト参照

対象のサーバ以外から以下コマンドで接続できるか確認
telnet mail.example.com 25
telnet mail.example.com 587
以下表示されれば接続できてる
220 mail.example.com ESMTP unknown

もしうまくいかなかったら/etc/postfix/master.cfが怪しいかも
というのは

smtps      inet  n       -       n       -       -       smtpd
smtp      inet  n       -       n       -       1       postscreen
submission inet n       -       n       -       -       smtpd

この3つがコメントになっているとポートが開いてくれない

smtps -> 465ポート
smtp -> 25ポート
submission -> 587ポート

で自分で確認した限りだと465ポートも開けておかないとtelnetで587に接続できない
なのでsmtpsも開けておく(勉強不足)

あとはエラー等でてないか確認しておしまい

対象サーバでpostfixのログでエラー等ないか確認
journalctl -xafu postfix

結論

ポートが開いてなくてGmailに登録できなかったので対象ポートで接続できるか確認出来れば問題なくGmailに登録できた

ポートとか認証系がまだ理解してないのでこれから学習していこ
何かおかしかったらご指摘していただけたら大変喜びます

3
8
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
3
8