0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Libpam-google-authenticatorを使ってSSHでLinuxでログインする

Last updated at Posted at 2024-12-11

最近この内容を知ったのでまとめてみました。

SSHで libpam-google-authenticator を使って二段階認証を設定する

Linux サーバーに SSH でログインする際、二段階認証(2FA)を導入してセキュリティを強化する手順を紹介します。libpam-google-authenticator を使用し、Google Authenticator アプリを使ったワンタイムパスワード (OTP) 認証を追加します。

前提条件

  • Linux サーバー (例: Ubuntu, CentOS など)
  • SSH が既に動作している
  • sudo 権限を持つユーザー
  • スマートフォンに Google Authenticator アプリをインストール済み

手順概要

  1. libpam-google-authenticator をインストールする
  2. SSH で PAM 認証を設定する
  3. ユーザーごとに Google Authenticator を設定する
  4. SSH 設定ファイルを編集し、二段階認証を有効にする

1. libpam-google-authenticator のインストール

まず、Google Authenticator の PAM モジュールをインストールします。

Ubuntu または Debian 系の場合

sudo apt update
sudo apt install libpam-google-authenticator

CentOS または RHEL 系の場合

sudo yum install epel-release
sudo yum install google-authenticator

2. PAM 設定の更新

/etc/pam.d/sshd ファイルに、Google Authenticator を使うための設定を追加します。

sudo nano /etc/pam.d/sshd

ファイルの一番下に、次の行を追加します:

auth required pam_google_authenticator.so

3. ユーザーごとに Google Authenticator を設定

各ユーザーが Google Authenticator で使用する OTP を生成します。

Google Authenticator を設定するユーザーでログインし、次のコマンドを実行します:

google-authenticator

ここで、いくつかの質問が表示されます。設定例は一例ですので個々での設定でお願いいたします。

  • 新しい認証トークンを作成しますか?: y
  • その後、緊急のスクラッチコードが5つ発行されます。
  • .google_authenticator ファイルを更新しますか? y
  • 同じ認証トークンの複数回使用を禁止しますか?これにより約30秒ごとに1回のログインに制限されますが、中間者攻撃に気付いたり、防いだりする可能性が高まります。 y
  • 同期の誤差を許可しますか?: y
  • 再利用可能なバックアップコードを表示しますか?: y
  • Rate-limiting 機能を有効にしますか?: y

この操作により、QR コードと秘密鍵が表示されます。Google Authenticator アプリで QR コードをスキャンして、OTP を生成できるようにします。

4. SSH 設定を変更して 2FA を有効にする

次に、SSH の設定を変更します。

/etc/ssh/sshd_config ファイルを開きます。

sudo nano /etc/ssh/sshd_config

次の 2 つのパラメータを確認・変更します:

ChallengeResponseAuthentication yes
UsePAM yes

設定を保存して終了したら、SSH サービスを再起動します。

sudo systemctl restart sshd

5. 設定のテスト

設定が完了したら、一度 SSH 接続を試みます。パスワードの後に、Google Authenticator に表示されるワンタイムパスワードを入力するよう求められます。

ssh your-user@your-server-ip

まとめ

これで、SSH ログインに二段階認証が追加されました。今後は、パスワードに加えてワンタイムパスワードを入力する必要があるため、セキュリティが大幅に向上します。Google Authenticator や、sshでのログインが可能です。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?