Xserver VPS メール設定(Postfix) - VPS

DNS レコードの設定

Linux のユーザー名が「 user 」、サーバーの IP アドレスが「 xxx.xxx.xxx.xxx 」、ホスト名が「 {hostname} 」、独自ドメインが「 example.com 」の場合で DNS レコードが以下の表のように設定されていた場合を前提にしています。

ホスト名 種別 内容 TTL 優先度
example.com TXT v=spf1 +a:s{hostname}.xserver.jp +a:example.com +mx include:spf.sender.xserver.jp ~all 3600 0
mail.example.com A xxx.xxx.xxx.xxx 3600 0
example.com MX mail.example.com 3600 0

Postfix

インストール

sudo dnf -y install postfix

ファイアウォールの設定を変更します。

sudo firewall-cmd --permanent --add-service=smtp --zone=public

postfix の設定ファイルである「 main.cf 」のファイルをコピーしておきます。

sudo cp -p /etc/postfix/main.cf /etc/postfix/main.cf.org

設定の変更

「 example.com 」の部分を独自ドメインに変更してください。

コピーしたファイルを変更していきます。

sudo vi /etc/postfix/main.cf

「 myhostname 」を次のように変更します。

myhostname = mail.example.com

「 mydomain 」を次のように変更します。

mydomain = example.com
masquerade_domains = example.com

「 myorigin 」を次のように変更します。

myorigin = $mydomain

「 inet_interfaces 」を次のように変更します。

inet_interfaces = all

「 mydestination 」を次のように変更します。

この設定で「 $mydomain 」を設定しておかないと、メールが送信できず、「 mail loops back to myself 」といったエラーメッセージが出る可能性が高いです。

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

「 local_recipient_maps 」を次のように変更します。

local_recipient_maps = unix:passwd.byname $alias_maps # コメントを外す

「 home_mailbox 」を次のように変更します。

home_mailbox = Maildir/ # コメントを外す

「 smtpd_banner 」を次のように変更します。

smtpd_banner = $myhostname ESMTP # 追記

SMTP-AUTH 用の設定パラメータは、main.cf の最後に追記します。

### SMTP-AUTH
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination

サブミッションポート設定

サブミッションポートの変更は「 master.cf 」を変更します。

オリジナルファイルをコピーしておきます。

 cp -p /etc/postfix/master.cf /etc/postfix/master.cf.org

ファイルを開いて変更します。

sudo vi /etc/postfix/master.cf

次のように変更します。

「 submission inet ... 」の行のコメントを外します。

# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (no)    (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
submission inet n       -       n       -       -       smtpd # コメントを外す

設定の反映

デフォルトの設定値と異なる項目を表示するには次のようにします。

postconf -n

ファイルに誤りがないかコマンドでチェックするには以下のようにします。

sudo postfix check

何も表示されなければファイルには問題はないです。

ファイアウォールの設定を変更します。

firewall-cmd --zone=public --add-service={smtp,smtp-submission} --permanent

次のコマンドを実行し、ファイアウォールの設定を反映させます。

sudo firewall-cmd --reload

セキュリティ対策

Postfix のバージョンを非表示にするするには以下のようにします。

/etc/postfix/main.cf

smtpd_banner = $myhostname ESMTP

メールソフトの設定

メールアドレス user@example.com
受信サーバー POP3 または IMAP
受信サーバーのホスト名 mail.example.com
受信サーバーのポート番号 POP3(110), IMAP(143)
送信サーバー SMTP
送信サーバーのホスト名 mail.example.com
送信サーバーのポート番号 25, 587(サブミッションポート)
ユーザー名 user(Linux のユーザー名)
パスワード user(Linux のユーザー名) のパスワード