$ run as a normal user# run as root (or with sudo)
The quick answer
ssh -J alice@bastion.example.com alice@10.0.1.20Or in ~/.ssh/config, once:
Host bastion
HostName bastion.example.com
User alice
Host 10.0.1.* *.internal.example.com
User alice
ProxyJump bastionThen simply: ssh 10.0.1.20.
Step by step
- How it works: your SSH client connects to the bastion, and through it opens a second, end-to-end encrypted SSH connection to the target. Your private key never leaves your laptop; the bastion only forwards the traffic.
- The same works for copy tools:
scp -J bastion backup.tar.gz 10.0.1.20:/tmp/
rsync -avz -e 'ssh -J bastion' ./site/ 10.0.1.20:/var/www/site/
ssh -J bastion1,bastion2 10.0.5.9With the config file, plain scp file 10.0.1.20:/tmp/ already goes through the bastion.
- Ansible uses your SSH config — or set it in the inventory:
[private:vars]
ansible_ssh_common_args='-o ProxyJump=alice@bastion.example.com'- Do not use agent forwarding (
ssh -A) to hop — anyone with root on the bastion could use your agent.ProxyJumpneeds no agent on the bastion. - Harden the bastion: key-only login, MFA if possible, only admins in
AllowGroups, no other software on it, port 22 open only from known networks, Fail2ban, and send its logs to a central place. The bastion needsAllowTcpForwarding yes(the default) — restrict it withPermitOpento the private networks if you want. - The targets allow SSH only from the bastion:
firewall-cmd --permanent --zone=internal --add-source=10.0.0.5/32
firewall-cmd --permanent --zone=internal --add-service=ssh
firewall-cmd --permanent --zone=public --remove-service=ssh
firewall-cmd --reload(10.0.0.5 = the bastion's private IP.)
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
channel 0: open failed: administratively prohibited | The bastion forbids TCP forwarding. | AllowTcpForwarding yes (or PermitOpen for the targets) on the bastion. |
Permission denied (publickey) at the target | Your key is not on the target (only on the bastion). | Put your public key on the target too. |
Old OpenSSH without -J | OpenSSH older than 7.3. | ProxyCommand ssh -W %h:%p bastion. |
| Slow logins through the bastion | DNS lookups on both hops. | UseDNS no on servers; use IPs or local DNS. |
| Host key warnings for internal IPs | Many networks reuse the same private IPs. | Use names (*.internal.example.com), or separate known_hosts per environment. |
| Your session dies when the bastion is patched | One bastion. | Two bastions, or use cloud access (Session Manager, Azure Bastion). |
Go further
- Command pages: ssh, scp, rsync.
- Next how-tos: Manage many servers in the SSH config file, Open an SSH tunnel to a database, Harden the SSH server, Connect to EC2 without SSH keys.
- Full guide: OpenSSH: jump hosts and tunnels. Azure: Azure Bastion.
1Why is ProxyJump safer than agent forwarding (ssh -A)?
Your key stays on your laptop; root on the bastion can not use your agent.
2How do you copy a file through a bastion in one command?
scp -J bastion FILE target:/path/ (or plain scp with ProxyJump in ~/.ssh/config).