Skip to content

SSH with GPG via Yubikey⚓︎

Establish authentication with GPG between a Windows client and a Linux server via yubikey.

Requirements⚓︎

  • Yubikey with OpenPGP capability
  • Gpg4win - GPG agent for Windows
  • kleopatra - Graphical interface for key management
  • [optional] Pageant if you want to use PuTTY - PuTTY's agent
  • SSH server with configued firewall

Sources⚓︎

Guide⚓︎

Key management on Windows⚓︎

Generate a key
:: Arguments: name, algorythm, use, expiration
gpg --quick-generate-key name@address.com ed25519 auth never

Do NOT choose to save the changes

It will destroy the key on your computer, and it can't be retrieved from the Yubikey.

Copy the key to the Yubikey
gpg --edit-key name@address.com
keytocard

:: Enter Q to exit
:: Do NOT save when prompted to, it will destroy the key on your computer !

We need to export the public key in SSH format in order to store it on the server. The easiest way is to use Kleopatra.
Open Kleopatra. The generated key should appear on the main tab. If it isn't there, click on Certificates at the top and Refresh certificates. Double click on the certificate and, in the newly opened window, click on the Subkeys tab. There, you can right-click on the key to export it as an OpenSSH key.

Lastly, the GPG agent needs to be restarted to take the key into account. You can put the following in the C:\Users\<user>\AppData\Roaming\Microsoft\Windows\Start Menu\ folder so it would be executed on login.

GPG_Starter.bat
1
2
3
4
:: Stop existing GPG instances
gpg-connect-agent "KILLAGENT" /bye
:: Launch new instance
gpg-connect-agent /bye

Here is another version that launches PuTTY:

launcher.bat
1
2
3
4
5
6
:: Stop existing GPG instances
gpg-connect-agent "KILLAGENT" /bye
:: Launch new instance
gpg-connect-agent /bye
:: Launch PuTTY. "start" allows the script to close immediatly.
start putty.exe

PuTTY configuration

If you're using PuTTY, make sure that Attempt authentication using Pageant is checked in Connection > SSH > Auth

Key management on Linux⚓︎

In your session, paste the previously exported key on a new line in .ssh/authorized_keys.

Debug⚓︎

If needed, logs can be viewed rapidly with the following command:

journalctl -u ssh -S "year-mm-dd hh:mm:ss"

Verbosity

Log verbosity can be enhanced by tweaking the following line:

/etc/ssh/sshd_config
# Logging
#SyslogFacility AUTH
LogLevel DEBUG3