Forwarding your YubiKey and gpg-agent to a remote SSH host allows you to use your local smartcard (YubiKey) and GPG keys seamlessly on the remote machine. Here's a step-by-step guide on how to do this:
Overview
- Goal: Use your local YubiKey and
gpg-agent on a remote SSH server.
- Key points:
- Forward the SSH agent (
ssh-agent) so the remote host can use your SSH keys.
- Forward the GPG agent socket so the remote host can use your GPG keys and smartcard.
- Forward the smartcard device (YubiKey) if needed.
Step 1: Forward SSH Agent
Make sure your local SSH agent is running and your keys are loaded:
eval "$(ssh-agent -s)"
ssh-add
Then connect with agent forwarding enabled:
ssh -A user@remote-host
This forwards your SSH agent socket, allowing the remote host to use your SSH keys.
Step 2: Forward GPG Agent Socket
2.1 Find your local GPG agent socket
On your local machine, the GPG agent socket is usually at:
~/.gnupg/S.gpg-agent.ssh
or
~/.gnupg/S.gpg-agent
You can check with:
gpgconf --list-dirs agent-ssh-socket
gpgconf --list-dirs agent-socket
2.2 Forward the GPG agent socket over SSH
You can forward the GPG agent socket using SSH's -R or -L options, but since the socket is a Unix domain socket, you need to use ssh -R with a remote socket or use socat to forward it over TCP.
Simplest method: Use ssh -R with a remote socket
On your local machine, run:
ssh -R /home/user/.gnupg/S.gpg-agent:/home/localuser/.gnupg/S.gpg-agent user@remote-host
This forwards the local socket to the remote socket path.
Alternatively, use socat to forward over TCP:
- On local machine, run:
socat TCP-LISTEN:12345,reuseaddr,fork UNIX-CONNECT:$(gpgconf --list-dirs agent-socket)
- On remote machine, connect back to local machine's port 12345 and create a local socket:
socat UNIX-LISTEN:$HOME/.gnupg/S.gpg-agent TCP:localmachine:12345
Then set the environment variable on the remote machine:
export GPG_AGENT_INFO=$HOME/.gnupg/S.gpg-agent
Step 3: Forward the Smartcard Device (YubiKey)
If you want to forward the actual USB device (YubiKey) to the remote host, you need USB forwarding over SSH, which is not natively supported.
Options:
- Use
ssh with usbip (complex, requires root on both ends).
- Use
gpg-agent forwarding only (recommended).
Usually, forwarding the GPG agent socket is enough because the agent communicates with the YubiKey locally.
Step 4: Configure Remote Environment
On the remote host, ensure:
gpg-agent is not running or is configured to use the forwarded socket.
SSH_AUTH_SOCK points to the forwarded socket (usually done automatically with ssh -A).
GPG_AGENT_INFO points to the forwarded GPG agent socket.
Example .bashrc or .bash_profile snippet on remote:
export GPG_AGENT_INFO=$HOME/.gnupg/S.gpg-agent
export SSH_AUTH_SOCK=$HOME/.gnupg/S.gpg-agent.ssh
Summary
- Use
ssh -A to forward SSH agent.
- Forward GPG agent socket via SSH remote socket forwarding or
socat.
- Use the forwarded sockets on the remote host.
- Usually, no need to forward the USB device itself.
If you want, I can help you with a script or more detailed instructions for your specific OS/setup!