DEV Community

loading...
Cover image for The ultimate guide to Yubikey on WSL2 [Part 2]

The ultimate guide to Yubikey on WSL2 [Part 2]

dzerycz profile image Jaroslav Živný Originally published at jardazivny.Medium Updated on ・3 min read

In the Previous part we configured OpenGPG with Yubikey. In case you have it done, we can continue on how to access your YubiKey in WSL2.

Disclaimer: This tutorial is written for WSL2 with Ubuntu. It may differ distro from distro.


Access your YubiKey in WSL2

Prerequisites

Install socat and wsl2-ssh-pageant in WSL:

# WSL2
$ sudo apt install socat
$ mkdir ~/.ssh
$ wget https://github.com/BlackReloaded/wsl2-ssh-pageant/releases/download/v1.2.0/wsl2-ssh-pageant.exe -O ~/.ssh/wsl2-ssh-pageant.exe
$ chmod +x ~/.ssh/wsl2-ssh-pageant.exe
Enter fullscreen mode Exit fullscreen mode

Sync sockets

This part is inspired by this tutorial.

Edit your ~/.bashrc (e.g. via nano or vim) and add following content:

# SSH Socket
# Removing Linux SSH socket and replacing it by link to wsl2-ssh-pageant socket
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock 
ss -a | grep -q $SSH_AUTH_SOCK 
if [ $? -ne 0 ]; then
  rm -f $SSH_AUTH_SOCK
  setsid nohup socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:$HOME/.ssh/wsl2-ssh-pageant.exe &>/dev/null &
fi
# GPG Socket
# Removing Linux GPG Agent socket and replacing it by link to wsl2-ssh-pageant GPG socket
export GPG_AGENT_SOCK=$HOME/.gnupg/S.gpg-agent 
ss -a | grep -q $GPG_AGENT_SOCK 
if [ $? -ne 0 ]; then
  rm -rf $GPG_AGENT_SOCK
  setsid nohup socat UNIX-LISTEN:$GPG_AGENT_SOCK,fork EXEC:"$HOME/.ssh/wsl2-ssh-pageant.exe --gpg S.gpg-agent" &>/dev/null &
fi
Enter fullscreen mode Exit fullscreen mode

Restart WSL by running

# CMD
wsl.exe --shutdown
Enter fullscreen mode Exit fullscreen mode

When you open Ubuntu Terminal now and run gpg --card-status you should be able to see something like this:

gpg --card-status

Import GPG key to WSL2

If you check GPG keys availible in WSL2 via gpg --list-keys or gpg --list-secret-keys you get empty results. We have to first import them. It’s quite easy just run:

# WSL2
$ gpg --card-edit
Enter fullscreen mode Exit fullscreen mode

This will open gpg command interface. Just type in fetch. It’ll get you public keys from keys.openpgp.org (we uploaded them there in the previous part

In case you haven’t uploaded the public keys to keys.openpgp.org (as shown in the part 1 of this tutorial). You can import it via asc file (exported in part 1) via:

gpg --import PATH_TO_ASC_FILE

Exit the gpg command interface via quit

If you now run gpg --list-keys you finally get your keys.

gpg --list-keys

Great success!

Now we are missing one small step. As you can see. The trustworthiness of our certificate is unknown (information next to the name). We can change it via running:

# WSL2
$ gpg --edit-key YOUR_KEY_ID # In my case 1E9...
Enter fullscreen mode Exit fullscreen mode

This opens gpg console insterface. Write:

# WSL2
trust # Change trust level
5     # Set trust level to ultimate
save  # Save the changes
Enter fullscreen mode Exit fullscreen mode

If you list keys via gpg --list-keys now. You should be able to see [ultimate] next to your name.

We’ll continue in the part 3.

Discussion (1)

Collapse
jmz0 profile image
James Crace

Thanks for this awesome guide!

Forem Open with the Forem app