SSH接続時に公開鍵認証してからパスワード認証する設定

はじめに

SSHに接続した際に以下の順番で2つの認証をパスしてログインできるようにしたい。

  1. 公開鍵認証
  2. パスワード認証

環境

以下のとおり。

% cat /etc/debian_version
12.12

% ssh -V
OpenSSH_9.2p1 Debian-2+deb12u7, OpenSSL 3.0.17 1 Jul 2025

設定手順

当該サーバにログインし、以下のコマンドで設定する。

% cd /etc/sshd
% sudo cp -p sshd_config sshd_config.YYYYMMDD (YYYYMMDDは今日の日付)
% sudo vi sshd_config
% diff sshd_config.20251121 sshd_config
59c59
< PasswordAuthentication no
---
> #PasswordAuthentication no
124a125,129
>
> # For login with public key and password
>
> PasswordAuthentication yes
> AuthenticationMethods publickey,password

これまではパスワード認証をゆるさず、公開鍵認証のみだった。
これを「AuthenticationMethods」の設定で、公開鍵認証の後にパスワード認証をするようにしている。

このメソッドはメモ:Google Authenticatorを用いたSSHの2要素認証でも使っていた。