ウェブサイト検索

Linux で SSH の 2 要素認証をセットアップする方法


デフォルトでは、SSH はすでにリモート マシン間で安全なデータ通信を使用していますが、SSH 接続に追加のセキュリティ層を追加したい場合は、Google Authenticator を追加できます。2 要素認証) モジュールを使用すると、SSH サーバーへの接続中にランダムなワンタイム パスワード (TOTP) 確認コードを入力できます。接続するときにスマートフォンまたはPCから確認コードを入力する必要があります。

Google Authenticator は、Google が開発したワンタイム パスコード (TOTP) 検証トークンの実装を含むオープンソース モジュールです。

いくつかのモバイル プラットフォームと PAM (Pluggable Authentication Module) をサポートします。これらのワンタイム パスコードは、OATHInitiative for Open Authentication によって作成されたオープン標準を使用して生成されます。

この記事では、Fedora、CentOS Stream、Rocky Linux、AlmaLinux などの RedHat ベースおよび Debian ベースの Linux ディストリビューションで 2 要素認証用にSSH をセットアップして構成する方法を説明します。 Ubuntu、Debian、Mint。

Linux への Google Authenticator のインストール

2 要素認証を設定するマシンを開き、次の PAM ライブラリと、PAM モジュールが < で正しく動作するために必要な開発ライブラリをインストールします。Google 認証モジュール。

RedHat ベースのシステムでは、次の yum コマンドを使用して「pam-devel」パッケージをインストールします。

yum install google-authenticator -y

Debian ベースのシステムでは、次の apt コマンドを使用して「libpam0g-dev」パッケージをインストールします。

sudo apt install libpam-google-authenticator -y

Google認証トークンを生成する

google-authenticator」 コマンドを実行すると、一連の質問が表示されます。

google-authenticator

ほとんどの場合、答えとして「y」 (はい) と入力するだけです。問題が発生した場合は、「google-authenticator」コマンドを再度入力して設定をリセットできます。

  • 認証トークンを時間ベースにしますか (y/n) y

この質問の後、「秘密キー」と「緊急コード」を取得します。これらの詳細をどこかに書き留めてください。後でGoogle Authenticator アプリを設定するために「秘密キー」が必要になります。

google-authenticator

Do you want authentication tokens to be time-based (y/n) y
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/root@tecmint%3Fsecret%3DCYZF2YF7HFGX55ZEPQYLHOO5JM%26issuer%3Dtecmint
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: CYZF2YF7HFGX55ZEPQYLHOM
Enter code from app (-1 to skip): -1 Code confirmation skipped Your emergency scratch codes are: 83714291 53083200 80975623 57217008 77496339

次に、セットアップ ウィザードに従い、以下に示すように、ほとんどの場合、答えを「y」 (はい) と入力します。

Do you want me to update your "/root/.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 By default, tokens are good for 30 seconds and 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. If you experience problems with poor time synchronization, you can increase the window from its default size of 1:30min to about 4min. Do you want to do so (y/n) y 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

Linux で Google Authenticator を使用するための SSH の構成

PAM 構成ファイル「/etc/pam.d/sshd」を開き、ファイルの最後に次の行を追加します。

auth required pam_google_authenticator.so nullok
auth required pam_permit.so

次に、SSH 設定ファイル「/etc/ssh/sshd_config」を開き、下にスクロールして次の行を見つけます。

ChallengeResponseAuthentication no

はい」に変更します。ということで、こうなります。

ChallengeResponseAuthentication yes

最後に、SSH サービスを再起動して、新しい変更を適用します。

systemctl restart sshd
Or
sudo systemctl restart sshd

Google認証アプリの設定

スマートフォンで Google 認証システム アプリを起動します。 + を押して、「セットアップ キーを入力してください」を選択します。このアプリをお持ちでない場合は、Android/iPhone/Blackberry デバイスに Google Authenticator アプリをダウンロードしてインストールできます。

アカウントの「名前」を追加し、前に生成した「秘密キー」を入力します。

携帯電話上で30 秒ごとに常に変更されるワンタイム パスワード (確認コード) が生成されます。

ここでSSH経由でログインしようとすると、Google Authenticator コード (確認コード) とパスワードの入力を求められます。 > SSH 経由でログインしようとするたびに。この確認コードを入力できる時間は 30 秒のみです。入力を忘れた場合は、新しい確認コードが再生成されます。

login as: tecmint
Access denied
Using keyboard-interactive authentication.
Verification code:
Using keyboard-interactive authentication.
Password:
Last login: Tue Apr 23 13:58:29 2022 from 172.16.25.125
[root@tecmint ~]#

スマートフォンをお持ちでない場合は、Authenticator と呼ばれるFirefox アドオンを使用して 2 要素認証を行うこともできます。

重要: 2 要素認証は、パスワードベースの SSH ログインで機能します。秘密/公開キー SSH セッションを使用している場合、2 要素認証は無視され、直接ログインします。