Using GPG Command-Line Tools in WSL2 and Signing Git Commits

Dec 31, 2024 at 03:08:59

Most of the methods found online involve using npiperelay to map the Gpg4win GPG Agent's socket into WSL2. However, a simpler approach is to use gpg.exe directly within WSL2.

If you want to sign Git commits but your private key is stored on a smart card, you can directly set it with:
git config --global gpg.program gpg.exe
(If GPG is not in Windows' PATH, you need to use the absolute path, which is usually /mnt/c/Program Files (x86)/GnuPG/bin/gpg.exe. For convenience, it's better to add it to PATH. After doing so, open a new terminal.)

Unless a program requires direct GPG Agent access or does not support custom GPG binaries, this approach is far more convenient than mapping the GPG Agent socket.


Translated from my Chinese blog post: 在 WSL2 中使用 GPG 命令行工具以及对 Git commit 进行签名