5分でできる!SSH + Google Authenticator 二段階認証設定(CentOS7)

Linux
Linux
スポンサーリンク

ここ最近、在宅勤務やサテライトオフィスで仕事をする方が増えたためか、サーバーへの SSH接続を IPアドレスで制限することができないケースがよくあります。しかし、特に重要なサーバーの場合は、パスワード認証だけではちょっと心配ですね。そこで今回は、Google Authenticator を使って SSHログインに二段階認証(「二要素認証」とも言います)を導入する手順をまとめてみました。

Google Authenticator アプリのインストール

すでにインストールされている方も多いかと思いますが、まずはじめに、お手持ちのスマートフォンに Google Authenticator(確認コードの生成アプリ)をインストールしておきます。

Google Authenticator(iOS)
Google Authenticator(Android)

Google Authenticator PAMモジュールのインストール

続いてサーバー(CentOS7)に、Google Authenticator PAMモジュール をインストールします。(EPELリポジトリからパッケージでインストールできます)

EPELリポジトリをインストールします。

sudo yum install epel-release

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

sudo yum install google-authenticator

設定前の注意点

続いて sshd の認証まわりの設定を行うのですが、設定にミスがあるとサーバーにSSHでログインできなくなってしまいますので(^^;) 設定するのとは別のウィンドウで SSHログインしておくことをオススメします。

sshd の認証設定(パスワード認証の場合)

SSHログイン時に、パスワード認証に加えて Google Authenticator の確認コードが求められるように設定します。

sshd の設定

sshd のチャレンジレスポンス認証を有効にします。(Google Authenticator もチャレンジレスポンス認証の一種です)

sudo vim /etc/ssh/sshd_config

ChallengeResponseAuthentication no
 ↓
ChallengeResponseAuthentication yes

sshd を再起動します。

sudo systemctl restart sshd

PAM の設定

続いて sshdの認証に Google Authenticator PAMモジュールを追加します。

次の1行を最終行に追加します。
sudo vim /etc/pam.d/sshd

auth required pam_google_authenticator.so nullok echo_verification_code

<モジュールオプションについて>

nullok
 Google Authenticator を設定していないユーザーが、認証コードなしでログインできることを許可します。SSHログインをパスワードで行なっている場合は、このオプションを付けないと SSHログインができなくなりますのでご注意ください。Google Authenticator の設定が終わったら、この nullok オプションは削除しても構いません。

echo_verification_code
 入力時に、確認コードを表示するようにします。確認コードの有効期間は30秒ですので表示しても問題ありません。確認コードの入力ミスを減らせますので、付けておくことをオススメします。

参考資料:https://github.com/google/google-authenticator-libpam#module-options

Google Authenticator の設定

本題の Google Authenticator の設定です。

サーバーに Google Authenticator を設定するユーザーでSSHログインして、下記コマンドを実行すると対話形式で Google Authenticator の設定が開始します。

google-authenticator

以下のように色々聞かれますが、すべて「y」(はい)でOKです。

y の場合は時間ベースの確認コードを、n の場合はカウンターベースの確認コードを生成します。

Do you want authentication tokens to be time-based (y/n) y

下のようなQRコード(バーコード)が表示されますので、Google Authenticator アプリでこの QRコードスキャンして登録します。(QRコードが表示されない場合は、表示されたURLにアクセスすればQRコードを表示できます)また、QRコードの下に Google Authenticator アプリが使えなくなった時のための緊急コードが表示されますので、安全な場所に控えておきましょう。

Google Authenticator 登録用のQRコード

Google Authenticator の秘密鍵や設定情報を以下の場所に保存してもよいかの確認です。

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

同じ確認コードを複数回使うことを禁止します。これにより30秒ごとに1回のログインに制限されます。

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

クライアント(Google Authenticatorをインストールしたスマートフォン)とサーバーで最大4分の時間のずれを許容します。スマートフォンの時間がずれることは稀にありますので y にしておくことをオススメします。

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) y

30秒ごとに3回までのログイン試行に制限します。すなわち確認コードを3回間違えるとその確認コードは無効になるということです。y にする場合は、あわせて Google Authenticator PAMモジュールの echo_verification_code オプションを付けておきましょう。

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

Google Authenticator の設定は、以上です。

Google Authenticator を設定したユーザーで SSHログインすると、パスワード認証の後に、Google Authenticator の確認コードが求められるようになります。

Password:
Verification code: 985159 ← 確認コード

sshd の認証設定(公開鍵認証の場合)

SSHの公開鍵認証を使っている場合は、Google Authenticator の設定を行っても確認コードを求められませんので、sshd と PAM に追加の設定が必要になります。

sshd の設定

sshd の設定ファイルに次の1行を追加します。(パスワード認証は無効になりますのでご注意ください)
sudo vim /etc/ssh/sshd_config

AuthenticationMethods publickey,keyboard-interactive

sshd を再起動します。

sudo systemctl restart sshd

この設定により、SSHログインの際に公開鍵認証(publickey)に加えて、Google Authenticator の確認コード(keyboard-interactive)の認証も求められるようになります。ただし、keyboard-interactive にはパスワード認証も含まれますので、公開鍵認証 → パスワード認証 → 確認コード の三段階認証になります(^^;)

PAM の設定

パスワード認証を求められないようにするには、以下のように設定します。

先ほど追加した Google Authenticator PAMモジュールを無効にします。
sudo vim /etc/pam.d/sshd

auth required pam_google_authenticator.so nullok echo_verification_code
  ↓
# auth required pam_google_authenticator.so nullok echo_verification_code

続いて、パスワード認証を無効にして、その下の行に Google Authenticator PAMモジュールを追加します。
sudo vim /etc/pam.d/password-auth

auth sufficient pam_unix.so nullok try_first_pass
 ↓
#auth sufficient pam_unix.so nullok try_first_pass
---(以下を追加)---
auth sufficient pam_google_authenticator.so nullok echo_verification_code
 

以上で、SSHの公開鍵認証を使っている場合でも Google Authenticator の確認コードのみが求められるようになります。

おわりに

最近なにかと二段階認証が必要になることが多くなりました。Google Authenticator は手軽に導入できるので便利ですね。

 

コメント

  1. みよしゆういち より:

    お世話になります。

    ブログ拝見し設定したのですが
    公開鍵の設定ができません。

    どいうふうに切り分けなどすればよいでしょうか

    他のサイトにソースがなく苦戦してます。

    • あぱーブログ あぱーブログ より:

      >みよしゆういちさん
      コメントありがとうございます。
      一度 sshd の認証設定を初期値に戻して、公開鍵認証ができるか否か試してみてください。

タイトルとURLをコピーしました