0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gpg4winとGit for Windowsでgithubを使う③:WSLとdevcontainer

Last updated at Posted at 2025-08-13

はじめに

WSL2から,gpg4winのgpg鍵(とssh鍵)へアクセスできる環境を作ります。
github はあまり関係ないです... すみません

ssh鍵

ssh クライアントは SSH_AUTH_SOCK 環境変数で指定されるunixソケットを使って ssh-agent にアクセスします。
wsl2-ssh-pagent を使ってこのソケットを作り,アクセスを gpg4win の pagent につなぎます。

wsl2-ssh-pagent.exe はどこにおいてもいいですが,私はホームディレクトリの.ssh配下に置いています。

.bash_profile に以下の設定を書きます:

.bash_profile
if ! ss -a | grep -q "$SSH_AUTH_SOCK"; then
  rm -f "$SSH_AUTH_SOCK"
  wsl2_ssh_pageant_bin="$HOME/.ssh/wsl2-ssh-pageant.exe"
  (setsid nohup socat UNIX-LISTEN:"$SSH_AUTH_SOCK,fork" EXEC:"$wsl2_ssh_pageant_bin" >/dev/null 2>&1 &)
  unset wsl2_ssh_pageant_bin
fi

wsl --shutdown してからもう一度WSLに入ると,ssh-add -L でWindows側のssh鍵が見えるはずです。

gpg鍵

まず,WSL2側でgpg-agent が起動することを止める必要があります(gpgから/systemdから)。
gpgからgpg-agentが起動されることは以下の設定ファイルで止められます:

.gnupg/gpg.conf
no-autostart

systemd が gpg-agent を起動することは以下のコマンドで止められます:

$ systemctl --user mask gpg-agent.service gpg-agent.socket gpg-agent-ssh.socket gpg-agent-extra.socket gpg-agent-browser.socket

次に,WSLで gpg-agent へのアクセスがあったら,これを gpg4win に転送する必要があります。これは wsl-relay で可能です。wsl-relay は gpg4win の"ソケット"(実際はただのファイルで%LocalAppData%\gnupg以下にある)を読み取り,WSLからの入力を実際のtcpポートにつないでくれます。

wsl-relay を コンパイルしてWSLからアクセスできる場所に置きます。私はホームディレクトリの.gnupgに置いています。

.bash_profile に以下の設定を書きます:

.bash_profile
gpg_agent_sock=$(gpgconf --list-dir | grep -oP '(?<=agent-socket:).+')
gpg_agent_extra_sock=$(gpgconf --list-dir agent-extra-socket)
if ! ss -a | grep -q "$gpg_agent_sock"; then
  rm -f "$gpg_agent_sock"
  rm -f "$gpg_agent_extra_sock"
  wsl_relay_bin="$HOME/.gnupg/wsl-relay.exe"
  (setsid nohup socat UNIX-LISTEN:$gpg_agent_sock,fork EXEC:"$wsl_relay_bin --input-closes --pipe-closes --gpg" >/dev/null 2>&1 &)
  ln -sf $gpg_agent_sock $gpg_agent_extra_sock
fi

なお,gpgがgpg-agentと通信するためのunixソケットの場所はgpgconfでわかるので,上のスクリプトはそれを用いています。
gpg_agent_extra_socket は devcontainer 用にいるかも... と思ったのですが,ホストがwindows なので不要でした(あってもなくても関係ない)。

最後に,Windows側のgpg公開鍵をWSL側に登録する必要があります(公開鍵がないとそもそもgpg-agentに聞きにいかない)。
Windows で以下のコマンドを投入します。

> gpg --export -a [鍵のID] > publickey

WSL で以下のコマンドを投入します。

$ gpg --import < publickey

WSL側で gpg -K とすると秘密鍵のエントリが表示され,gpg4winと通信できていることが確認できるはずです。

上手くいかないときは,gpg に --debug-level guru --debug-all を付けて動かしましょう(gpg -bsau 鍵ID -v -v --debug-level guru --debug-all など)。

devcontainer

ここまで作業すれば,devcontainer では特に作業が要らずWSL環境と同じようにssh/gpgが使えます。

  • ssh は SSH_AUTH_SOCK を vscode がマップしてくれる
  • gpg は vscode がホストの設定を使ってくれる(詳細 ※未チェック)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?