概要
AmazonLinux2のEC2にメールサーバを立てたのでメモ。
セットアップ
セキュリティグループの設定
SMTP(25):0.0.0.0/0
IMAPS(993):0.0.0.0/0
POP3(110):0.0.0.0/0
SMTPS(465):0.0.0.0/0
POP3S(995):0.0.0.0/0
Postfixインストール
インストール
$ sudo yum -y install postfix
起動確認
$ systemctl status postfix
postfixの自動起動の確認
$ systemctl is-enabled postfix
/etc/postfix/main.cf を設定
以下はコメントアウト
# mydestination = $myhostname, localhost.$mydomain, localhost
# inet_interfaces = localhost
#inet_protocols = all
inet_protocols = ipv4
以下を追加
# add
myhostname = mail.test.site
mydomain = test.site
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
inet_interfaces = all
mynetworks = 10.0.0.0/16, 127.0.0.0/8
relay_domains = $mydestination
home_mailbox = Maildir/
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
smtpd_client_restrictions =
permit_mynetworks,
reject_rbl_client spamcop.net,
reject_rbl_client dynablock.wirehub.net,
reject_rbl_client opm.blitzed.org,
reject_rbl_client sbl.spamhaus.org,
reject_rbl_client list.dsbl.org,
check_client_access hash:/etc/postfix/reject_client,
reject_unknown_client,
permit
リロードして再起動
$ postfix reload
$ postfix stop
$ postfix start
これで接続は防げるが、メールログが溢れるので怪しい接続は通信レベルで遮断してやる。
$ iptables -I INPUT -s XXX.XXX.XXX.XXX -j DROP
設定されたかの確認。
$ iptables -L
これで/var/maillogにも出てこなくなるはず。
saslauthdインストール
インストール
$ yum install cyrus-sasl cyrus-sasl-md5
起動と自動起動設定
$ systemctl start saslauthd
$ systemctl is-enabled saslauthd
$ systemctl enable saslauthd
$ systemctl is-enabled saslauthd
MTA設定
設定確認
$ alternatives --display mta
mta - status is auto.
link currently points to /usr/sbin/sendmail.postfix *PostfxであればOK 。Sendmailになってれば変更する。
/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.
Postfixの再起動
$ systemctl restart postfix
メールアカウントの追加
$ useradd hoge
$ passwd hoge
$ su - hoge
$ mkdir Maildir
$ pwd
/home/hoge
$ ll
total 0
drwxrwxr-x 2 cloudpack cloudpack 6 Aug 16 05:02 Maildir
Dovecotインストール
インストール
$ yum -y install dovecot
dovecotの自動起動設定
$ systemctl enable dovecot
$ systemctl is-enabled dovecot
設定
・メールの保存場所をホームディレクトリのMaildirに設定。
・認証方法にloginを追加。
(略)
#以下を追加
mail_location = maildir:~/Maildir
auth_mechanisms = plain login
↓のコメントアウトを外す
protocols = imap pop3 lmtp
(略)
service imap-login {
inet_listener imap {
port = 143
}
inet_listener imaps {
port = 993
ssl = yes
}
(略)
service pop3-login {
inet_listener pop3 {
port = 110
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
(略)
service auth {
(略)
mode = 0666
user = postfix
group = postfix
}
dovecotを起動
$ systemctl start dovecot
$ systemctl status dovecot
送信設定
サブミッションポートを有効にする。
#submission inet n - n - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
を下記のように訂正する。
submission inet n - n - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
リロードして反映する。
postfix reload
unkown hostからの接続を拒否する
SASL認証をかけているので実害はないがログが膨れるので知らないIPからアクセスを拒否する。
ネットワーク接続時点で拒否したいのでiptablesで行う。
まずはインストール。
yum install iptables-services ipset-service
ホワイトリストとなるipsetの作成
sh
ipset create WhiteListFtp hash:net ← ipset作成
ipset add WhiteListFtp xx.xx.xx.xx/32 ← SSH接続しているIPを設定
保存
ipset save > /etc/sysconfig/ipset
拒否するIPを設定ファイルに記述する
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A INPUT -s 185.143.75.0/24 -j DROP
-A INPUT -s 212.70.149.0/24 -j DROP
-A INPUT -s 193.56.28.0/24 -j DROP
-A INPUT -p tcp -j ACCEPT
COMMIT
/etc/sysconfig/ip6tablesはIPV6は使わないので空のままにしておく。
サービスの有効と起動
systemctl enable ipset.service
systemctl start ipset.service
systemctl enable iptables.service
systemctl start iptables.service
systemctl enable ip6tables.service
systemctl start ip6tables.service