はじめに(このページの目的)
VPSを構築した直後は、rootログインやパスワードログインが有効な状態であり、セキュリティ上のリスクが高い状態です。 このページでは、初心者の方でも確実に実施できるよう、SSH鍵認証の導入・ファイアウォール設定・不要サービスの停止など、VPSの基本的なセキュリティ強化手順を丁寧に解説します。
作業前の確認ポイント
- rootアカウントにログインできる(初期状態ではrootのみ有効)
- ローカルPCにSSHクライアント(例:Tera Term、OpenSSH)がある
- 公開鍵/秘密鍵の管理やファイル転送の基本を理解している
実施手順(ステップ構成)
ステップ1:パッケージの最新化
まず、システムのアップデートを行い、既知の脆弱性を修正します。
sudo dnf update -y
ステップ2:一般ユーザーの作成とsudo権限付与
管理用の一般ユーザー(例:adminuser
)を作成し、wheel
グループに追加します。
sudo adduser adminuser
sudo passwd adminuser
sudo usermod -aG wheel adminuser
ステップ3:SSH公開鍵認証を設定
ローカルPCで鍵ペアを生成
ssh-keygen -t rsa -b 4096
生成された公開鍵(~/.ssh/id_rsa.pub
)をコピーします。
サーバー側で公開鍵を登録
sudo mkdir -p /home/adminuser/.ssh
sudo nano /home/adminuser/.ssh/authorized_keys
公開鍵を貼り付けて保存したら、以下の権限を設定します:
sudo chown -R adminuser:adminuser /home/adminuser/.ssh
sudo chmod 700 /home/adminuser/.ssh
sudo chmod 600 /home/adminuser/.ssh/authorized_keys
ステップ4:SSH設定変更(rootログイン禁止・パスワード無効化)
以下の設定を /etc/ssh/sshd_config
の末尾などに追記または変更します。
sudo nano /etc/ssh/sshd_config
# パスワードログインを無効化(鍵認証のみ許可)
PasswordAuthentication no
# rootログインを完全に禁止
PermitRootLogin no
# チャレンジレスポンス認証の無効化
ChallengeResponseAuthentication no
# PAM(Pluggable Authentication Modules)使用(通常はyesでOK)
UsePAM yes
# SSHログイン可能なユーザーを制限(例:adminuser のみに制限)
AllowUsers adminuser
設定後、sshdを再起動します:
sudo systemctl restart sshd
ステップ5:ファイアウォール設定(firewalld)
ファイアウォールの起動と有効化
sudo systemctl enable --now firewalld
必要なサービスを許可
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
不要なサービス(例:cockpit)の無効化
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --reload
現在の設定の確認
sudo firewall-cmd --list-all
ステップ6:fail2banの導入(SSH攻撃対策)
sudo dnf install -y fail2ban
sudo systemctl enable --now fail2ban
ステップ7:SSHポート番号の変更(任意)
例えば、ポート番号を2222
に変更したい場合:
sudo nano /etc/ssh/sshd_config
Port 2222
ファイアウォールにもポートを追加:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
sudo systemctl restart sshd
ステップ8:不要なサービスの停止
現在有効なサービス一覧を確認し、不要なものを無効化します。
sudo systemctl list-unit-files --type=service | grep enabled
# 例:bluetoothサービスを無効化
sudo systemctl disable bluetooth
sudo systemctl stop bluetooth
ステップ9:自動セキュリティアップデート設定
sudo dnf install -y dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
設定ファイル:/etc/dnf/automatic.conf
接続確認とトラブルシューティング
症状:
作成したユーザーでパスワードログインを試みたが、「認証失敗」になる。 /var/log/secure
に次のようなログが出る:
Disconnected from authenticating user adminuser xxx.xxx.xxx.xxx port 5989 [preauth]
原因:
/etc/sysconfig/sshd
に以下のようなオプションが設定されていた:
OPTIONS="-oPasswordAuthentication=no"
対処方法:
sudo nano /etc/sysconfig/sshd
該当行をコメントアウトまたは削除:
# OPTIONS="-oPasswordAuthentication=no"
その後、sshdを再起動:
sudo systemctl restart sshd
/etc/ssh/sshd_config
側の設定も確認します:
PasswordAuthentication yes
UsePAM yes
再度、一般ユーザーでSSHログインできるか確認します。
補足情報まとめ(表形式など)
設定項目 | 推奨値 |
---|---|
公開鍵認証 | 有効(PasswordAuthentication no) |
rootログイン | 禁止(PermitRootLogin no) |
ファイアウォール | 有効(firewalld使用) |
SSHポート | 22(または任意の高番号ポート) |
fail2ban | 導入済み |
dnf-automatic | タイマー有効化済み |
まとめ
- rootログインやパスワードログインは無効化し、SSH鍵認証へ移行
- 一般ユーザーにsudo権限を付与して運用
- firewalldやfail2banによる外部からの保護設定を実施
- 自動アップデート設定により、長期的な保守性も確保
- トラブル時はログや
/etc/sysconfig/sshd
の確認を忘れずに
コメント