「Google Authenticator」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(校正(でき・出来))
(→‎SSH: 校正(two-pass authentication --> 2 パス認証))
 
(同じ利用者による、間の10版が非表示)
10行目: 10行目:
 
{{Related|アイデンティティ管理}}
 
{{Related|アイデンティティ管理}}
 
{{Related articles end}}
 
{{Related articles end}}
[https://github.com/google/google-authenticator Google Authenticator] はワンタイムパスコード (OTP) による2段階認証を提供します。OTP 生成アプリケーション iOS, Android, Blackberry で利用できます。[[S/KEY 認証]]と同じに認証機構は Linux の [[PAM]] システムに統合されています。このガイドではこの機構をインストール設定する方法をします。
+
[https://github.com/google/google-authenticator Google Authenticator] はワンタイムパスコード ([[Wikipedia:One-time password|OTP]]) を使用した二段階認証手続きを提供します。この認証方式[[オープン認証イニシアチブ|オープン認証イニシアチブ(OATH)]]って最初標準化されました。認証メカニズムは Linux の [[PAM]] システムに統合されています。ガイドではこのメカニズムのインストール設定方法を紹介します。
   
  +
逆の操作(Linux 上で Google Authenticator 互換のコードを生成する方法)については、以下の[[#Code generation|コード生成]]を参照してください。
==インストール==
 
   
  +
== インストール ==
{{Pkg|libpam-google-authenticator}} パッケージをインストールしてください。開発版は {{AUR|google-authenticator-libpam-git}} でインストールできます。
 
   
  +
クライアントプログラム {{man|1|google-authenticator}} と PAM モジュール {{ic|pam_google_authenticator.so}} を提供する {{Pkg|libpam-google-authenticator}} パッケージを[[インストール]]します。開発版は {{AUR|google-authenticator-libpam-git}} で利用可能です。
==PAM の設定==
 
   
  +
== 設定 ==
{{Warning|SSH で設定を行っている場合は、全てが動作するか確認する前にセッションを終了してはいけません。自分自身をロックアウトしてしまう危険があります。また、PAM を有効にする前にキーファイルを生成するようにしてください。}}
 
   
  +
このセクションでは、システムの [[PAM]] を設定して、SSH およびオプションで[[#デスクトップログイン|デスクトップログイン]]に対して Google Authenticator の OTP 認証を要求する方法を説明します。
大抵は2段階認証が必要になるのはリモートログインのときだけです。該当する PAM 設定ファイルは {{ic|/etc/pam.d/sshd}} です。Google Authenticator をあらゆるところで使いたい場合、{{ic|/etc/pam.d/system-auth}} に変更を加えます。ただし、その場合は自分を締め出してしまわないように細心の注意を払って下さい。このガイドでは {{ic|/etc/pam.d/sshd}} を編集するので、ローカルセッションではとりあえず安全に設定することができます (絶対安全というわけではありません)。
 
   
  +
{{Note|完全な設定を行うには、[[#使用方法|シークレットキーのファイル]]を生成する必要があります。PAM 設定を編集(適用)する前に生成することを検討してください。}}
Unix パスワードと OTP の両方を入力するために {{ic|pam_google_authenticator.so}} を以下のように {{ic|/etc/pam.d/sshd}} の system-remote-login 行の上に追加します:
 
  +
  +
=== SSH ===
  +
  +
{{Warning|すべての設定を [[SSH]] 経由で行う場合、すべてが正常に動作することを確認するまでセッションを閉じないでください。さもないと、ログインできなくなる可能性があります。}}
  +
  +
通常、リモートログインに対してのみ 2 パス認証を要求します。対応する PAM 設定ファイルは {{ic|/etc/pam.d/sshd}} です。Google Authenticator を全体に適用したい場合は、{{ic|/etc/pam.d/system-auth}} を変更する必要がありますが、この場合は非常に慎重に進め、ログインできなくなることを避けてください。
  +
このガイドでは、最も安全な(必ずしも必要ではありませんが)ローカルセッションで {{ic|/etc/pam.d/sshd}} を編集します。
  +
  +
UNIX パスワードと OTP の両方を入力するには、{{ic|pam_google_authenticator.so}} を {{ic|/etc/pam.d/sshd}} 内の system-remote-login 行の上に追加します。
   
 
'''auth required pam_google_authenticator.so'''
 
'''auth required pam_google_authenticator.so'''
30行目: 39行目:
 
session include system-remote-login
 
session include system-remote-login
   
うすると Unix パスワードの前に OTP を尋ねるようになります。2つのモジュールの順入れ替えると訊かれる順序が逆になります。
+
れにより、OTP が要求された後に UNIX パスワードの入力が求められます。これらのモジュールの順変更すると順序が逆になります。
   
{{Warning|秘密鍵ファイルを生成しユーザーだけが SSH を使てログインできるようになります (下を参照)。}}
+
{{Warning|シークレットキーのファイル(下記参照)を生成しているユーザーのみが SSH を使用してログインできます。}}
   
OTP または Unix パスワードのどちらかでログインできるようにするには:
+
OTP または UNIX パスワードのいずれかでログインを許可すには、次のようにしま
   
auth '''sufficient''' pam_google_authenticator.so
+
auth '''sufficient''' pam_google_authenticator.so
   
{{ic|/etc/ssh/'''sshd_config'''}} でチャレンジレスポンス認証を有効にします:
+
{{ic|/etc/ssh/sshd_config.d/'''99-archlinux.conf'''}} でキーボード対話型認証を有効にします
ChallengeResponseAuthentication yes
 
そして {{ic|sshd}} の設定をリロードしてください:
 
# systemctl reload sshd
 
   
  +
KbdInteractiveAuthentication yes
{{Warning|SSH 鍵のペアによる認証を使っていて[[SSH 鍵#パスワードログインの無効化|パスワードログインを無効化]]していた場合、OpenSSH はこれら全ての設定を無視します。ただし、OpenSSH 6.2 から、{{ic|AuthenticationMethods}} を追加することで2段階認証と鍵による認証の両方を使うことができるようになっています。[[SSH 鍵#2段階認証と公開鍵]] を見て下さい。}}
 
   
  +
最後に、{{ic|sshd.service}} を[[リロード]]します。
==秘密鍵ファイルの生成==
 
   
  +
{{Warning|SSH キーペアで認証し、パスワードログインを[[OpenSSH#公開鍵認証を強制する|無効化]]している場合、OpenSSH はこれを無視します。ただし、OpenSSH 6.2 以降では、{{ic|AuthenticationMethods}} を追加して、二要素認証と鍵ベースの認証の両方を許可できます。詳細は [[OpenSSH#二要素認証と公開鍵]]を参照してください。}}
{{Tip|QR コードを生成するには {{Pkg|qrencode}} をインストールします。認証アプリで QR コードをスキャンすれば自動的に鍵が設定されます。}}
 
   
  +
==== ローカルネットワーク外から接続したときのみ OTP を要求する ====
2段階認証を使用したい場合はユーザーのホームフォルダに秘密鍵ファイルを生成する必要があります。''google-authenticator'' を使うことでとても簡単に生成することができます:
 
   
  +
場合によっては、ローカルネットワーク外から接続するときにのみ 2FA 機能を有効にしたいことがあります。
$ google-authenticator
 
  +
これを実現するには、ファイル(例: {{ic|/etc/security/access-local.conf}})を作成し、2FA をバイパスできるネットワークを追加します。
Do you want authentication tokens to be time-based (y/n) y
 
<Here you will see generated QR code>
 
Your new secret key is: ZVZG5UZU4D7MY4DH
 
Your verification code is 269371
 
Your emergency scratch codes are:
 
70058954
 
97277505
 
99684896
 
56514332
 
82717798
 
 
Do you want me to update your "/home/username/.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) n
 
 
If the computer that you are logging into is not 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
 
   
  +
# ローカル IP 範囲のみ許可
緊急時のスクラッチコードを安全なところに保管しておくことを推奨します (例: コードを印刷して金庫にしまう)。携帯電話 (つまり OTP 生成ツール) をなくしてしまったときの唯一の (SSH による) ログイン手段となります。コードは {{ic|~/.google_authenticator}} にも保存されるので、ログインしている間はいつでも確認することが可能です。
 
  +
+ : ALL : 192.168.20.0/24
  +
# 追加ネットワーク: VPN トンネル IP 範囲(ある場合)
  +
+ : ALL : 10.8.0.0/24
  +
+ : ALL : LOCAL
  +
- : ALL : ALL
   
  +
次に、{{ic|/etc/pam.d/sshd}} を編集し、以下の行を追加します。
==OTP 生成ツールの設定==
 
下記から携帯電話に生成ツールをインストールしてください。
 
   
  +
#%PAM-1.0
* '''FreeOTP''' for [https://play.google.com/store/apps/details?id=org.fedorahosted.freeotp Android]/[https://itunes.apple.com/es/app/freeotp-authenticator/id872559395 iOS].
 
  +
'''auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-local.conf'''
* '''Google Authenticator''' for [https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2 Android]/[https://itunes.apple.com/es/app/google-authenticator/id388497605 iOS].
 
  +
auth required pam_google_authenticator.so
  +
auth include system-remote-login
  +
account include system-remote-login
  +
password include system-remote-login
  +
session include system-remote-login
   
  +
=== デスクトップログイン ===
アプリケーションメニューから適切なボタンをクリックして新しいアカウントを作成し、秘密鍵ファイルを生成したときに表示される URL から QR コードをスキャンするか、秘密鍵を手動で入力します (上の例では 'ZVZG5UZU4D7MY4DH')。
 
   
  +
{{Warning|結果を別のコンソールでテストするまで、セッションからログアウトしないでください。}}
これで30秒ごとに新しく生成されるパスコードトークンをあなたの携帯で見ることができます。
 
   
  +
Google Authenticator の PAM プラグインは、コンソールログインや GDM にも使用できます。{{ic|/etc/pam.d/login}} または {{ic|/etc/pam.d/gdm-password}} ファイルに次の行を追加するだけです。
==テスト==
 
他のマシンや他のターミナルウィンドウからホストに SSH で接続して認証システムが動作しているか確認してください:
 
   
  +
auth required pam_google_authenticator.so
$ ssh hostname
 
  +
login as: <username>
 
  +
== 使用方法 ==
Verification code: <generated/backup-code>
 
  +
Password: <password>
 
  +
2 パス認証を使用したいユーザーは、それぞれ
$
 
  +
* ホームフォルダにシークレットキーのファイルを生成し、
  +
* 対応する OTP ジェネレーターを設定する必要があります。
  +
  +
=== シークレットキーのファイル生成 ===
  +
  +
{{Tip|スキャン可能な QR コードを生成するために {{Pkg|qrencode}} を[[インストール]]してください。QR コードを認証アプリでスキャンすると、自動的にキーが設定されます。}}
  +
  +
''google-authenticator'' は以下のように TOTP シークレットキーのファイルを生成します。
  +
  +
{{hc|$ google-authenticator|
  +
Do you want authentication tokens to be time-based (y/n) y
  +
''generated_QR_code_here''
  +
Your new secret key is: ZVZG5UZU4D7MY4DH
  +
Your verification code is 269371
  +
Your emergency scratch codes are:
  +
70058954
  +
97277505
  +
99684896
  +
56514332
  +
82717798
  +
  +
Do you want me to update your "/home/''username''/.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) n
  +
  +
If the computer that you are logging into is not 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
  +
}}
  +
  +
'''緊急スクラッチコードを安全に保管'''することをお勧めします(印刷して安全な場所に保管してください)。これらは、携帯電話(OTP ジェネレーター)を紛失した場合、SSH を介してログインする唯一の方法です。また、{{ic|~/.google_authenticator}} にも保存されているため、ログインしている限りいつでも確認できます。
  +
  +
==== 保存場所 ====
   
  +
シークレットキーのファイル保存パスを変更したい場合は、{{ic|--secret}} フラグを使用します。
==秘密鍵の保存場所==
 
秘密鍵ファイルの置き場所を変更したい場合、{{ic|--secret}} フラグを使います:
 
   
$ google-authenticator --secret="/'''PATH_FOLDER'''/'''USERNAME'''"
+
$ google-authenticator --secret="/''path_folder''/''username''"
   
{{ic|/etc/pam.d/sshd}} を編集して PAM の設定も忘れずに変更してください:
+
その後、PAM の場所パスを {{ic|/etc/pam.d/sshd}} 忘れずに変更します。
   
 
{{hc|/etc/pam.d/sshd|2=
 
{{hc|/etc/pam.d/sshd|2=
auth required pam_google_authenticator.so user=root secret=/'''PATH_FOLDER'''/${USER}
+
auth required pam_google_authenticator.so user=root secret=/''path_folder''/${USER}
 
}}
 
}}
   
{{ic|user&#61;root}} を使うことで PAM root ユーザーファイルを検索ます。
+
{{ic|1=user=root}} は、PAM root ユーザーを使用してファイルを検索させるために使用されます。
   
また、秘密鍵ファイルのパーミッションに注意してください。所有者だけがファイル読み込めるように設定してください。例えroot を所有者る場合:
+
また、シークレットキーのファイルのパーミッションに注意してください。ファイルは'''所有者のみ読み取り可能'''でなけれなりません(chmod: {{ic|400}})。ここでは、所有者は root で
   
# chown root:root /'''PATH_FILE'''/'''SECRET_KEY_FILES'''
+
$ chown root:root /''path_file''/''secret_key_files''
# chmod 400 /'''PATH_FILE'''/'''SECRET_KEY_FILES'''
+
$ chmod 400 /''path_file''/''secret_key_files''
   
  +
=== コード生成 ===
==デスクトップログイン==
 
Google Authenticator の PAM プラグインをコンソールのログインや [[GDM]] のログインで使うこともできます。以下を {{ic|/etc/pam.d/login}} または {{ic|/etc/pam.d/gdm-password}} ファイルに追加してください:
 
   
  +
最終設定ステップでは、各ユーザーがホームディレクトリに生成されたシークレットキーのファイルを選択した OTP ジェネレーターに関連付けて認証コードを生成する必要があります。冗長性のために、モバイル端末の OTP アプリケーションや別のパスワードマネージャーなど、複数のデバイスでジェネレーターを設定したり、以前に生成した緊急スクラッチコードをバックアップとして利用することもできます。
auth required pam_google_authenticator.so
 
   
  +
==== モバイル端末ジェネレーター ====
== コードの生成 ==
 
   
OTP 生成アプリケーションをインストールした機器を紛失した場合、Google Authenticator が設定されたシステムにログインすることができなくなります
+
携帯電話にジェネレーターアプリケーションをインストールします(例):
別の手段で生成したコードを使用することで、機器を紛失した場合でもログインすることができるようになります。
 
   
  +
* Android 用 [[Wikipedia:FreeOTP|FreeOTP]]([https://f-droid.org/packages/org.fedorahosted.freeotp/ F-Droid]、[https://play.google.com/store/apps/details?id=org.fedorahosted.freeotp Google Play])または iOS 用([https://itunes.apple.com/app/freeotp-authenticator/id872559395 App Store])。
=== コードの管理 ===
 
  +
* Android 用 '''FreeOTP+'''([https://f-droid.org/packages/org.liberty.android.freeotpplus/ F-Droid]、[https://play.google.com/store/apps/details?id=org.liberty.android.freeotpplus Google Play])。
  +
* Android 用 '''Aegis'''([https://f-droid.org/packages/com.beemdevelopment.aegis/ F-Droid]、[https://play.google.com/store/apps/details?id=com.beemdevelopment.aegis Google Play])。
  +
* Android 用 [[Wikipedia:Bitwarden|Bitwarden]]([https://mobileapp.bitwarden.com/fdroid/ F-Droid]、[https://play.google.com/store/apps/details?id=com.x8bit.bitwarden Google Play])または iOS 用([https://apps.apple.com/app/bitwarden-password-manager/id1137397744 App Store])。
  +
* Android 用 [[Wikipedia:Google Authenticator|Google Authenticator]]([https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2 Google Play])または iOS 用([https://itunes.apple.com/app/google-authenticator/id388497605 App Store])。
   
  +
モバイルアプリケーションで新しいアカウントを作成し、シークレットキーのファイル生成時に表示された URL から QR コードをスキャンするか、シークレットキー(上記の例では「ZVZG5UZU4D7MY4DH」)を手動で入力します。
{{AUR|gashell}}を使用することで、google authenticatorのコードを表示、作成、管理することが出来ます。代わりに、{{AUR|auther-git}}を使うことも出来ます。
 
   
  +
これで、携帯電話に 30 秒ごとに新しいパスコードトークンが生成されるはずです。
=== コマンドライン ===
 
   
  +
Google Authenticator を他のシステムでも設定している場合、デバイスを紛失するとそれらのシステムにログインできなくなることがあります。コードを生成する追加の方法を持つことが役立ちます。
{{ic|oath-tool}}を使えば、簡単にコードを作成できます。{{Pkg|oath-toolkit}}パッケージをインストールして、以下のコマンドを実行すれば作成できます。秘密鍵は、{{ic|google-authenticator}}を実行した際に出力されるsecret key (上の例では 'ZVZG5UZU4D7MY4DH')になります。また、{{ic|~/.google_authenticator}} でも確認することが出来ます。
 
   
  +
==== コードマネージャー ====
$ oathtool --totp -b ''secret key''
 
   
  +
Google Authenticator コードの表示、生成、保存、管理を可能にするスクリプトは {{AUR|gashell}} によって提供されています。代替オプションとして {{AUR|auther-git}} もあります。
殆どのAndroidシステムでは、適切なユーザーであればGoogle Authenticator データベースをコピーしたり、sqlite3に直接アクセスすることができます。
 
  +
下記のスクリプトは、Google Authenticatorデータベースを読み出して、見つかった秘密鍵ごとにコードを生成します。
 
  +
==== KeePassXC ====
  +
  +
GUI パスワードマネージャー {{Pkg|keepassxc}} では、Google Authenticator コードをエントリに関連付け、その後 OTP コードを生成して QR コード経由でキーをエクスポートすることができます。
  +
  +
==== コマンドライン ====
  +
  +
最も簡単なコード生成方法は {{man|1|oathtool}} です。これは {{Pkg|oath-toolkit}} パッケージに含まれており、次のように使用できます。
  +
  +
$ oathtool --totp --base32 ''secret_key''
  +
  +
ほとんどの Android システムでは、Google Authenticator データベースをデバイスからコピーして直接アクセスできます。このデータベースは [[SQLite]] データベースです。ただし、[https://gist.github.com/jbinto/8876658 2022年7月のある時点] で、アカウントテーブルのシークレット列で暗号化が使用され始めました。データベースバックアップでこの暗号化が使用されていない場合、このシェルスクリプトを使用して Google Authenticator データベースを読み取り、見つかった各キーのライブコードを生成できます。
   
 
{{hc|google-authenticator.sh|2=
 
{{hc|google-authenticator.sh|2=
 
#!/bin/sh
 
#!/bin/sh
   
# This is the path to the Google Authenticator app file. It's typically located
+
# これは Google Authenticator アプリファイルへのパスです。通常は
  +
# Android の /data にあります。PC にコピーして安全な場所に保管し、
# in /data under Android. Copy it to your PC in a safe location and specify the
 
  +
# ここにそのパスを指定します。
# path to it here.
 
 
DB="/path/to/com.google.android.apps.authenticator/databases/databases"
 
DB="/path/to/com.google.android.apps.authenticator/databases/databases"
   
158行目: 200行目:
 
done
 
done
 
}}
 
}}
  +
  +
=== テスト ===
  +
  +
別のマシンや別のターミナルウィンドウからホストに SSH 接続します。
  +
  +
$ ssh ''hostname''
  +
login as: ''username''
  +
Verification code: ''generated/backup_code''
  +
Password: ''password''
  +
$
  +
  +
{{TranslationStatus|Google Authenticator|2024-09-05|811688}}

2024年9月5日 (木) 21:19時点における最新版

関連記事

Google Authenticator は、ワンタイムパスコード (OTP) を使用した二段階認証手続きを提供します。この認証方式は、オープン認証イニシアチブ(OATH) によって最初に標準化されました。認証メカニズムは Linux の PAM システムに統合されています。本ガイドでは、このメカニズムのインストールと設定方法を紹介します。

逆の操作(Linux 上で Google Authenticator 互換のコードを生成する方法)については、以下のコード生成を参照してください。

インストール

クライアントプログラム google-authenticator(1) と PAM モジュール pam_google_authenticator.so を提供する libpam-google-authenticator パッケージをインストールします。開発版は google-authenticator-libpam-gitAUR で利用可能です。

設定

このセクションでは、システムの PAM を設定して、SSH およびオプションでデスクトップログインに対して Google Authenticator の OTP 認証を要求する方法を説明します。

ノート: 完全な設定を行うには、シークレットキーのファイルを生成する必要があります。PAM 設定を編集(適用)する前に生成することを検討してください。

SSH

警告: すべての設定を SSH 経由で行う場合、すべてが正常に動作することを確認するまでセッションを閉じないでください。さもないと、ログインできなくなる可能性があります。

通常、リモートログインに対してのみ 2 パス認証を要求します。対応する PAM 設定ファイルは /etc/pam.d/sshd です。Google Authenticator を全体に適用したい場合は、/etc/pam.d/system-auth を変更する必要がありますが、この場合は非常に慎重に進め、ログインできなくなることを避けてください。 このガイドでは、最も安全な(必ずしも必要ではありませんが)ローカルセッションで /etc/pam.d/sshd を編集します。

UNIX パスワードと OTP の両方を入力するには、pam_google_authenticator.so/etc/pam.d/sshd 内の system-remote-login 行の上に追加します。

auth            required        pam_google_authenticator.so
auth            include         system-remote-login
account         include         system-remote-login
password        include         system-remote-login
session         include         system-remote-login

これにより、OTP が要求された後に UNIX パスワードの入力が求められます。これらのモジュールの順序を変更すると、順序が逆になります。

警告: シークレットキーのファイル(下記参照)を生成しているユーザーのみが SSH を使用してログインできます。

OTP または UNIX パスワードのいずれかでログインを許可するには、次のようにします。

auth            sufficient      pam_google_authenticator.so

/etc/ssh/sshd_config.d/99-archlinux.conf でキーボード対話型認証を有効にします。

KbdInteractiveAuthentication yes

最後に、sshd.serviceリロードします。

警告: SSH キーペアで認証し、パスワードログインを無効化している場合、OpenSSH はこれを無視します。ただし、OpenSSH 6.2 以降では、AuthenticationMethods を追加して、二要素認証と鍵ベースの認証の両方を許可できます。詳細は OpenSSH#二要素認証と公開鍵を参照してください。

ローカルネットワーク外から接続したときのみ OTP を要求する

場合によっては、ローカルネットワーク外から接続するときにのみ 2FA 機能を有効にしたいことがあります。 これを実現するには、ファイル(例: /etc/security/access-local.conf)を作成し、2FA をバイパスできるネットワークを追加します。

# ローカル IP 範囲のみ許可
+ : ALL : 192.168.20.0/24
# 追加ネットワーク: VPN トンネル IP 範囲(ある場合)
+ : ALL : 10.8.0.0/24
+ : ALL : LOCAL
- : ALL : ALL

次に、/etc/pam.d/sshd を編集し、以下の行を追加します。

#%PAM-1.0
auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-local.conf
auth      required  pam_google_authenticator.so
auth      include   system-remote-login
account   include   system-remote-login
password  include   system-remote-login
session   include   system-remote-login

デスクトップログイン

警告: 結果を別のコンソールでテストするまで、セッションからログアウトしないでください。

Google Authenticator の PAM プラグインは、コンソールログインや GDM にも使用できます。/etc/pam.d/login または /etc/pam.d/gdm-password ファイルに次の行を追加するだけです。

auth required pam_google_authenticator.so

使用方法

2 パス認証を使用したいユーザーは、それぞれ

  • ホームフォルダにシークレットキーのファイルを生成し、
  • 対応する OTP ジェネレーターを設定する必要があります。

シークレットキーのファイル生成

ヒント: スキャン可能な QR コードを生成するために qrencodeインストールしてください。QR コードを認証アプリでスキャンすると、自動的にキーが設定されます。

google-authenticator は以下のように TOTP シークレットキーのファイルを生成します。

$ google-authenticator
Do you want authentication tokens to be time-based (y/n) y
generated_QR_code_here
Your new secret key is: ZVZG5UZU4D7MY4DH
Your verification code is 269371
Your emergency scratch codes are:
  70058954
  97277505
  99684896
  56514332
  82717798

Do you want me to update your "/home/username/.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) n

If the computer that you are logging into is not 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

緊急スクラッチコードを安全に保管することをお勧めします(印刷して安全な場所に保管してください)。これらは、携帯電話(OTP ジェネレーター)を紛失した場合、SSH を介してログインする唯一の方法です。また、~/.google_authenticator にも保存されているため、ログインしている限りいつでも確認できます。

保存場所

シークレットキーのファイル保存パスを変更したい場合は、--secret フラグを使用します。

$ google-authenticator --secret="/path_folder/username"

その後、PAM の場所パスを /etc/pam.d/sshd で忘れずに変更します。

/etc/pam.d/sshd
auth required pam_google_authenticator.so user=root secret=/path_folder/${USER}

user=root は、PAM に root ユーザーを使用してファイルを検索させるために使用されます。

また、シークレットキーのファイルのパーミッションに注意してください。ファイルは所有者のみ読み取り可能でなければなりません(chmod: 400)。ここでは、所有者は root です。

$ chown root:root /path_file/secret_key_files
$ chmod 400 /path_file/secret_key_files

コード生成

最終設定ステップでは、各ユーザーがホームディレクトリに生成されたシークレットキーのファイルを選択した OTP ジェネレーターに関連付けて認証コードを生成する必要があります。冗長性のために、モバイル端末の OTP アプリケーションや別のパスワードマネージャーなど、複数のデバイスでジェネレーターを設定したり、以前に生成した緊急スクラッチコードをバックアップとして利用することもできます。

モバイル端末ジェネレーター

携帯電話にジェネレーターアプリケーションをインストールします(例):

モバイルアプリケーションで新しいアカウントを作成し、シークレットキーのファイル生成時に表示された URL から QR コードをスキャンするか、シークレットキー(上記の例では「ZVZG5UZU4D7MY4DH」)を手動で入力します。

これで、携帯電話に 30 秒ごとに新しいパスコードトークンが生成されるはずです。

Google Authenticator を他のシステムでも設定している場合、デバイスを紛失するとそれらのシステムにログインできなくなることがあります。コードを生成する追加の方法を持つことが役立ちます。

コードマネージャー

Google Authenticator コードの表示、生成、保存、管理を可能にするスクリプトは gashellAUR によって提供されています。代替オプションとして auther-gitAUR もあります。

KeePassXC

GUI パスワードマネージャー keepassxc では、Google Authenticator コードをエントリに関連付け、その後 OTP コードを生成して QR コード経由でキーをエクスポートすることができます。

コマンドライン

最も簡単なコード生成方法は oathtool(1) です。これは oath-toolkit パッケージに含まれており、次のように使用できます。

$ oathtool --totp --base32 secret_key

ほとんどの Android システムでは、Google Authenticator データベースをデバイスからコピーして直接アクセスできます。このデータベースは SQLite データベースです。ただし、2022年7月のある時点 で、アカウントテーブルのシークレット列で暗号化が使用され始めました。データベースバックアップでこの暗号化が使用されていない場合、このシェルスクリプトを使用して Google Authenticator データベースを読み取り、見つかった各キーのライブコードを生成できます。

google-authenticator.sh
#!/bin/sh

# これは Google Authenticator アプリファイルへのパスです。通常は
# Android の /data にあります。PC にコピーして安全な場所に保管し、
# ここにそのパスを指定します。
DB="/path/to/com.google.android.apps.authenticator/databases/databases"

sqlite3 "$DB" 'SELECT email,secret FROM accounts;' | while read A
do
        NAME=`echo "$A" | cut -d '|' -f 1`
        KEY=`echo "$A" | cut -d '|' -f 2`
        CODE=`oathtool --totp -b "$KEY"`
        echo -e "\e[1;32m$CODE\e[0m - \e[1;33m$NAME\e[0m"
done

テスト

別のマシンや別のターミナルウィンドウからホストに SSH 接続します。

$ ssh hostname
login as: username
Verification code: generated/backup_code
Password: password
$
翻訳ステータス: このページは en:Google Authenticator の翻訳バージョンです。最後の翻訳日は 2024-09-05 です。もし英語版に 変更 があれば、翻訳の同期を手伝うことができます。