[debian] Debian GNU/Linux に Google Authenticator 認証でログインする
RSA SecurIDソフトウェアトークンのように、ワンタイムパスワード生成機の所有者のみにSSHログインを許可するための設定。できるだけセキュアにしたいVPCや社内ネットワークのゲートウェイサーバに設定することを想定した設定のメモです。
Google Authenticatorで2段階認証を行う [Debian]
とUse Google Authenticator to login to a Linux PCが非常に参考になりました。特に後者の記事は丁寧ですばらしいです。
google-authenticator のインストール
Google code からコードをダウンロードする
sudo apt-get install mercurial subversion cd ~/; hg clone https://code.google.com/p/google-authenticator/ cd google-authenticator/libpam
google-authenticator のビルド。libpam0g-dev がちゃんとインストールされていれば一発でmakeが通ると思います。無事ビルドできたらインストール。
sudo apt-get install make gcc libpam0g-dev libqrencode3
cd ~/google-authenticator/libpam
make
sudo make install
PAMの設定
/usr/share/pam-configs/ 以下にPAMの設定を作成する。こちらの設定のまま、>/usr/share/pam-configs/google-all と /usr/share/pam-configs/google-enough を作成。
PAMについては、http://d.hatena.ne.jp/int128/20090726/1248622071 が参考になりました。 今回の場合、既存の認証にgoogle-authenticatorを追加したいため、required である、google-all を利用することにします。 以下のコマンドを実行し、google-all を選択。
sudo pam-auth-update
SSHの設定
以下の設定を変更。ChallengeResponseAuthentication 以外の認証をnoにして、google-authenticator 以外でのログインを許可しないようにする。
ChallengeResponseAuthentication と UsePAM は、google-authenticator を利用するために必要。
PubkeyAuthentication no PasswordAuthentication no RhostsRSAAuthentication no RSAAuthentication no ChallengeResponseAuthentication yes UsePAM yes
sshd の reload を忘れずに。この際、万が一設定をミスってログインできなくならないように、sudo su - になったターミナルをふたつほど開いておくと良いと思います。
sudo /etc/init.d/ssh reload
google-authenticator でキーを作成
$ google-authenticator <QRCODEへのURL> [ ] [ QRコード ] [ ] Your new secret key is: 4M2HNEG3DC42XXXX Your verification code is 5458*** Your emergency scratch codes are: 949987** 942234** 296841** 456148** 794053** Do you want me to update your "/home/<your name>/.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) n 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 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
上記でサーバー側の設定は完了です。
Google Authenticator のインストール
- iPhone の App Store で Google Authenticator をダウンロード
- アプリを起動し、右下の「+」で、設定を追加
- 画面下の「バーコードをスキャン」でサーバ側設定の最後で表示したバーコードを読み込む。
これで設定は完了。
SSHクライアントでログイン
ログインしてみると Verification を聞かれるようになる。
$ ssh localhost Verification code: [Google Authenticator に表示されているワンタイムパスワードを入力] Password: [UNIXパスワードを入力]