2要素認証(2段階認証 ssh+Google Authenticator)でRHELへ接続してみた

コンピュータ
スポンサーリンク

sshでパスワード認証後、Google Authenticatorを利用して2要素認証する場合の手順となります。

注意事項として、Tera Term(テラターム)は2要素認証のログインに対応していないため、puttyなど他のターミナルソフトを利用する必要があります。

なお、SELinuxも無効にしている前提となります。SELinuxが有効の場合、別途設定が必要となりますのでご注意ください。

スポンサーリンク

ツール類の追加

EPELの追加

EPELに関する詳細は以下を参照ください。

Extra Packages for Enterprise Linux (EPEL) の使用方法 – Red Hat Customer Portal

今回利用するツールは拡張パッケージとなるため、RHELからサポートを受けることはできませんのでご注意ください。CnetOSの場合、epelを導入する際のコマンドが少し変わるだけで、他は同じ手順でできると思います。

//RHEL8.5の環境で実施しております
# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.5 (Ootpa)
#
#
#


//epel追加前のリポジトリ状態
# dnf repolist
サブスクリプション管理リポジトリーを更新しています。
repo id                                                         repo の名前
rhel-8-for-x86_64-appstream-rpms                                Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
rhel-8-for-x86_64-baseos-rpms                                   Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
#
#


//epelのリポジトリ追加
# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
サブスクリプション管理リポジトリーを更新しています。
Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)                                                                17 kB/s | 4.5 kB     00:00
Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)                                                                36 MB/s |  45 MB     00:01
Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)                                                                   16 kB/s | 4.1 kB     00:00
Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)                                                                   43 MB/s |  51 MB     00:01
メタデータの期限切れの最終確認: 0:00:08 時間前の 2022年08月06日 14時53分14秒 に実施しました。
epel-release-latest-8.noarch.rpm                                                                                        27 kB/s |  24 kB     00:00
依存関係が解決しました。
=======================================================================================================================================================
 パッケージ                            アーキテクチャー                バージョン                          リポジトリー                          サイズ
=======================================================================================================================================================
インストール:
 epel-release                          noarch                          8-16.el8                            @commandline                           24 k

トランザクションの概要
=======================================================================================================================================================
インストール  1 パッケージ

合計サイズ: 24 k
インストール後のサイズ: 34 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                               1/1
  インストール中   : epel-release-8-16.el8.noarch                                                                                                  1/1
  scriptletの実行中: epel-release-8-16.el8.noarch                                                                                                  1/1
Many EPEL packages require the CodeReady Builder (CRB) repository.
It is recommended that you run /usr/bin/crb enable to enable the CRB repository.

  検証             : epel-release-8-16.el8.noarch                                                                                                  1/1
インストール済みの製品が更新されています。

インストール済み:
  epel-release-8-16.el8.noarch

完了しました!
#
#


//リポジトリが追加されていることを確認
# dnf repolist
サブスクリプション管理リポジトリーを更新しています。
repo id                                                         repo の名前
epel                                                            Extra Packages for Enterprise Linux 8 - x86_64
epel-modular                                                    Extra Packages for Enterprise Linux Modular 8 - x86_64
rhel-8-for-x86_64-appstream-rpms                                Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
rhel-8-for-x86_64-baseos-rpms                                   Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
#
#

google-authenticatorとqrencodeの追加

google-authenticator(Googleの2要素認証するためのツール)と、qrencode(QRコードを表示するツール)を追加します。

//google-authenticatorの追加
# dnf install google-authenticator
サブスクリプション管理リポジトリーを更新しています。
メタデータの期限切れの最終確認: 0:01:32 時間前の 2022年08月06日 15時06分30秒 に実施しました。
依存関係が解決しました。
=======================================================================================================================================================
 パッケージ                                    アーキテクチャー                バージョン                          リポジトリー                  サイズ
=======================================================================================================================================================
インストール:
 google-authenticator                          x86_64                          1.07-1.el8                          epel                           57 k

トランザクションの概要
=======================================================================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 57 k
インストール後のサイズ: 135 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
google-authenticator-1.07-1.el8.x86_64.rpm                                                                             548 kB/s |  57 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                    44 kB/s |  57 kB     00:01
Extra Packages for Enterprise Linux 8 - x86_64                                                                         1.6 MB/s | 1.6 kB     00:00
GPG 鍵 0x2F86D6A1 をインポート中:
 Userid     : "Fedora EPEL (8) <epel@fedoraproject.org>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
これでよろしいですか? [y/N]: y
鍵のインポートに成功しました
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                               1/1
  インストール中   : google-authenticator-1.07-1.el8.x86_64                                                                                        1/1
  scriptletの実行中: google-authenticator-1.07-1.el8.x86_64                                                                                        1/1
  検証             : google-authenticator-1.07-1.el8.x86_64                                                                                        1/1
インストール済みの製品が更新されています。

インストール済み:
  google-authenticator-1.07-1.el8.x86_64

完了しました!
#
#
#

