RockyLinuxでの postfix, dovecot設定ですが、
実際のところは、CentOSからの乗り換え作業です。
ついでにnamedも入れます。
環境
さくらのVPS 1G
説明で使うホスト名等
ホスト名 HSTNM
ドメイン名 EXAMPLE.COM
IPアドレス 123.456.789.012 (255を超えているのは説明上の故意です)
基本設定
swap設定
デフォルトではswap領域がありません。
この状態ではopendkimのインストールができないので、swap領域を作ります。
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
cp -p /etc/fstab /etc/fstab.org
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
一旦リブートします。
reboot
アップデート
最新の状態に更新します。
sudo dnf update
ホスト設定
/etc/hostname
hstnm.example.com
/etc/resolv.conf
search example.com
nameserver DNSサーバのIPアドレス
nameserver DNSサーバのIPアドレス(セカンダリ)
DNS設定
ドメインのDNSサーバに登録して、反映されるのを待ってから、
さくらインターネットのサーバ設定で、IPv4の逆引き設定をします。
ユーザー作成
一般的にはLDAPなりADなりを使うと思いますが、単一サーバで完結させるケースで説明します。
旧環境で情報の取得
getent group > group.txt
getent passwd > passwd.txt
getent shadow > shadow.txt
一般ユーザー分だけ切り出します。
rockyが1000を使っているので注意が必要です。
passwd.txt
foo:x:1001:1001::/home/foo:/bin/bash
bar:x:1002:1002::/home/bar:/sbin/nologin
sudo newusers passwd.txt
ユーザーに一対一に対応するグループは明示的に作らなくても、勝手に作られます。
shadowは、
user:encrypted-password
の形式に置き換えてから、chpasswdで一括設定します。
sudo chpasswd --encrypted < shadow.txt
/etc/groupのwheelにrootになれるユーザーを追記します。
wheel:x:10:rocky,foo
インストール
suでの実行です。
dnf install postfix
dnf install dovecot
dnf config-manager --enable crb
dnf install epel-release
dnf install opendkim opendkim-tools
dnf install bind
postfix設定
/etc/postfix/main.cf
変更点
myhostname = hstnm.example.com
mydomain = example.com
myorigin = $mydomain
#inet_interface = localhost
inet_interface = all
#mydestination = $myhostname, localhost.$mydomain, localhost
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks_style = host
mynetworks = 123.456.789.012, 127.0.0.0/8
relay_domains = $mydestination
recipient_delimiter = _
home_mailbox = Maildir/
mail_spool_directory = /var/spool/mail
header_checks = regexp:/etc/postfix/header_checks
追加
# SASL
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
broken_sasl_auth_clients = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
message_size_limit = 10485760
default_process_limit = 50
# DKIM
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = $smtpd_milters
milter_default_action = accept
/etc/postfix/master.cf
変更点
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
/etc/postfix/header_checks
元サーバからコピーします。
独自設定のため、未掲載です。
dovecot設定
/etc/dovecot/dovecot.conf
変更点
protocols = pop3
/etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain login
!include auth-system.conf.ext
/etc/dovecot/conf.d/10-mail.conf
変更点
mail_location = maildir:~/Maildir
/etc/dovecot/conf.d/10-master.conf
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
SSL設定
さくらインターネット経由で、JRPSの一番安い証明書を使います。
openssl genrsa -aes256 2048 > private.key
openssl req -new -key private.key -out server.csr -sha256
パスフレーズは、適当な長さで設定します。
Countr Name: JP
State or Province Name: 都道府県
Locality Name: 市
Organization Name: 団体名
Organization Unit Name: 省略
Common Name: hstnm.example.com
Email Address: 省略
A challenge password: 省略
An optional company name: 省略
作成される server.csr を提出します。
メール方式にすると、commonnameのサーバにメールが送られるので、あらかじめメール受信が可能なことを確認しておきます。
server.csrと一緒に作成されたprivate.keyを変換して配置します。
openssl rsa -in private.key -out private.pem
/etc/pki/dovecot/private/dovecot.pem として置きます。
受け取った証明書に、中間証明書、ルート証明書をまとめます。
cat server.crt JPRS_DVCA_G4_PEM.cer scrootca2.txt > dovecot.pem
/etc/pki/dovecot/certs/dovecot.pem に置きます。
openssl s_client --connect hstnm.example.com:995 -showcerts
で外部から確認します。
opendkim設定
キー作成
/etc/opendkim/keys/example.com/240116.private にキーを秘密鍵を置きます。
240116は作成日ですが、何でも良いです。
sudo opendkim-genkey -b 2048 -D /etc/opendkim/keys/ -d example.com -s 240116
sudo chown -R opendkim:opendkim /etc/opendkim/keys
240116.txtが同時に作成されます。
これは、DNSの設定に追加します。
なお、キーはドメインと結びついていますが、ホストのIPなどには紐付いていませんので、環境を移行しても流用できます。
設定ファイル
/etc/opendkim.conf
変更点
Mode sv
Socket inet:8891@localhost
#Socket local:/run/opendkim/opendkim.sock
SoftwareHeader no
#KeyFile /etc/opendkim/keys/default.private
KeyTable /etc/opendkim/KeyTable
SigningTable refile:/etc/opendkim/SigningTable
ExternalIgnoreList refile:/etc/opendkim/TrustedHosts
InternalHosts refile:/etc/opendkim/TrustedHosts
/etc/opendkim
/etc/opendkim/KeyTable
末尾に追加
240116._domainkey.example.com example.com:240116:/etc/opendkim/keys/example.com/240116.private
/etc/opendkim/SigningTable
末尾に追加
*@example.com 240116._domainkey.example.com
/etc/opendkim/TrustedHosts
変更なし
まとめて有効化
systemctl enable postfix dovecot opendkim
bind設定
手元の作業メモです。
/etc/named.conf
/var/named
systemctl start named
systemctl enable named