AlmaLinux9のSSH設定

AlmaLinux9のSSHは、一番初めにやらなければならない設定と言える。VPSやクラウドの場合SSHサーバ機能だけは設定されているので、心配はないが正しく設定しておかないと後々面倒となる。特に、設定に失敗するとコンソールからログインするしかなくなるので、一番初めに片づけておこう!

関連記事:Openssh-9.3p2 AlmaLinux8

cd /etc/ssh
cp sshd_config sshd_config.org
vi sshd_config

#       $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
# AlamaLinux9.x
Include /etc/ssh/sshd_config.d/*.conf

# Port 22 # F/Wで制御されている場合は有効でも良い
Port 20022

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

SyslogFacility AUTHPRIV
PermitRootLogin no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no

PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

PasswordAuthentication no
PermitEmptyPasswords no

UsePAM yes

KbdInteractiveAuthentication no
ClientAliveInterval 60
ClientAliveCountMax 3

Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes256-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256

Subsystem      sftp internal-sftp
Match User sftp_user
ChrootDirectory /var/www
X11Forwarding no
AllowTcpForwarding no
PasswordAuthentication yes

# Match User admin_user # パスワードのみでログイン可能のユーザを作らなければならない場合
# PasswordAuthentication yes

※sftp_userアカウントでSFTPによるWebコンテンツなどの管理を行えるようにしています。

AlmaLinux9のSSHを行う場合、AlmaLinux 9 で使用されている OpenSSH_8.7 に対して、**セキュリティの高い設定(Hardening)**を行うには、以下の観点で設定を強化するのが一般的です。

AlmaLinux9のSSH設定 補足

ED25519署名の鍵ペアを作成

user1アカウントに対して「ED25519」で新たな鍵ペアを作成する場合、

su - user1
ssh-keygen -t ed25519
cat .ssh/id_ed25519.pub >> .ssh/authorized_keys

🔒 OpenSSH セキュリティ強化チェックリスト(sshd_config 編)

設定ファイルの場所(通常):

/etc/ssh/sshd_config<

1. プロトコルとバージョン確認

OpenSSH 8.7 は SSHv2 のみサポートされているので特に設定不要ですが、古い環境では以下を確認:

Protocol 2

2. 暗号方式の制限(Ciphers, MACs, KexAlgorithms)

以下は安全とされているアルゴリズムの一例(2025年現在でも妥当):

# 推奨される暗号スイート
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes256-ctr
# 推奨MAC
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
# 鍵交換アルゴリズム
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256

3. 公開鍵認証のみに限定

PasswordAuthentication no
PermitRootLogin no
AuthenticationMethods publickey

PermitRootLogin noyes, prohibit-password, without-password などにできますが、最も厳密なのは no


4. ホスト鍵の制限(古いアルゴリズムの無効化)

安全な鍵タイプだけに限定:

HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key

2048ビット未満の RSA 鍵や DSA 鍵は使用しないでください。


5. ログと監査

LogLevel VERBOSE

これにより、どの鍵が認証に使用されたかなどの詳細ログが取れます(監査用途)。


6. タイムアウト・接続制限

ClientAliveInterval 300
ClientAliveCountMax 2
MaxAuthTries 3
LoginGraceTime 30

7. 使用ポート変更(ポートスキャン対策)※限定的

Port 222

セキュリティを劇的に高めるものではありませんが、攻撃の「騒音」を減らす程度には有効です。


8. 特定ユーザ/グループに限定

AllowUsers alice bob
# また
AllowGroups sshusers

※使えなかったので注意

9. システム的な補足設定

  • SELinux の有効化(AlmaLinux 9 はデフォルトで有効)
  • Fail2Ban や firewalld との併用(ブルートフォース対策)
  • 公開鍵のビット数:
    • RSA なら最低 3072bit(理想は 4096bit)
    • ed25519(高速・高セキュリティ)

✅ 設定後の再起動

設定ファイル編集後は必ず以下を実行:

sshd -t   # 設定ファイルの構文チェック
systemctl restart sshd