//qrencodeの追加
# dnf install qrencode
サブスクリプション管理リポジトリーを更新しています。
メタデータの期限切れの最終確認: 0:02:05 時間前の 2022年08月06日 15時06分30秒 に実施しました。
依存関係が解決しました。
=======================================================================================================================================================
 パッケージ                       アーキテクチャー          バージョン                       リポジトリー                                        サイズ
=======================================================================================================================================================
インストール:
 qrencode                         x86_64                    3.4.4-5.el8                      rhel-8-for-x86_64-appstream-rpms                     27 k
依存関係のインストール:
 qrencode-libs                    x86_64                    3.4.4-5.el8                      rhel-8-for-x86_64-appstream-rpms                     59 k

トランザクションの概要
=======================================================================================================================================================
インストール  2 パッケージ

ダウンロードサイズの合計: 85 k
インストール後のサイズ: 169 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/2): qrencode-3.4.4-5.el8.x86_64.rpm                                                                                 109 kB/s |  27 kB     00:00
(2/2): qrencode-libs-3.4.4-5.el8.x86_64.rpm                                                                            233 kB/s |  59 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                   335 kB/s |  85 kB     00:00
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                               1/1
  インストール中   : qrencode-libs-3.4.4-5.el8.x86_64                                                                                              1/2
  インストール中   : qrencode-3.4.4-5.el8.x86_64                                                                                                   2/2
  scriptletの実行中: qrencode-3.4.4-5.el8.x86_64                                                                                                   2/2
  検証             : qrencode-3.4.4-5.el8.x86_64                                                                                                   1/2
  検証             : qrencode-libs-3.4.4-5.el8.x86_64                                                                                              2/2
インストール済みの製品が更新されています。

インストール済み:
  qrencode-3.4.4-5.el8.x86_64                                             qrencode-libs-3.4.4-5.el8.x86_64

完了しました!
#
#
#

認証絡みの設定

sshdの設定

//sshd_configの修正
# vim /etc/ssh/sshd_config

以下の部分を「no」から「yes」に修正する
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication no
ChallengeResponseAuthentication yes

//設定を反映させる
# systemctl restart sshd.service

pamの設定

//sshd_configの修正
# vim /etc/pam.d/sshd

以下の部分を「no」から「yes」に修正する
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication no
ChallengeResponseAuthentication yes

//sshdに設定を反映させる
# systemctl restart sshd.service



//sshd向けのpam修正
# vim /etc/pam.d/sshd

末尾に以下の内容を追記する
auth required pam_google_authenticator.so nullok echo_verification_code

動作確認

テスト用のユーザ作成

//動作確認用のユーザ「google」を作成し、パスワードを設定
# useradd google
# passwd google
ユーザー google のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。


Google-Authの設定

スマホ側のGoogle Authアプリは、直感で操作可能と思いますので、省略いたしますが、本作業から必要となりますので準備をお願いします。

以下、2要素認証するユーザの数だけ、本作業を実施する必要があります。

//2要素認証するユーザにて作業を実施するため、suする
# su - google
$
$

//google-authの設定
$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y  ← yを実行すると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/google@pop%3Fsecret%3D46RJWT3X2OK6GTI73QATH3LDEM%26issuer%3Dpop




★実機では、ここにQRコードが表示されます★




Your new secret key is: 46RJWT3X2OK6GTI73QATH3LDEMt key is: 46RJWT3X2OK6GTI73QATH3LDEM
Enter code from app (-1 to skip): 650922 ← スマホのGoogle Auth側でQRコードを読み込み、表示されている番号を入力
Code confirmed
Your emergency scratch codes are: ← google authが利用できない場合の緊急用コード
  70916798
  20206183
  17153262
  83918300
  84767965

Do you want me to update your "/home/google/.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, 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

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
$


//設定したユーザのホームディレクトリ配下に設定内容が保存されている
$ cat .google_authenticator
46RJWT3X2OK6GTI73QATH3LDEM
" RATE_LIMIT 3 30
" WINDOW_SIZE 17
" DISALLOW_REUSE
" TOTP_AUTH
70916798
20206183
17153262
83918300
84767965
$

以降の動作確認は、別途、新しいターミナルなどを開いて動作確認してください。本作業を失敗するとログインできなくなる恐れがありますので、必ず上記作業をしているセッションは閉じないでください。

puttyで接続確認

冒頭に記載している通り、Teratermは2要素認証の接続に対応しておりませんので(回避方法あります)、puttyなどで接続確認をします。

puttyを起動し、接続先IPアドレスを入力し接続

① ユーザ名

②ユーザに紐づくパスワード

② google-authのアプリで表示されているコードを入力

これで、無事にログインができたと思います。

上記作業でも、うまくいかない場合、SELinuxが有効でないか、sshdやpamの設定があっているか再度ご確認ください。また、ネットワーク絡みの問題か切り分けるためには、次の通り、ローカル内で接続できるか試してみると良いかと思います。

//ローカルから、googleユーザでssh接続してみることにより、ネットワーク絡みの問題か切り分けることが可能
# ssh google@localhost
Password:
Verification code: 279486
Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last failed login: Sat Aug  6 16:01:40 JST 2022 from ::1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Sat Aug  6 16:01:02 2022
$
$

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