Multi‑jump bastion access without storing keys: practical ssh‑agent forwarding

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

  1. OpenSSH 8.9+ – that’s the default on most 2026 distros.

  2. ssh-agent running locally and the key loaded (ssh-add ~/.ssh/id_rsa).

  3. The bastion’s sshd_config must allow agent forwarding:

    AllowAgentForwarding yes
    
  4. The 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
  • -A enables agent forwarding on the first hop.
  • -J specifies 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

SymptomLikely CauseFix
Connection closed by remote host after loginAgent forwarding disabled on bastionVerify AllowAgentForwarding yes and reload sshd (systemctl reload sshd).
Permission denied (publickey) on targetKey not present in local agentRun ssh-add ~/.ssh/id_rsa and confirm with ssh-add -l.
ssh: connect to host target.internal port 22: Connection timed outNetwork path blocked between bastion and targetCheck firewall rules on bastion (iptables -L -n) and ensure target accepts SSH from the bastion’s IP.
ssh: no matching key foundWrong key type (e.g., ED25519) not accepted by targetEnsure target’s authorized_keys contains the correct public key.

Security Trade‑offs

ApproachProsCons
Key on bastionSimple, no agent setupKey can be stolen if bastion is compromised
Agent forwardingKey never leaves workstationRequires 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

See also