LoginSignup
6
7

More than 3 years have passed since last update.

VPS(Ubuntu)のログインにGoogle 2段階認証プロセス(2FA)を使う方法

Last updated at Posted at 2019-02-06

環境

Conoha VPS(Ubuntu 18.04)

方法

以下、root権限を持ったアカウントで作業してください。

1:UbuntuにGoogle 2FAを入れる

sudo apt-get install libpam-google-authenticator

2:設定を進める

1で2段階認証プロセスが無事入ったことが確認できたら、次に以下を打ち込み、設定を進めましょう。

google-authenticator

3:詳細設定

そうすると、以下のように質問形式で設定を進めていくようになりますので、自分の要件に満たす回答を選んでください。

Do you want authentication tokens to be time-based (y/n):fist_tone4: y
(時間に基づいた認証トークンを使いたいですか?)

これをYESと答えると、コンソール上に巨大なQRコードが出てくると思います。
フルスクリーンモードにしないと見えないかもしれませんが、これをスマートフォンのAuthenticatorのアプリで読み込むと、自動でトークンが発行されます。
Authenticatorのアプリをお持ちでない場合には、こちらからダウンロードできます。
iOS
Android
次に、以下のような質問が出てくると思います。
これはセキリュティ上NOにする理由がないので、これもYESにしましょう。

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
(同じトークンで複数のユーザーがログインするのを防ぐ)

次に、トークンの有効期限延長について聞かれますが、これはNoでいいと思います。

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

最後の質問です。
こちらはブルートフォースアタックを防ぐための設定です。
30秒間にログイン試行を3回までに限定できますので、これはYESにしましょう。

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

4:Google 2段階認証プロセスを有効にする

以上の通りに設定が終わると、次に2段階認証を有効にしましょう。

/etc/pam.d/sshd
auth required pam_google_authenticator.so (初めの行に追記)

次に、/etc/ssh/sshd_config  ファイルを編集します。

ChallengeResponseAuthentication yes (コメントアウトを削除)

ここまでで全てのファイルが設定完了となりますので、設定を再読み込みしましょう。

sudo systemctl restart sshd

そして、実際にログインしようとすると、以下のようにトークンが求められるようになるので、スマホの画面に表示されているトークンを入力しましょう。

Verification code:

お疲れ様でした。  
✳︎こちらの記事を参考にしました。
"SSH two-factor authentication: How to enable on your VPS"
https://blog.ssdnodes.com/blog/ssh-two-factor-authentication-vps/

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7