目的
注意!! この記事は2018年9月に書き留めていたメモ書きを元にしています。すでにだいぶ古い情報になっていますので、皆様の参考にはならないと思います。自分の備忘録として残しています。
前回の記事でさくらのVPSにUbuntuを入れたので、次はメールサーバを構築します。使うのはPostfixとDovecotです。あくまで個人で使う目的のメールサーバです。
正直ちゃんとまとめて書いてないので参考にならないと思います……。
Postfixインストール
$ sudo apt install postfix Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: ssl-cert Suggested packages: procmail postfix-mysql postfix-pgsql postfix-ldap postfix-pcre sasl2-bin dovecot-common postfix-cdb mail-reader postfix-doc openssl-blacklist The following NEW packages will be installed: postfix ssl-cert 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 1,169 kB of archives. After this operation, 3,759 kB of additional disk space will be used. Do you want to continue? [Y/n]
Postfix Configurationという画面が現れ、Please select the mail server configuration type that best meets your needs.と言われる。 選択肢は以下の5つ。
-No configuration -Internet Site -Internet with smarthost -Satellite system -Loacl only
Internet Siteを選んだ。
System mail nameを入力するように言われる。自分のドメイン名を入力する。mail.とか付けていない素のドメイン名。
そして処理が進む。
Get:1 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 ssl-cert all 1.0.37 [16.9 kB] Get:2 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 postfix amd64 3.1.0-3ubuntu0.3 [1,152 kB] Fetched 1,169 kB in 0s (4,524 kB/s) Preconfiguring packages ... Selecting previously unselected package ssl-cert. (Reading database ... 91489 files and directories currently installed.) Preparing to unpack .../ssl-cert_1.0.37_all.deb ... Unpacking ssl-cert (1.0.37) ... Selecting previously unselected package postfix. Preparing to unpack .../postfix_3.1.0-3ubuntu0.3_amd64.deb ... Unpacking postfix (3.1.0-3ubuntu0.3) ... Processing triggers for man-db (2.7.5-1) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for ufw (0.35-0ubuntu2) ... Processing triggers for systemd (229-4ubuntu21.4) ... Processing triggers for ureadahead (0.100.0-19) ... Setting up ssl-cert (1.0.37) ... Setting up postfix (3.1.0-3ubuntu0.3) ... Adding group `postfix' (GID 118) ... Done. Adding system user `postfix' (UID 110) ... Adding new user `postfix' (UID 110) with group `postfix' ... Not creating home directory `/var/spool/postfix'. Creating /etc/postfix/dynamicmaps.cf Adding group `postdrop' (GID 119) ... Done. setting myhostname: *****.sakura.ne.jp setting alias maps setting alias database changing /etc/mailname to mydomain.com setting myorigin setting destinations: $myhostname, mydomain.com, *****.sakura.ne.jp, localhost.**.sakura.ne.jp, localhost setting relayhost: setting mynetworks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 setting mailbox_size_limit: 0 setting recipient_delimiter: + setting inet_interfaces: all setting inet_protocols: all /etc/aliases does not exist, creating it. WARNING: /etc/aliases exists, but does not have a root alias. Postfix is now set up with a default configuration. If you need to make changes, edit /etc/postfix/main.cf (and others) as needed. To view Postfix configuration values, see postconf(1). After modifying main.cf, be sure to run '/etc/init.d/postfix reload'. Running newaliases Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for systemd (229-4ubuntu21.4) ... Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for ufw (0.35-0ubuntu2) ...
ここから設定に入るわけだが、viで編集するのが辛いのでvimを入れる。
$ sudo apt install vim
main.cfを編集する。ここでいろいろ試行錯誤してみたが、自信がないのでどう設定したかは省略させていただきます。
aliasesを編集する。
$ sudo vim /etc/aliases $ sudo newaliases
/etc/aliased.dbのタイムスタンプが更新されていることを確認する。
確認のためメールを送ってみる。mailコマンドを使えるようにするため、mailutilsをインストールする。
$ sudo apt install mailutils Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: guile-2.0-libs libgc1c2 libgsasl7 libkyotocabinet16v5 libltdl7 liblzo2-2 libmailutils4 libmysqlclient20 libntlm0 libpython2.7 libunistring0 mailutils-common mysql-common Suggested packages: mailutils-mh mailutils-doc The following NEW packages will be installed: guile-2.0-libs libgc1c2 libgsasl7 libkyotocabinet16v5 libltdl7 liblzo2-2 libmailutils4 libmysqlclient20 libntlm0 libpython2.7 libunistring0 mailutils mailutils-common mysql-common 0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded. Need to get 5,867 kB of archives. After this operation, 27.9 MB of additional disk space will be used. Do you want to continue? [Y/n] Y (省略)
mailコマンドで自分宛にメールを送ってみる。メール本文の終わりはCtrl+Dを打つのが正しいみたいだ。
$ mail yourusername@localhost Cc: Subject: Test mail Hi!
その後自分のホームディレクトリを見てみると、勝手にMaildirディレクトリができている。事前に作っておく必要はなかったみたい。 同様してmailコマンドで自分のGmailアカウント宛に送ってみたが、なんと送れた。あれ、submissionポートとか設定していないといけない気もしたけど……。まあ送れたからいいや。
dovecotインストール
$ sudo apt install dovecot-core dovecot-imapd dovecot-pop3d Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libexttextcat-2.0-0 libexttextcat-data ntpdate Suggested packages: dovecot-gssapi dovecot-sieve dovecot-pgsql dovecot-mysql dovecot-sqlite dovecot-ldap dovecot-lmtpd dovecot-managesieved dovecot-solr The following NEW packages will be installed: dovecot-core dovecot-imapd dovecot-pop3d libexttextcat-2.0-0 libexttextcat-data ntpdate 0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded. Need to get 2,819 kB of archives. After this operation, 8,987 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 ntpdate amd64 1:4.2.8p4+dfsg-3ubuntu5.9 [48.6 kB] Get:2 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 libexttextcat-data all 3.4.4-1ubuntu3 [164 kB] Get:3 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 libexttextcat-2.0-0 amd64 3.4.4-1ubuntu3 [13.1 kB] Get:4 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dovecot-core amd64 1:2.2.22-1ubuntu2.8 [2,423 kB] Get:5 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dovecot-imapd amd64 1:2.2.22-1ubuntu2.8 [142 kB] Get:6 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dovecot-pop3d amd64 1:2.2.22-1ubuntu2.8 [28.4 kB] Fetched 2,819 kB in 0s (4,473 kB/s) Selecting previously unselected package ntpdate. (Reading database ... 94235 files and directories currently installed.) Preparing to unpack .../ntpdate_1%3a4.2.8p4+dfsg-3ubuntu5.9_amd64.deb ... Unpacking ntpdate (1:4.2.8p4+dfsg-3ubuntu5.9) ... Selecting previously unselected package libexttextcat-data. Preparing to unpack .../libexttextcat-data_3.4.4-1ubuntu3_all.deb ... Unpacking libexttextcat-data (3.4.4-1ubuntu3) ... Selecting previously unselected package libexttextcat-2.0-0:amd64. Preparing to unpack .../libexttextcat-2.0-0_3.4.4-1ubuntu3_amd64.deb ... Unpacking libexttextcat-2.0-0:amd64 (3.4.4-1ubuntu3) ... Selecting previously unselected package dovecot-core. Preparing to unpack .../dovecot-core_1%3a2.2.22-1ubuntu2.8_amd64.deb ... Unpacking dovecot-core (1:2.2.22-1ubuntu2.8) ... Selecting previously unselected package dovecot-imapd. Preparing to unpack .../dovecot-imapd_1%3a2.2.22-1ubuntu2.8_amd64.deb ... Unpacking dovecot-imapd (1:2.2.22-1ubuntu2.8) ... Selecting previously unselected package dovecot-pop3d. Preparing to unpack .../dovecot-pop3d_1%3a2.2.22-1ubuntu2.8_amd64.deb ... Unpacking dovecot-pop3d (1:2.2.22-1ubuntu2.8) ... Processing triggers for man-db (2.7.5-1) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for ufw (0.35-0ubuntu2) ... Processing triggers for systemd (229-4ubuntu21.4) ... Setting up ntpdate (1:4.2.8p4+dfsg-3ubuntu5.9) ... Setting up libexttextcat-data (3.4.4-1ubuntu3) ... Setting up libexttextcat-2.0-0:amd64 (3.4.4-1ubuntu3) ... Setting up dovecot-core (1:2.2.22-1ubuntu2.8) ... Creating config file /etc/dovecot/dovecot.conf with new version Creating config file /etc/dovecot/dovecot-dict-auth.conf.ext with new version Creating config file /etc/dovecot/dovecot-dict-sql.conf.ext with new version Creating config file /etc/dovecot/dovecot-sql.conf.ext with new version Creating config file /etc/dovecot/conf.d/10-auth.conf with new version Creating config file /etc/dovecot/conf.d/10-director.conf with new version Creating config file /etc/dovecot/conf.d/10-logging.conf with new version Creating config file /etc/dovecot/conf.d/10-mail.conf with new version Creating config file /etc/dovecot/conf.d/10-master.conf with new version Creating config file /etc/dovecot/conf.d/10-tcpwrapper.conf with new version Creating config file /etc/dovecot/conf.d/15-lda.conf with new version Creating config file /etc/dovecot/conf.d/15-mailboxes.conf with new version Creating config file /etc/dovecot/conf.d/90-acl.conf with new version Creating config file /etc/dovecot/conf.d/90-plugin.conf with new version Creating config file /etc/dovecot/conf.d/90-quota.conf with new version Creating config file /etc/dovecot/conf.d/auth-checkpassword.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-deny.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-dict.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-master.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-passwdfile.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-sql.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-static.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-system.conf.ext with new version Creating config file /etc/dovecot/conf.d/auth-vpopmail.conf.ext with new version Setting up dovecot-imapd (1:2.2.22-1ubuntu2.8) ... Creating config file /etc/dovecot/conf.d/20-imap.conf with new version Setting up dovecot-pop3d (1:2.2.22-1ubuntu2.8) ... Creating config file /etc/dovecot/conf.d/20-pop3.conf with new version Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for systemd (229-4ubuntu21.4) ... Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for ufw (0.35-0ubuntu2) ... Processing triggers for dovecot-core (1:2.2.22-1ubuntu2.8) ...
/etc/dovecot/dovecot.confと/etc/dovecot/conf.d/下のいくつかのconfを修正する。 SSLに対応したいが、とりあえずそれは後回しにして設定した。 大まかにはこちらを参考にした。
https://qiita.com/mizuki_takahashi/items/1b33e1f679359827c17d
dovecotを再起動。
$ sudo systemctl restart dovecot.service
今の時点でなんのプロセスが何番のポート番号を使っているか確認してみる。 lsof -i
を打つ。
smtp, ntp, pop3, imap2が新たに起動している。
mailコマンドで自分宛にしてみる。
外部からimapで接続できるか試してみる。 ファイアウォールのポート143を開ける。SMTPの開けておかないとThunberbirdでアカウントの設定ができないので25も開ける。
$ sudo iptables -I INPUT 7 -p tcp -m tcp --dport 143 -j ACCEPT $ sudo iptables -I INPUT 7 -p tcp -m tcp --dport 25 -j ACCEPT $ sudo iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG 2 DROP tcp -- anywhere anywhere tcp flags:!FIN,SYN,RST,ACK/SYN state NEW 3 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE 4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 5 ACCEPT icmp -- anywhere anywhere 6 ACCEPT all -- anywhere anywhere 7 ACCEPT tcp -- anywhere anywhere tcp dpt:smtp 8 ACCEPT tcp -- anywhere anywhere tcp dpt:imap2 9 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:xxxxx 10 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination
しかし143は空いたが、25は外部からアクセスできない。設定が悪いというわけでもなさそうなので、おそらくOP25B対策としてさくら側かどこかでブロックされているのではと判断。なのでSubmissionポートの設定をする。
/etc/postfix/master.cfを編集してpostfixを再起動。
ファイアウォールを再設定。
$ sudo iptables -D INPUT 7 $ sudo iptables -I INPUT 7 -p tcp -m tcp --dport 587 -j ACCEPT $ sudo iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG 2 DROP tcp -- anywhere anywhere tcp flags:!FIN,SYN,RST,ACK/SYN state NEW 3 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE 4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 5 ACCEPT icmp -- anywhere anywhere 6 ACCEPT all -- anywhere anywhere 7 ACCEPT tcp -- anywhere anywhere tcp dpt:submission 8 ACCEPT tcp -- anywhere anywhere tcp dpt:imap2 9 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:xxxxx 10 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination
とりあえずこれで外部からimapでメール受信できるようになった。
さくらの情報を見ると、GRUBの設定に少しカスタマイズが入っているらしいので、/etc/default/grubをなるべく元に戻した。
SSL有効化
ここまでで送受信はできるようになったが、平文でやりとりしてしまっているのでSSLを有効にする。というかsmtps, ipop3s, imapsのみを有効にする。
ちゃんとした証明書を取りたいが、後で取るとしてひとまずオレオレ証明書を作って置いた。 場所はpostfixのデフォルトと同じ場所にしておいた。ファイルのパーミッションもデフォルトのファイルと同じにしておいた。
(これはデフォルトの例だけど、この辺をいじった) smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
postfixのmain.cfに設定を入れて、master.cfのsmptdを有効にする。動作確認ができればSubmission portは塞いでしまう。
ssl_protocols = !SSLv2
dovectの設定を入れて、pop3sとimapsを有効にする。pop3とimapは塞いでしまう。
ファイアウォールを開ける。まずimaps。
$ sudo iptables -I INPUT 9 -p tcp -m tcp --dport 993 -j ACCEPT $ sudo iptables -D INPUT 10 $ sudo iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG 2 DROP tcp -- anywhere anywhere tcp flags:!FIN,SYN,RST,ACK/SYN state NEW 3 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE 4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 5 ACCEPT icmp -- anywhere anywhere 6 ACCEPT all -- anywhere anywhere 7 ACCEPT tcp -- anywhere anywhere tcp dpt:smtp 8 ACCEPT tcp -- anywhere anywhere tcp dpt:submission 9 ACCEPT tcp -- anywhere anywhere tcp dpt:imaps 10 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:xxxxx 11 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination
imapsでメールクライアントから接続確認してみる。証明書はオレオレなので当然そこは警告が出る。パスワードは変わらない。
次はsmtps。なんかよくわからないけど、ポート番号465は、ポートの名称としてはurdって表示されるみたいだ。
$ sudo iptables -D INPUT 8 $ sudo iptables -I INPUT 8 -p tcp -m tcp --dport 465 -j ACCEPT $ sudo iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG 2 DROP tcp -- anywhere anywhere tcp flags:!FIN,SYN,RST,ACK/SYN state NEW 3 DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE 4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 5 ACCEPT icmp -- anywhere anywhere 6 ACCEPT all -- anywhere anywhere 7 ACCEPT tcp -- anywhere anywhere tcp dpt:smtp 8 ACCEPT tcp -- anywhere anywhere tcp dpt:urd 9 ACCEPT tcp -- anywhere anywhere tcp dpt:imaps 10 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:xxxxx 11 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination
これでsmtpsで送信できるようになった……、と思いきや、送信できていない。 正確に言うと、送った先(Gmail)を見てみると、迷惑メール扱いになっていた。 最初にpostfixを構築してすぐの状態でメールを送信した時には成功していたので、何か違いがあるはず。 思い当たるのはhostnameを変えたこと。 さくらVPSでサーバを構築した時のデフォルトのhostnameでは送信に成功して、hostnameを変えた後は失敗していることを考えると、おそらくDNS逆引きがうまくできていないから相手先メールサーバで怪しいと判定されているのでは。あるいはSPFレコードも関係するかもしれない。
ということはまずちゃんとDNSを設定するのが先。 ここのサイトが参考になった。
(参考にしたURLがメモってなかった……)
さくらVPSの管理画面から「ネームサーバ登録」を選ぶと「ネームサーバの利用申請」のページへ行ける。そこの「新しいネームサーバの登録」に自分のドメイン名を入力して登録する。するとゾーンの編集ができるようになるので、サポートページなどを参考に正しく入力する。
https://help.sakura.ad.jp/hc/ja/articles/206207381
こちらもすごく参考になる。
https://server-setting.info/blog/smtp-sakuravps-presetting.html
SPFレコードについてはここも参考にした。
https://sakuravps.hajimete-guide.com/entry/do_not_spam
IPAの記事もある。
https://www.ipa.go.jp/security/topics/20120523_spf.html
正しくゾーンの編集ができたら、自分が契約しているレジストラのページに行き、DNSの設定をさくらのネームサーバを指すように変更する。TTLは普通86400とかになっていると思うので、反映されるのは1日以上かかる。
反映されたら、今度は逆引きの設定を行う。さくらのVPSコントロールパネルの各種設定から「ホスト名逆引き登録」を選び、IPアドレスと紐付けるホスト名を入力する。これは事前に正引きがきちんとできる状態でないと登録できないらしい。
いろいろ設定が終わったら、上記のIPAの記事にあったport25.comというサービスあてにメールを送ってみた。 すると"Authentication Report"というメールが返ってきてチェックの結果を教えてくれる。
Thank you for using the verifier, The Port25 Solutions, Inc. team ========================================================== Summary of Results ========================================================== SPF check: pass "iprev" check: pass DKIM check: none SpamAssassin check: ham
うん。いいみたいです。SpamAssassinのチェックが良くないのはBodyが空のメールを送ったからだと思われるのでそこは気にしなくていい。