5
2

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.

Windows 10, 11 標準搭載のSSHクライアントの俺的ベストプラクティス

Last updated at Posted at 2021-02-09

自分の環境再現手順メモ

Windows 10 標準搭載のSSHクライアントで鍵生成 + ssh-agent

GitHubのドキュメントとかはGit Bash使う手順が書いてあるが、正直トラブルの元なので素直にWindows標準のを使う。バージョンが古い?動くことが大事なんだよ。

前提

Windows 10 Pro 64bit 20H2, 21H1
Windows 11 Pro 64bit 21H2, 22H2

とりあえず最新にしていれば再現するんじゃね?最近セットアップした環境なら標準でOpenSSH クライアントが入っていた。

操作

PowerShellを立ち上げる

Winキー → powershell → enter

image.png

ちゃんとこれ↑を立ち上げる。

鍵を生成する

ssh-keygen -t ecdsa -b 521 -C "Comment"

image.png

コメントはわかりやすい文字列に変える。きちんとパスフレーズ(passphrase)をかける。

ここでどんな鍵アルゴリズムを使うかは人によって分かれるかもしれない。接続先にCentOS 6のOpenSSHがある人は Ed25519 アルゴリズムの鍵で認証を行うことができないはずである。一方、RSAの鍵はちょっと前の OpenSSH Agent でバグるという厄介な問題があった。今は治っているらしい?各々の環境で調整しましょう。

管理者権限のPowerShellを立ち上げる

Start-Process -Verb runas powershell

UAC確認ダイアログが出るので、許可する。

SSH Agentの自動起動と起動

Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service -PassThru | Select-Object -Property Name, StartType, Status

image.png

どう書くかは趣味が分かれるがサクッと1行が楽だろう。
Runningなのが確認できたら exit で閉じて先の通常権限のPowerShellに戻る。

SSH Agentに鍵を追加

ssh-add .ssh\id_ecdsa

この手順通りならカレントディレクトリは %USERPROFILE% のはず。違ったら適宜調整。パスフレーズを聞かれるので、 ssh-keygen で入れたパスフレーズを答える。

image.png

追加 : Git for Windows向け

Windowsのssh-agent周りの事情は歴史的経緯でかなり複雑となっている( https://qiita.com/slotport/items/e1d5a5dbd3aa7c6a2a24 をご参考に)が、「最もWindowsらしいソースコード」( https://github.com/PowerShell/openssh-portable )で動いており、きちんとWindows Serviceとして組み込まれている Win32-OpenSSH を基礎に据えるのが妥当と思い構築している。

問題はMSYS2をベースにしているGit for Windowsの存在だが、単にGitのバックエンドとなるSSHの実装を環境変数で指示すればいいだけなので、以下のコマンドを実行すれば良い。

[Environment]::SetEnvironmentVariable('GIT_SSH', 'C:\Windows\System32\OpenSSH\ssh.exe', 'User')

image.png

↓PowerShell立ち上げ直し

image.png

これで快適なSSHライフが送れます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?