- Ubuntu 20 .04にPostfixをインストールして設定 する方法
- eo光環境でPostfixによるOP25B対策
- PostfixでSTARTTLSを有効にしてみる
- Postfixのセキュリティ対策
- Postfixによる、セキュリティに配慮したメールサーバの構築方法
Postfixをインストール
sudo apt install postfix sasl2-bin
設定チュートリアル
- メール設定の一般的なタイプ:** Internet Site**
- システムメール名: example.com(mail.example.comではない)
- rootおよびpostmasterメール受信者: プライマリLinuxアカウントユーザー名
- メールを受信する他の宛先: $myhostname, example.com、mail.example.com、localhost.example.com、localhost
- メールキューの同期更新を強制しますか?:** No**
- ローカルネットワーク: 127.0.0/8 [:ffff:127.0.0.0]/104 [:1]/128
- メールボックスのサイズ制限: 0
- ローカルアドレス拡張文字: +
- 使用するインターネットプロトコル:ALL
設定チュートリアルを再実行する場合
sudo dpkg-reconfigure postfix
main.cfの設定
sudo nano /etc/postfix/main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
#smtpd_banner = $myhostname ESMTP
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2
# TLS parameters
smtpd_use_tls = yes
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_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination
# SASL Setting
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
broken_sasl_auth_clients = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
smtpd_tls_session_cache_timeout = 3600s
smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level = may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/isp_auth
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = login plain
myhostname = mail.non-pro.net
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, mail.example.com, example.com, raspberrypi, localhost.localdomain, localho>
relayhost = [smtpauth.eonet.ne.jp]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
home_mailbox = Maildir/
# MTA情報の隠蔽
smtpd_banner = ESMTP MTA
# 内部ユーザの隠蔽
disable_vrfy_command = yes
# HELO コマンドを必須にする
smtpd_helo_required = yes
#相手のホスト名が DNS に登録されていない、FQDNが解決できない、ホスト名の文法が不正の場合に拒否
smtpd_helo_restrictions = permit_mynetworks, reject_unknown_hostname, reject_non_fqdn_hostname, reject_invalid_hostname, permit
#ドメインが解決できない、FQDNが解決できない場合に拒否
smtpd_sender_restrictions = permit_mynetworks, reject_unknown_sender_domain, reject_non_fqdn_sender
sudo nano /etc/postfix/master.cf
smtp inet n - n - - smtpd
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
パスワードファイルを作成
sudo nano /etc/postfix/isp_auth
[smtpauth.eonet.ne.jp]:587 プロバイダのSMTPアカウント:パスワード
権限の変更とマッピング
sudo chmod 600 /etc/postfix/isp_auth
sudo postmap /etc/postfix/isp_auth
設定値の確認と再起動
sudo postconf [-d:デフォルト値][-n:デフォルトと異なる値]
sudo systemctl restart postfix
ファイアーウォールの設定
sudo ufw allow Postfix
sudo ufw allow 'Postix SMTPS'
アカウントの設定
メール用のディレクトリが作成されるようにする。
sudo mkdir -p /etc/skel/Maildir/{new,cur,tmp}
sudo chmod -R 700 /etc/skel/Maildir/
ユーザ作成
sudo useradd -m new_user
sudo useradd -sm /sbin/nologin new_user
パスワードの設定
sudo passwd new_user
ユーザの削除
sudo userdel -r new_user
saslの追加(dovecot saslを使用していない場合)
sudo saslpasswd2 -u example.com -c new_user
sudo sasldblistusers2 # ユーザが追加されているか確認
Dovecot
インストール
sudo apt install dovecot-core dovecot-imapd ovecot-pop3d
dovecot.confの設定
sudo nano /etc/dovecot/dovecot.conf
listen = *, ::
10-master.confの設定
sudo nano /etc/dovecot/conf.d/10-master.conf
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
10-mail.confの設定
sudo nano /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
10-auth.confの設定
sudo nano /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
10-ssl.confの設定
sudo nano /etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem
再起動
sudo systemctl restart dovecot
telnetでメールの送信テスト
# telnet localhost 25
HELO localhost
MAIL FROM:<hoge@localhost> ←ユーザーネームを置き換える
RCPT TO:<huga@example.com> ←相手のメールアドレス
DATA
Subject:Send Test Mail
From:hogehoge<hoge@localhost> ←ユーザー名とユーザーネームを置き換える
To:hoge<hogehoge@gmail.com> ←相手のメールアドレス(実際に利用しているメールアドレス)
This is test mail ←ここから本文
from telnet
. ←このドットにも意味があるので注意