SSH Agent Forwarding Through a Bastion: Safer Alternatives to `ForwardAgent yes`
When you connect to a private server through a bastion, SSH agent forwarding can seem like the simplest way to authenticate at both hops. But forwarding does not copy your private key to the bastion; it gives processes there access to a socket that can ask your local agent to sign authentication requests. While the connection is active, a compromised account or untrusted process on the bastion may be able to use that access to authenticate elsewhere as you.
If the bastion is not a machine you trust, avoid forwarding your local agent. OpenSSH’s ProxyJump lets your local SSH client reach the private server through the bastion without making the agent available on it.
What agent forwarding exposes
With this pattern:
ssh -A bastion # Now running on the bastion: ssh app.internal the first SSH connection forwards access to your local agent. The second SSH client, running on the bastion, can use the forwarded agent to request signatures. It does not receive your private key itself, but an attacker who can access the forwarded socket may be able to authenticate to services that accept keys in your agent for as long as the forwarding connection is available.
Forwarding is not automatically unsafe in every environment. It can be useful when you need to run SSH commands from a remote machine and trust that machine and its administrators. But it is not required just because a route passes through a bastion. For background on that routing, see the OpenSSH jump-host guide.
Use ProxyJump to keep authentication local
Instead of logging into the bastion and starting another SSH client there, ask your local SSH client to connect through it:
ssh -J bastion app.internal The client establishes the route through bastion, then authenticates to app.internal from your local machine. Your local private key is not sent through the connection, and the bastion does not receive your forwarded agent socket. If your key is held by a local agent, the local SSH client may ask that agent to sign the target server’s authentication request; the request is not being made by a process on the bastion.
You can make the route and key selection explicit in ~/.ssh/config:
Host bastion HostName bastion.example.net User jumpuser IdentityFile ~/.ssh/id_ed25519_bastion IdentitiesOnly yes ForwardAgent no Host app.internal HostName 10.20.0.15 User deploy ProxyJump bastion IdentityFile ~/.ssh/id_ed25519_app IdentitiesOnly yes ForwardAgent no Connect with:
ssh app.internal This setup uses separate identities for the two SSH authentications: one for the bastion and another for the private host. Both key files stay on your local machine. The corresponding public keys need to be authorized for the right accounts on each server. IdentitiesOnly yes tells SSH to use the configured identity files rather than offering every identity available from an agent; it does not itself disable agent use or forwarding. If a configured identity is available through your local agent, SSH may still use the agent to sign with it.
For more examples of per-host OpenSSH settings, see the SSH config examples.
Check the effective forwarding setting
SSH configuration can be affected by matching host blocks, included files, command-line options, and global settings. Check what the client resolves for the destination:
ssh -G app.internal | grep -i '^forwardagent ' Expected setting when forwarding is disabled:
forwardagent no Also check the bastion alias, since it is a separate SSH host entry:
ssh -G bastion | grep -i '^forwardagent ' ssh -G prints the effective client configuration; it does not contact either server. If the result is not no, inspect your configuration files and any command-line options. OpenSSH generally uses the first obtained value for a setting, so the order of matching configuration matters. The ssh_config manual documents the rules for your installed OpenSSH version.
When forwarding really is needed
If you must run SSH from the bastion—for example, a deployment script there needs to connect onward—agent forwarding may be necessary. Enable it only for the specific trusted host rather than globally:
Host trusted-bastion ForwardAgent yes Use a dedicated key with limited access where possible, and close the session when the work is done. Disabling forwarding does not prevent a bastion from using credentials or access already available to its own account; it specifically avoids exposing your local agent through that SSH session.
If ProxyJump reaches the target but authentication fails, verify the target username, selected identity, and the public key authorized on the target. Avoid turning on forwarding as a troubleshooting shortcut; diagnosing the rejected key is a separate problem, covered in this guide to SSH Permission denied (publickey) errors.
I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.