LinuxでSSHの二要素認証を設定する方法
デフォルトでは、SSHはリモートマシン間で安全なデータ通信を使用していますが、SSH接続に追加のセキュリティ層を加えたい場合は、Google Authenticator(二要素認証)モジュールを追加することができます。これにより、SSHサーバーに接続する際にランダムなワンタイムパスワード(TOTP)検証コードを入力する必要があります。接続時にスマートフォンまたはPCから検証コードを入力する必要があります。
Google Authenticatorは、Googleによって開発されたワンタイムパスコード(TOTP)検証トークンの実装を含むオープンソースモジュールです。
いくつかのモバイルプラットフォームとPAM(プラガブル認証モジュール)をサポートしています。これらのワンタイムパスコードは、OATH オープン認証イニシアティブによって作成されたオープンスタンダードを使用して生成されます。
この記事では、Fedora、CentOS Stream、Rocky Linux、AlmaLinux、Ubuntu、Debian、MintなどのRedHat系およびDebian系Linuxディストリビューションで、二要素認証のためにSSHを設定および構成する方法を示します。
LinuxにGoogle Authenticatorをインストールする
二要素認証を設定したい機械を開き、PAMモジュールがGoogle authenticatorモジュールと正しく動作するために必要な開発ライブラリと共に、以下のPAMライブラリをインストールしてください。
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) yLinuxでSSHをGoogle Authenticatorを使用するように設定する
PAM 設定ファイル &8216;&47;etc&47;pam.d&47;sshd&8216; を開き、ファイルの下部に次の行を追加します。
auth required pam_google_authenticator.so nullok
auth required pam_permit.so
次に、SSH構成ファイル&8216;&47;etc&47;ssh&47;sshd_config&8216;を開き、下にスクロールして次の行を見つけます。
ChallengeResponseAuthentication no
それを「はい」に変更します。だから、こうなります。
ChallengeResponseAuthentication yes
最後に、新しい変更を反映させるためにSSHサービスを再起動します。
# systemctl restart sshd
Or
sudo systemctl restart sshd
Google Authenticatorアプリの設定
スマートフォンでGoogle Authenticatorアプリを起動します。+を押して「セットアップキーを入力」を選択します。このアプリを持っていない場合は、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のアドオンを使用することもできます。
style="color: red;">重要: 二要素認証はパスワードベースのSSHログインで機能します。プライベート/パブリックキーのSSHセッションを使用している場合、二要素認証は無視され、直接ログインされます。




