Multi‑jump Bastion Access Without Storing Keys
When you need to reach a private host behind a bastion, the first thing most people do is drop a private key on the jump machine and use it to hop over. That’s quick, but it gives the bastion a copy of the key it never should have. A cleaner pattern is to forward your local ssh‑agent through the bastion so the key never lands on the jump host.
Why Forwarding Is Safer
- No key copy – Your key stays on your workstation. The bastion only ever sees the public key and the SSH session.
- Audit‑friendly – The bastion can log the agent‑forwarding request, but it can’t pull the key out of your agent.
- Key rotation – Rotate keys on your workstation without touching the bastion at all.
Prerequisites
OpenSSH 8.9+ – that’s the default on most 2026 distros.
ssh-agentrunning locally and the key loaded (ssh-add ~/.ssh/id_rsa).The bastion’s
sshd_configmust allow agent forwarding:AllowAgentForwarding yesThe target host must trust the public key you use on the bastion.
One‑Command Multi‑jump
ssh -A -J user@bastion.example.com user@target.internal
-Aenables agent forwarding on the first hop.-Jspecifies the jump host.- The agent is automatically forwarded to the target.
Running a command on the target is just as simple:
ssh -A -J user@bastion.example.com user@target.internal 'sudo systemctl restart nginx'
ProxyCommand for Older SSH
If your client doesn’t support -J, fall back to ProxyCommand:
ssh -A -o ProxyCommand="ssh -W %h:%p user@bastion.example.com" user@target.internal
Common Pitfalls & Fixes
| Symptom | Likely Cause | Fix |
|---|---|---|
Connection closed by remote host after login | Agent forwarding disabled on bastion | Verify AllowAgentForwarding yes and reload sshd (systemctl reload sshd). |
Permission denied (publickey) on target | Key not present in local agent | Run ssh-add ~/.ssh/id_rsa and confirm with ssh-add -l. |
ssh: connect to host target.internal port 22: Connection timed out | Network path blocked between bastion and target | Check firewall rules on bastion (iptables -L -n) and ensure target accepts SSH from the bastion’s IP. |
ssh: no matching key found | Wrong key type (e.g., ED25519) not accepted by target | Ensure target’s authorized_keys contains the correct public key. |
Security Trade‑offs
| Approach | Pros | Cons |
|---|---|---|
| Key on bastion | Simple, no agent setup | Key can be stolen if bastion is compromised |
| Agent forwarding | Key never leaves workstation | Requires bastion to allow forwarding; risk of “man‑in‑the‑middle” if bastion is malicious (it can request the agent to sign arbitrary data) |
To mitigate the malicious‑bastion risk, use a restricted key on the bastion that only allows SSH and no other capabilities. A jump‑only key with command="echo 'This key is for jump only'; exit 1" in authorized_keys is a good pattern.
Automating the Flow
A tiny helper script gets rid of the boilerplate:
#!/usr/bin/env bash
set -euo pipefail
BASTION=user@bastion.example.com
TARGET=user@target.internal
CMD=${1:-}
ssh -A -J "$BASTION" "$TARGET" "$CMD"
Make it executable (chmod +x jump.sh) and drop it into your PATH. Now:
jump.sh 'df -h'
Logging and Auditing
On the bastion, enable LogLevel VERBOSE in sshd_config. That logs the user, the forwarded key fingerprint, and the target host. Pair it with auditd to capture the exact command that ran on the target:
auditctl -w /var/log/auth.log -p wa
Reference
- OpenSSH Agent Forwarding – https://www.openssh.com/portable.html
- SSH Configuration Options – https://man.openbsd.org/ssh_config