「Google Authenticator」の版間の差分
Kusanaginoturugi (トーク | 投稿記録) →PAM の設定: 更新 |
Kusanaginoturugi (トーク | 投稿記録) 古い記事を削除 |
||
| 84行目: | 84行目: | ||
auth required pam_google_authenticator.so |
auth required pam_google_authenticator.so |
||
==秘密鍵ファイルの生成== |
|||
{{Tip|QR コードを生成するには {{Pkg|qrencode}} をインストールします。認証アプリで QR コードをスキャンすれば自動的に鍵が設定されます。}} |
|||
2段階認証を使用したい場合はユーザーのホームフォルダに秘密鍵ファイルを生成する必要があります。''google-authenticator'' を使うことでとても簡単に生成することができます: |
|||
$ google-authenticator |
|||
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 |
|||
緊急時のスクラッチコードを安全なところに保管しておくことを推奨します (例: コードを印刷して金庫にしまう)。携帯電話 (つまり OTP 生成ツール) をなくしてしまったときの唯一の (SSH による) ログイン手段となります。コードは {{ic|~/.google_authenticator}} にも保存されるので、ログインしている間はいつでも確認することが可能です。 |
|||
==OTP 生成ツールの設定== |
|||
下記から携帯電話に生成ツールをインストールしてください。 |
|||
* '''FreeOTP''' for [https://play.google.com/store/apps/details?id=org.fedorahosted.freeotp Android]/[https://itunes.apple.com/es/app/freeotp-authenticator/id872559395 iOS]. |
|||
* '''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]. |
|||
アプリケーションメニューから適切なボタンをクリックして新しいアカウントを作成し、秘密鍵ファイルを生成したときに表示される URL から QR コードをスキャンするか、秘密鍵を手動で入力します (上の例では 'ZVZG5UZU4D7MY4DH')。 |
|||
これで30秒ごとに新しく生成されるパスコードトークンをあなたの携帯で見ることができます。 |
|||
==テスト== |
|||
他のマシンや他のターミナルウィンドウからホストに SSH で接続して認証システムが動作しているか確認してください: |
|||
$ ssh hostname |
|||
login as: <username> |
|||
Verification code: <generated/backup-code> |
|||
Password: <password> |
|||
$ |
|||
==秘密鍵の保存場所== |
|||
秘密鍵ファイルの置き場所を変更したい場合、{{ic|--secret}} フラグを使います: |
|||
$ google-authenticator --secret="/'''PATH_FOLDER'''/'''USERNAME'''" |
|||
{{ic|/etc/pam.d/sshd}} を編集して PAM の設定も忘れずに変更してください: |
|||
{{hc|/etc/pam.d/sshd|2= |
|||
auth required pam_google_authenticator.so user=root secret=/'''PATH_FOLDER'''/${USER} |
|||
}} |
|||
{{ic|user=root}} を使うことで PAM は root ユーザーでファイルを検索します。 |
|||
また、秘密鍵ファイルのパーミッションに注意してください。所有者だけがファイルを読み込めるように設定してください。例えば root を所有者とする場合: |
|||
# chown root:root /'''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}} ファイルに追加してください: |
|||
auth required pam_google_authenticator.so |
|||
== コードの生成 == |
|||
OTP 生成アプリケーションをインストールした機器を紛失した場合、Google Authenticator が設定されたシステムにログインすることができなくなります。 |
|||
別の手段で生成したコードを使用することで、機器を紛失した場合でもログインすることができるようになります。 |
|||
=== コードの管理 === |
|||
{{AUR|gashell}}を使用することで、google authenticatorのコードを表示、作成、管理することができます。代わりに、{{AUR|auther-git}}を使うこともできます。 |
|||
=== コマンドライン === |
|||
{{ic|oath-tool}}を使えば、簡単にコードを作成できます。{{Pkg|oath-toolkit}}パッケージをインストールして、以下のコマンドを実行すれば作成できます。秘密鍵は、{{ic|google-authenticator}}を実行した際に出力されるsecret key (上の例では 'ZVZG5UZU4D7MY4DH')になります。また、{{ic|~/.google_authenticator}} でも確認することができます。 |
|||
$ oathtool --totp -b ''secret key'' |
|||
殆どのAndroidシステムでは、適切なユーザーであればGoogle Authenticator データベースをコピーしたり、sqlite3に直接アクセスすることができます。 |
|||
下記のスクリプトは、Google Authenticatorデータベースを読み出して、見つかった秘密鍵ごとにコードを生成します。 |
|||
{{hc|google-authenticator.sh|2= |
|||
#!/bin/sh |
|||
# This is the path to the Google Authenticator app file. It's typically located |
|||
# 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" |
|||
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 |
|||
}} |
|||
2024年9月5日 (木) 18:27時点における版
関連記事
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 認証を要求する方法を説明します。
SSH
通常、リモートログインに対してのみ二要素認証を要求します。対応する 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 パスワードの入力が求められます。これらのモジュールの順序を変更すると、順序が逆になります。
OTP または UNIX パスワードのいずれかでログインを許可するには、次のようにします。
auth sufficient pam_google_authenticator.so
/etc/ssh/sshd_config.d/99-archlinux.conf でキーボードインタラクティブ認証を有効にします。
KbdInteractiveAuthentication yes
最後に、sshd.service をリロードします。
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