5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

OpenSSH 8.2から、U2F/FIDO2デバイスを使用したハードウェア認証がサポートされるようになりました。
これにより、秘密鍵が漏洩しても、鍵を生成したデバイス(YubiKeyなど)がないとSSH接続ができないようないい感じの環境を作ることができるようになりました。

以前でも、OpenPGPやPIVなどを利用する方法がありましたが、設定が少し複雑で厄介でした。
今回のFIDO2デバイスを用いたed25519sk鍵認証は比較的お手軽そうなのでやってみます。

環境

ローカル Windows 11 Pro 23H2
接続先 Ubuntu 22.04.4 LTS

準備

Windowsにデフォルトで入っているOpenSSHでは、バージョンが古く、*-sk鍵に対応していないため、最新のv9.5.0.0p1-Betaを導入します。(8.9.0以上バージョンであれば問題なさそうです)

winget install Microsoft.OpenSSH.Beta

完了するとC:\Program Files\OpenSSHにバイナリが展開されているので、環境変数として追加しておきます。

image.png

鍵の生成

FIDOデバイスが使える鍵には、ecdsa-skとed25519-skがありますが、鍵長はどちらも256bitなので、パフォーマンス上優れているed25519-sk鍵を今回は用います。

ssh-keygen -t ed25519-sk

表示されたダイアログに従いYubiKeyでの認証を済ませます。

image.png

image.png

ちなみに、-O residentオプションを付けるとYubiKey本体に秘密鍵を保存してくれるみたいです。
複数マシンで手軽に共有したい際などには便利ですが、紛失時のリスクもあるため、よく考えて実行しましょう。

公開鍵をUbuntu機(ホスト側)に登録

windows側で以下のコマンドを実行し、鍵を転送します。

scp ~/.ssh/id_ed25519_sk.pub [user]@[host]:home/[user]/

[user][host]は環境に合わせて書き換えてください。

Ubuntu側で以下のコマンドを実行し、鍵を適用します。

mv ./id_ed25519_sk.pub ~/.ssh/authorized_keys

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

/etc/ssh/sshd_configを開き、

sudo vim /etc/ssh/sshd_config

以下の項目を変更することで、パスワードでのログインを無効化します。

PasswordAuthentication no

変更後、サービスを再起動します。

sudo systemctl restart sshd

確認

動作確認します。

ssh -i ~/.ssh/id_ed25519_sk 172.16.10.6

Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-113-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Tue Jul  9 13:06:32 UTC 2024

  System load:           0.68
  Usage of /:            81.8% of 50.41GB
  Memory usage:          14%
  Swap usage:            0%
  Processes:             191
  Users logged in:       1
  IPv4 address for eth0: 172.16.10.6
  IPv6 address for eth0: 2400:4152:6062:a400:be24:11ff:feff:631c

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


Last login: Tue Jul  9 12:16:00 2024
chan-mai@test-sandbox:~$

無事ログインできました。

GitHubのでも使える

SSH and GPG keysから先程生成したed25519-sk鍵を登録することができます。

image.png

authorized_keysへの登録を楽する

ちなみに、GitHubに登録してある公開鍵はhttps://github.com/[username].keys で公開されているので、それを~/.ssh/authorized_keysに書き込んであげることができます。

私の場合はこんな感じ

curl https://github.com/chan-mai.keys >> ~/.ssh/authorized_keys

おわりに

YubiKeyを使い始めてから強く感じているのは、なくさない・忘れないことの大切さです。

YubiKeyが手元にないと、サーバにログインできなくなってしまいます。
そのため、絶対にどこかに置き忘れたり紛失したりしてはいけません。

私の場合、GoogleアカウントやMisskeyのパスキーとしても使用しているので、なおさら重要です。

この記事を読んで「使ってみようかな」と思った方は、ぜひ試してみてください。

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?