12 Mar 2018
Use GPG Key for SSH Authentication with GnuPG Agent
(only tested on macOS)
Besides managing the GPG keychain, GnuPG is also able to handle SSH key authentication. This makes the native ssh-agent obsolete. gpg-agent can be used for this instead.
First, a subkey for authentication needs to be added to an existing GPG key.
Add Subkey
- Edit existing key in expert mode:
gpg --expert --edit-key <$KEY_ID> - Type
addkey…8to edit capabilitiesSto toggle off the sign capabilityEto toggle off the encrypt capabilityAto toggle on the authenticate capabilityQto quit- Keysize:
4096 0for ‘never expire’saveandquit
Configure zsh/bash
- Add this lines to
~/.zshrcor~/.bashrc:gpg-connect-agent --quiet /bye >/dev/null 2>/dev/null gpg-agent --daemon --quiet --enable-ssh-support >/dev/null 2>&1 export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)" export GPG_TTY=$(tty) - Kill running
gpg-agentprocesses:killall -9 gpg-agent - Reload shell configuration:
source ~/.zshrc
Add Subkey to GPG Agent
- Find the keygrip (identified by
[A]for authentication):gpg --list-secret-keys --with-keygrip - Write the keygrip to the
sshcontrolfile:echo "<$KEYGRIP>" >> ~/.gnupg/sshcontrol - Read the public SSH key and add it to the server’s
~/.ssh/authorized_keysfile:ssh-add -L
The SSH client now uses the gpg-agent to authenticate.