The agent and agent forwarding
What an SSH agent does, how forwarding works, and why it carries a risk.
An agent is a small program that holds your unlocked private keys in memory and signs on request. You enter the passphrase once, and every connection after that works without asking.
On a computer: ssh-agent starts it, ssh-add loads a key, and the SSH_AUTH_SOCK variable tells programs where it is.
Agent forwarding
With agent forwarding (ssh -A or ForwardAgent yes) the remote host can use your local agent to sign for a further login. So from host A you can ssh to host B with your own key, though the key is not copied to the remote host. If forwarding does not work, see SSH agent forwarding not working: Could not open a connection to your authentication agent.
The risk
While you are connected, a process on host A that can reach your forwarded agent (the host's root, or you if you are on the same account) can ask your agent to sign. It cannot copy the private key, but it can use it to log in as you to anything that trusts the key, for as long as the connection is open.
Because of this:
- Forward the agent only to hosts you trust fully.
- Prefer a jump host (
ProxyJump) when the goal is only to reach a host behind another. See Jump hosts. - Use the agent's confirmation option (
ssh-add -c) so each signature needs your approval. - Do not forward in a shared account.
Without the agent
If agent forwarding is off, a remote shell that needs a key (for example to run git pull over SSH) cannot use yours. Options: a separate key on the host, a deploy key for that one job, or a jump host.
Security keys and agents
A key held in hardware (see Security keys) can be used through an agent. The device still asks for a touch for each signature, which limits the risk.
Related
- Jump hosts
- Config file
- SSH agent not available after sudo or su
- SSH "Too many authentication failures"
- App guide: Agent forwarding
- SSH agent forwarding not working: Could not open a connection to your authentication agent
- Change or remove an SSH key passphrase with ssh-keygen -p
- ssh-add "Could not open a connection to your authentication agent": start the agent
- SSH "agent refused operation": signing failed and could not add identity