January 12, 2026

DebianへのSSH接続を公開鍵認証とワンタイムパスワードの2段階にする

LinuxへのSSHログイン時に、ワンタイムパスワードの2段階認証を追加してくれるものといえば、Googleが提供してくれる libpam-google-authenticator が思い浮かぶ。
昔、CentOS 7で使ったことがあったけど、今回はDebian 12とDebian 13で試してみた。

前提

このドキュメントで前提とすること

  • 現在SSHはパスワードログイン禁止、公開鍵認証で接続している
  • Homeディレクトリを暗号化していない
  • TOTP (Time-based One-time Password) を使う
    • HOTP (HMAC-Based One-time Password) を使いたい場合はドキュメントを読んでほしい

パッケージを探す

GitHubの手順ではビルドしろと書いてある…

aptでパッケージ提供してくれていないかなと探してみたら見つけたけど、パッケージ名は libpam-google-authenticator だった。

$ apt-cache show libpam-google-authenticator | egrep 'Package:|Version:|Homepage:'
Package: libpam-google-authenticator
Version: 20191231-2.1
Homepage: https://github.com/google/google-authenticator-libpam

GitHubの Tags を見ると2025/02/14の1.11が最新らしいけど、ビルドは面倒なのでこのパッケージを使う。

インストール

普通にコマンドを打つだけ

$ sudo apt update
$ sudo apt install libpam-google-authenticator

設定

ワンタイムパスワードの生成

SSHログインする全ての既存、新規ユーザーで必要。

パッケージには実行ファイルが1つ含まれている。

$ dpkg -L libpam-google-authenticator | grep bin/
/usr/bin/google-authenticator

/usr/bin/ にはパスが通っているのでそのまま実行するだけ

$ google-authenticator -t

libqrencode が入っていない場合、ここで表示されるリンクでQRコードが表示されるらしいけど、アクセスすると404エラー…
APIが変わった?
QRコードが表示されなかったらシークレットキーで登録できるので困らないはず。

Warning: pasting the following URL into your browser exposes the OTP secret to Google:
  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/debian@debian-host%3Fsecret%HOGEHOGE%26issuer%3Ddebian-host

libqrencodeが入っていればここでQRコードが表示されるので、Google Authenticator(iOS / Android)などで読み取るなり、secret keyをKeePassXCに入力してコードを出力できるようにする。

QRコードが表示されないとか、ソフトがQRコードを読み取りできないなら、シークレットキーを入力して登録。
表示される6桁の数字を入力し、正しければ次に進む。

Your new secret key is: HOGEHOGE
Enter code from app (-1 to skip): 123456
Code confirmed
Your emergency scratch codes are:
  12345678
  23457689
  34567890
  45678901
  56789012

このファイルを更新してよいか?

Do you want me to update your "/home/debian/.google_authenticator" file? (y/n) y

同じ認証トークンの複数回使用を禁止するか?

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

デフォルトではクライアントとサーバーで最大30秒の時間ズレに対応可能、これを4分間に伸ばすか?

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) n

30秒に最大3回のレート制限をするか?

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y

pam

sudo vi /etc/pam.d/sshd

先頭の @include common-auth をコメントアウトし、その次の行に追記。
コメントアウトしない場合、パスワードも尋ねられる。

#@include common-auth
auth required pam_google_authenticator.so

sshd

$ sudo vi /etc/ssh/sshd_config

以下3行の変更、追加が必要。

UsePAM yes
PubkeyAuthentication yes
KbdInteractiveAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

UsePAM はデフォルトで yes のはず。
AuthenticationMethods は書かれていないので最終行に追記する。

設定ファイルの構文チェック

sudo sshd -t

何も言われないなら問題なし

sshdの再起動

$ sudo systemctl restart sshd

ここまでの設定を何かしくじっていると、上記を実行後にログインできなくなるので、既にログインしているユーザーはsshを抜けず、新しいターミナルで接続できることを確認する!

© 2020 nissy-lab.com