LoginSignup
78
56

More than 5 years have passed since last update.

Windows 10のssh-agentをコマンド プロンプト、WSL、Git Bashで使ってみた

Posted at

Windows 10(April 2018 Update)に付属のssh-agentを使用してみました。
コマンド プロンプト(cmd)、WSL、Git Bashで使えることを確認しました。
(WSLはOSSの ssh-agent-wsl が必要)

ssh-agent の有効化

GUIで行う場合

  • サービスOpenSSH Authentication Agentのプロパティを開く。
  • スタートアップの種類を「自動」に変更する。
  • 「開始」をクリックしてサービスを開始する。 service_openssh_property.png

CUIで行う場合

  • コマンド プロンプトを「管理者として実行」する。
  • sc config ssh-agent start=auto
  • sc start ssh-agent

秘密鍵の登録

例としてコマンド プロンプトで行います。WSL、Git Bashで行っても登録先は同じです。
WSLで行う場合は $HOME/.ssh に適切なパーミッションで鍵を用意する必要があります。(ssh-agentを用いない場合と同様)

秘密鍵の例

%USERPROFILE%.ssh\id_ed25519
(例: C:\Users\foo\.ssh\id_ed25519

コマンド例

C:\Users\foo\.ssh> ssh-add id_ed25519
Enter passphrase for C:\Users\foo\.ssh\id_ed25519:
Identity added: C:\Users\foo\.ssh\id_ed25519 (bar@example.com)
C:\Users\foo\.ssh> ssh-add -l
256 SHA256:XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxX bar@example.com (ED25519)

コマンド プロンプトで使用する

普通にsshするだけです。

C:\> ssh bar@example.com

WSLで使用する

ssh-agent-wsl を導入する

https://github.com/rupor-github/ssh-agent-wsl からダウンロードする。
必ず Windows 側に配置する。
(例: C:\local\ssh-agent-wsl)

.bashrc に登録する

.bashrc
eval $(/mnt/c/local/ssh-agent-wsl/ssh-agent-wsl -r)

Git Bashで使用する

Windowsの 環境変数 GIT_SSHC:\Windows\system32\OpenSSH\ssh.exe を設定します。
それ以外に特に準備は必要ありません。

env_git_ssh.png

気になること

コマンド プロンプトから ssh すると、リモートから ssh-add -l を確認できない

$ ssh-add -l
channel 1: chan_shutdown_read: shutdown() failed for fd 8 [i0 o0]: Not a socket
error fetching identities for protocol 1: communication with agent failed
error fetching identities for protocol 2: communication with agent failed
The agent has no identities.

ただし使用はできている模様です。
当投稿には記載していませんが、.ssh/configForwardAgent Yesによる鍵の転送は行えています。(ローカルで ssh-add した鍵をリモートで使用できる。)

パスフレーズの保存先

ssh-addする際に入力したパスフレーズは Windows を再起動しても保存されています。(再入力不要)
これはどこに保存されているのでしょう。
こんな 記事 もあるようですが、ちょっと読み切れませんでした。

ssh-agent-wslの注意書き

SECURITY NOTICE: All the usual security caveats applicable to WSL apply. Most
importantly, all interaction with the Win32 world happens with the credentials
of the user who started the WSL environment. In practice, if you allow someone
else to log in to your WSL environment remotely, they may be able to access the
SSH keys stored in your ssh-agent with ssh-agent-wsl. This is a fundamental
feature of WSL; if you are not sure of what you're doing, do not allow remote
access to your WSL environment (i.e. by starting an SSH server).

78
56
2

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
78
56