postfix実践入門
#2022.02 #mail
https://gihyo.jp/assets/images/cover/2010/9784774143750.jpg
書籍に従って、vagrantにメールサーバーを立ててテスト送信してみた時の作業メモ
準備
任意のディレクトリでvagrantのボックスを作成する
vagrant init centos/7
Vagrantfileの編集
code:txt
config.vm.network "private_network", ip: "192.168.56.11"
config.vm.hostname = "mail.server-test.info"
Vagrant ゲストOSのWebサーバーにホスト名でアクセスする
vagrant up
エラーが出た
Virtual boxをとりあえずアプデする
結局macの設定でアクセス許可がなかったぽくて、macの設定から変更
Vagrantfileで設定したIPがまずかったらしく、再度エラーが出たのでIPを修正
https://stackoverflow.com/questions/69728426/e-accessdenied-when-creating-a-host-only-interface-on-virtualbox-via-vagrant
Weirdly enough I can change the IPv4 address of the adapter (either via VBoxManage or the GUI) but only between 192.168.56.1 and 192.168.63.254. anything outside this range fails.
code:error.bash
// macの設定が原因のエラー
~/p/vm ❯❯❯ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
〜〜省略
Stderr: VBoxManage: error: The virtual machine 'vm_default_1645241252446_64728' has terminated unexpectedly during startup with exit code 1 (0x1)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
// IPアドレスが原因のエラー
~/p/vm ❯❯❯
~/p/vm ❯❯❯ vim practice/vm/Vagrantfile
config.vm.network "private_network", ip: "192.168.33.10" # コメントアウトしてあったのを戻して使ったらこれがダメだった
~/p/vm ❯❯❯ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Clearing any previously set network interfaces...
There was an error while executing VBoxManage, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: "hostonlyif", "ipconfig", "vboxnet1", "--ip", "192.168.33.1", "--netmask", "255.255.255.0"
Stderr: VBoxManage: error: Code E_ACCESSDENIED (0x80070005) - Access denied (extended info not available)
VBoxManage: error: Context: "EnableStaticIPConfig(Bstr(pszIp).raw(), Bstr(pszNetmask).raw())" at line 242 of file VBoxManageHostonly.cpp
~/p/vm ❯❯❯
無事にvagrant upができた🎉
postfixのインストール
vagrant ssh してサーバー内で作業していく
書籍だと、sendmailを無効化してpostfixを入れるという手順になってたけど、Cent7は標準でpostfix入ってた
yum check-update したけどアップデートはなさそうだった
code:bash
root@mail vagrant# yum info postfix
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cat.net
* extras: mirrors.cat.net
* updates: mirrors.cat.net
Installed Packages
Name : postfix
Arch : x86_64
Epoch : 2
Version : 2.10.1
Release : 9.el7
Size : 12 M
Repo : installed
From repo : anaconda
Summary : Postfix Mail Transport Agent
URL : http://www.postfix.org
License : IBM and GPLv2+
Description : Postfix is a Mail Transport Agent (MTA), supporting LDAP, SMTP AUTH (SASL),
: TLS
root@mail vagrant#
sendmail入ってないみたい。postfixしか入ってなかったら切り替える作業が不要だった。
code:bash
root@mail vagrant# alternatives --config mta
There is 1 program that provides 'mta'.
Selection Command
-----------------------------------------------
*+ 1 /usr/sbin/sendmail.postfix
Enter to keep the current selection+, or type selection number:
root@mail vagrant#
root@mail vagrant# alternatives --display mta
mta - status is manual.
link currently points to /usr/sbin/sendmail.postfix
/usr/sbin/sendmail.postfix - priority 30
slave mta-mailq: /usr/bin/mailq.postfix
slave mta-newaliases: /usr/bin/newaliases.postfix
slave mta-pam: /etc/pam.d/smtp.postfix
slave mta-rmail: /usr/bin/rmail.postfix
slave mta-sendmail: /usr/lib/sendmail.postfix
slave mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
slave mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
slave mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
slave mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
Current `best' version is /usr/sbin/sendmail.postfix.
root@mail vagrant#
postfix起動してバージョンの確認
code:bash
root@mail vagrant# service postfix start
Redirecting to /bin/systemctl start postfix.service
root@mail vagrant#
root@mail vagrant# postconf mail_version
mail_version = 2.10.1
root@mail vagrant#
postfixのステータス
code:bash
root@mail vagrant# postfix status
postfix/postfix-script: the Postfix mail system is running: PID: 869
root@mail vagrant#
postfixのコマンドの説明
postalias
postfixが利用する、エイリアス・ルックアップテーブルの作成や変更
http://www.postfix-jp.info/trans-2.1/jhtml/postalias.1.html
標準で以下の設定
code:bash
root@mail vagrant# postconf -d | grep alias_database
alias_database = hash:/etc/aliases
root@mail vagrant#
postconf
postfixの設定。全部見るとめちゃ長い
(例)受信メールの最大サイズ
code:bash
root@mail vagrant# postconf -d | wc -l
816
root@mail vagrant#
root@mail vagrant# postconf -d | grep message_size_limit
message_size_limit = 10240000
root@mail vagrant#
(例)main.cfを書き換えるとき
code:bash
postconf -e 'myorigin = $myhostname'
postconf -n とすると、デフォルト値のままになっていないパラメータだけ(=変えたとこだけ?)を表示できる
http://www.postfix-jp.info/trans-2.2/jhtml/postconf.1.html
-n main.cf で明示的に指定されていて、組み込まれたデフォルト値のまま残っていないパラメータ設定を表示します。
postcat
メールキューにあるファイルを参照できる
code:bash
postcat -q キューID
# 送信できなかったキューが残ってるとこんな感じ
root@mail vagrant# postcat -q 5C2F040A3039
*** ENVELOPE RECORDS deferred/5/5C2F040A3039 ***
message_size: 382 633 1 0 382
message_arrival_time: Sat Feb 19 05:55:18 2022
create_time: Sat Feb 19 05:55:36 2022
named_attribute: log_ident=5C2F040A3039
named_attribute: rewrite_context=local
sender: user@server-test.info
named_attribute: log_client_name=localhost
named_attribute: log_client_address=IPv6:::1
named_attribute: log_client_port=53862
named_attribute: log_message_origin=localhost::1
named_attribute: log_helo_name=mail.server-test.info
named_attribute: log_protocol_name=ESMTP
named_attribute: client_name=localhost
named_attribute: reverse_client_name=localhost
named_attribute: client_address=::1
named_attribute: client_port=53862
named_attribute: helo_name=mail.server-test.info
named_attribute: protocol_name=ESMTP
named_attribute: client_address_type=10
named_attribute: dsn_orig_rcpt=rfc822;root@server-test.info
original_recipient: root@server-test.info
recipient: root@server-test.info
*** MESSAGE CONTENTS deferred/5/5C2F040A3039 ***
Received: from mail.server-test.info (localhost IPv6:::1)
by mail.server-test.info (Postfix) with ESMTP id 5C2F040A3039
for <root@server-test.info>; Sat, 19 Feb 2022 05:55:18 +0000 (UTC)
Message-Id: <20220219055536.5C2F040A3039@mail.server-test.info>
Date: Sat, 19 Feb 2022 05:55:18 +0000 (UTC)
From: user@server-test.info
this is my message
How are you???
Good Bye!
*** HEADER EXTRACTED deferred/5/5C2F040A3039 ***
*** MESSAGE FILE END deferred/5/5C2F040A3039 ***
root@mail vagrant#
postqueue
メールキューリストの表示、キューのフラッシュを行う
code:bash
root@mail vagrant# postsuper -d 5C2F040A3039
postsuper: 5C2F040A3039: removed
postsuper: Deleted: 1 message
root@mail vagrant#
postsuper
キューtmpファイルの削除、キューの移動、キューないのメールの削除など(実行にroot権限が必要)
chkconfigで自動起動の設定をする
code:bash
root@mail vagrant# chkconfig postfix on
Note: Forwarding request to 'systemctl enable postfix.service'.
root@mail vagrant#
確認だけでいい場合はchkconfig --list postfix
ホストOSからtelnetでゲストOSの25番ポートに接続したいがつながらない
ローカルから、vagrantのIPでtelnet接続しようとしたらダメだった
code:bash
~/p/vm ❯❯❯ telnet 192.168.56.11 25
Trying 192.168.56.1...
telnet: connect to address 192.168.56.1: Connection refused
telnet: Unable to connect to remote host
~/p/vm ❯❯❯
pingはとおる
code:bash
~/p/vm ❯❯❯ ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1): 56 data bytes
64 bytes from 192.168.56.1: icmp_seq=0 ttl=64 time=1.428 ms
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.121 ms
64 bytes from 192.168.56.1: icmp_seq=3 ttl=64 time=0.104 ms
64 bytes from 192.168.56.1: icmp_seq=4 ttl=64 time=0.111 ms
^C
--- 192.168.56.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.084/0.370/1.428/0.529 ms
~/p/vm ❯❯❯
この辺見て、ネットワーク調査したけどあっていそうだった。ホストオンリーアダプタで動作してればOKだと思う。
せっかくなので、ネットワークの調査方法とか、vagrantのネットワーク周りの設定とかのブログを漁った
https://portaltan.hatenablog.com/entry/2015/10/13/163427
ゲストOSのifconfigでVagrantfileで設定したIPがついてることを確認
pingだとポート指定できないので、nmapで試したけどnmap通らなかった
ss -lnptしてみたけど、25ポートはLISTENになっていた
https://qiita.com/centipede/items/64e8f7360d2086f4764f
https://yoshinorin.net/2016/08/07/local-mailserver/
ifconfigの出力結果に書いてあること
結局、postfix側の設定だった。本に書いてあった設定、あんまり大事じゃないと思って飛ばしてたら、まさにそこに書いてあったやつ。先に以下の手順をやればよかっただけだった。
inet_interfaces = all の設定が必要
https://server-setting.info/centos/postfix-relay-error-point.html
postfixがローカル配送のみ許可してて、外部からの接続を拒否してた
デフォルトがinet_interfaces = localhost
postfixの設定
設定ファイルコピーしてバックアップとっておく
cp /etc/postfix/main.cf /etc/postfix/main.cf.org
使用するhostnameのチェック
code:bash
root@mail postfix# hostname -f
mail.server-test.info
root@mail postfix#
設定で変更した箇所 vim /etc/postfix/main.cf
code:txt
myhostname = mail.server-test.info
mydomain = server-test.info
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 192.168.56.0/28, 127.0.0.0/8
relay_domains = $mydestination
relayhost =
home_mailbox = Maildir/
最後にpostfixをリスタート service postfix restart
メールを使うユーザーを用意
適当に2つくらい作る
code:bash
root@mail vagrant# useradd user1
root@mail vagrant# useradd user2
root@mail vagrant#
ホストOSからメールの送信
telnet ホストIP ポートでプロトコルをしゃべる準備ができる
code:bash
~ ❯❯❯ telnet 192.168.56.11 25
Trying 192.168.56.11...
Connected to 192.168.56.11.
Escape character is '^]'.
220 mail.server-test.info ESMTP Postfix
EHLO mail.server-test.info # コマンドは大文字でも小文字でもOK(ここから先は小文字)
250-mail.server-test.info
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:<user1@server-test.info>
250 2.1.0 Ok
rcpt to:<user2@server-test.info>
250 2.1.5 Ok
rcpt to:<vagrant@server-test.info> # 複数の宛先に送るときは、複数回rcpt toコマンドを送る
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
this is my test mail.
from user1 to user2 and vagrant!
Good bye!!!
. # メール本文の終わりはドットだけで改行する
250 2.0.0 Ok: queued as AEDAB40A303B
quit # もし接続を維持する場合はNOOPコマンドを送ればOK
221 2.0.0 Bye
Connection closed by foreign host.
~ ❯❯❯
受信メールの確認
メールサーバーに入って、ファイルの中身をcatで確認する
code:bash
root@mail vagrant# cat /home/user2/Maildir/new/1645254509.V801I6001aeaM998646.mail.server-test.info
Return-Path: <user1@server-test.info>
X-Original-To: user2@server-test.info
Delivered-To: user2@server-test.info
Received: from mail.server-test.info (unknown 192.168.56.1)
by mail.server-test.info (Postfix) with ESMTP id AEDAB40A303B;
Sat, 19 Feb 2022 07:05:56 +0000 (UTC)
this is my test mail.
from user1 to user2 and vagrant!
Good bye!!!
root@mail vagrant#
root@mail vagrant#
root@mail vagrant#
root@mail vagrant# cat /home/vagrant/Maildir/new/1645254510.V801I2013b05M7796.mail.server-test.info
Return-Path: <user1@server-test.info>
X-Original-To: vagrant@server-test.info
Delivered-To: vagrant@server-test.info
Received: from mail.server-test.info (unknown 192.168.56.1)
by mail.server-test.info (Postfix) with ESMTP id AEDAB40A303B;
Sat, 19 Feb 2022 07:05:56 +0000 (UTC)
this is my test mail.
from user1 to user2 and vagrant!
Good bye!!!
root@mail vagrant#
maillogの確認
メールサーバーの/var/log/maillogを見てみるとpostfixのログがある
code:txt
root@mail vagrant# cat /var/log/maillog
Feb 19 07:05:25 localhost postfix/smtpd3775: connect from unknown192.168.56.1
Feb 19 07:06:59 localhost postfix/smtpd3775: AEDAB40A303B: client=unknown192.168.56.1
Feb 19 07:08:29 localhost postfix/cleanup3778: AEDAB40A303B: message-id=<>
Feb 19 07:08:29 localhost postfix/qmgr3718: AEDAB40A303B: from=<user1@server-test.info>, size=242, nrcpt=2 (queue active)
Feb 19 07:08:30 localhost postfix/local3779: AEDAB40A303B: to=<user2@server-test.info>, relay=local, delay=153, delays=153/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Feb 19 07:08:30 localhost postfix/local3780: AEDAB40A303B: to=<vagrant@server-test.info>, relay=local, delay=153, delays=153/0.02/0/0, dsn=2.0.0, status=sent (delivered to maildir)
Feb 19 07:08:30 localhost postfix/qmgr3718: AEDAB40A303B: removed
Feb 19 07:08:44 localhost postfix/smtpd3775: disconnect from unknown192.168.56.1
root@mail vagrant#
Dovecotの設定
POPやIMAPを使うためにDovecotを設定していく
dovecotインストール
yum -y install dovecot
dovecot.confの設定
code:txt
vi /etc/dovecot/dovecot.conf
# 以下編集内容
protocols = imap pop3 lmtp
listen = *
vi /etc/dovecot/conf.d/10-mail.conf
# 以下編集内容
mail_location = maildir:~/Maildir
vi /etc/dovecot/conf.d/10-auth.conf
# 以下編集内容
disable_plaintext_auth = no # 一旦平文で認証させる
auth_mechanisms = plain login
vi /etc/dovecot/conf.d/10-ssl.conf
# 以下編集内容
ssl = no
dovecotの起動
service dovecot start
ポートの確認
code:bash
root@mail ~# netstat -ln | grep -e 143 -e 110
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
unix 2 ACC SEQPACKET LISTENING 11014 /run/udev/control
root@mail ~#
ホストOSからtelnetでIMAP接続してメール確認(ポート143)
code:bash
~ ❯❯❯ telnet 192.168.56.11 143
Trying 192.168.56.11...
Connected to 192.168.56.11.
Escape character is '^]'.
* OK CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN Dovecot ready.
1 login user2 password
1 OK CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY SPECIAL-USE Logged in
2 list "" *
* LIST (\HasNoChildren) "." INBOX
2 OK List completed (0.001 + 0.000 secs).
3 select INBOX
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*) Flags permitted.
* 2 EXISTS
* 2 RECENT
* OK UNSEEN 1 First unseen.
* OK UIDVALIDITY 1645261892 UIDs valid
* OK UIDNEXT 3 Predicted next UID
3 OK READ-WRITE Select completed (0.005 + 0.000 + 0.004 secs).
4 fetch 1 body[]
* 1 FETCH (FLAGS (\Seen \Recent) BODY[] {358}
Return-Path: <user1@server-test.info>
X-Original-To: user2@server-test.info
Delivered-To: user2@server-test.info
Received: from mail.server-test.info (unknown 192.168.56.1)
by mail.server-test.info (Postfix) with ESMTP id AEDAB40A303B;
Sat, 19 Feb 2022 07:05:56 +0000 (UTC)
this is my test mail.
from user1 to user2 and vagrant!
Good bye!!!
)
4 OK Fetch completed (0.001 + 0.000 secs).
5 logout
* BYE Logging out
5 OK Logout completed (0.001 + 0.000 secs).
Connection closed by foreign host.
~ ❯❯❯
ホストOSからtelnetでPOP接続してメール確認(ポート110)
code:bash
~ ❯❯❯ telnet 192.168.56.11 143
Trying 192.168.56.11...
Connected to 192.168.56.11.
Escape character is '^]'.
* OK CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN Dovecot ready.
USER user2
USER BAD Error in IMAP command received by server.
^CConnection closed by foreign host.
~ ❯❯❯ telnet 192.168.56.11 110
Trying 192.168.56.11...
Connected to 192.168.56.11.
Escape character is '^]'.
+OK Dovecot ready.
USER user2
+OK
PASS password
+OK Logged in.
LIST
+OK 2 messages:
1 358
2 328
.
TOP 1
-ERR Noise after message number:
TOP 2
-ERR Noise after message number:
RETR 2
+OK 328 octets
Return-Path: <user1@server-test.info>
X-Original-To: user2@server-test.info
Delivered-To: user2@server-test.info
Received: from mail.server-test.info (unknown 192.168.56.1)
by mail.server-test.info (Postfix) with ESMTP id 362C0400A4C7
for <user2@server-test.info>; Sat, 19 Feb 2022 08:37:11 +0000 (UTC)
teeeeest!!!
.
RETR 1
+OK 358 octets
Return-Path: <user1@server-test.info>
X-Original-To: user2@server-test.info
Delivered-To: user2@server-test.info
Received: from mail.server-test.info (unknown 192.168.56.1)
by mail.server-test.info (Postfix) with ESMTP id AEDAB40A303B;
Sat, 19 Feb 2022 07:05:56 +0000 (UTC)
this is my test mail.
from user1 to user2 and vagrant!
Good bye!!!
.
QUIT
+OK Logging out.
Connection closed by foreign host.
~ ❯❯❯
dovecotのログの確認
メールサーバーの/var/log/dovecot.logを確認する
imapとpopの接続ログがそれぞれ確認できる
code:bash
root@mail ~# cat /var/log/dovecot.log
Feb 19 09:07:20 master: Info: Dovecot v2.2.36 (1f10bfa63) starting up for imap, pop3, lmtp (core dumps disabled)
Feb 19 09:11:20 imap-login: Info: Login: user=<user2>, method=PLAIN, rip=192.168.56.1, lip=192.168.56.11, mpid=4321, session=<8qz5YVvYt8/AqDgB>
Feb 19 09:11:48 imap(user2): Info: Logged out in=55 out=1298
Feb 19 09:16:03 imap-login: Info: Disconnected (no auth attempts in 0 secs): user=<>, rip=192.168.56.1, lip=192.168.56.11, session=<02bVclvY48/AqDgB>
Feb 19 09:18:26 master: Warning: Killed with signal 15 (by pid=4332 uid=0 code=kill)
Feb 19 09:18:26 imap-login: Warning: Killed with signal 15 (by pid=1 uid=0 code=kill)
Feb 19 09:18:26 imap-login: Info: Disconnected: Shutting down (no auth attempts in 53 secs): user=<>, rip=192.168.56.1, lip=192.168.56.11, session=<UoBZe1vY6s/AqDgB>
Feb 19 09:18:26 master: Info: Dovecot v2.2.36 (1f10bfa63) starting up for imap, pop3, lmtp (core dumps disabled)
Feb 19 09:19:06 pop3-login: Info: Login: user=<user2>, method=PLAIN, rip=192.168.56.1, lip=192.168.56.11, mpid=4358, session=<4vu1fVvY7c/AqDgB>
Feb 19 09:20:20 pop3(user2): Info: Disconnected: Logged out top=0/0, retr=2/718, del=0/2, size=686
Feb 19 09:56:44 master: Warning: Killed with signal 15 (by pid=4408 uid=0 code=kill)
Feb 19 09:56:44 master: Info: Dovecot v2.2.36 (1f10bfa63) starting up for imap, pop3, lmtp (core dumps disabled)
root@mail ~#
実はこの後、書籍にならって証明書をインストールしてpop3s、imapsも設定しようとしてみたけどルート証明書のエラーが出てしまった
ローカルでもオレオレ証明書ダメなのかな?
これを読んで試してみる
https://hrkworks.com/it/linux/server/mail02/