Postfixのメール送信が遅れる。IPv4の接続を優先する設定

メールログを見ていて下記エラーが目に留まった時の覚書。

環境: CentOS 7.6, Postfix 2.10.1

postfix/smtp[13310]: connect to gmail-smtp-in.l.google.com[2404:6800:4008:c01::1a]:25: Connection timed out
postfix/smtp[13310]: Trusted TLS connection established to gmail-smtp-in.l.google.com[108.177.125.26]:25: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)

最初のログはIPv6で接続しようとしていてタイムアウトしている。
タイムアウトを待つ分だけメール送信に時間が掛かっていた。

IPv6を無効にするのも何となく不安なので、IPv4から先に試すように設定。
「smtp_address_preference」を使う。Postfix 2.8から使える設定。

Postfixのバージョンを確認
# yum info postfix

postconfを使う方法もある
# postconf -d | grep version

設定ファイルを編集
# less /etc/postfix/main.cf
# Enable IPv4, and IPv6 if supported
inet_protocols = all
# Try IPv4 first
smtp_address_preference = ipv4

testしてサービス再起動
# postfix check
# systemctl restart postfix

ログを監視しながら動作確認
# tail -f /var/log/maillog
# mail hoge@gmail.com


【関連記事】