How do I SSH through a jump host (bastion)?

Add -J user@bastion to the ssh command, or write ProxyJump bastion in the block of the server. ssh connects to the bastion first and opens the connection to the server through it.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

A jump host, also called a bastion, is a server you can reach from outside that in turn can reach servers you cannot. To get to the inner server, run ssh -J deploy@bastion deploy@inner. For a server you use often, write ProxyJump bastion in its block of ~/.ssh/config, and ssh inner does the same.

The setup used here

Two servers are involved. The bastion is on the network you are on. The target is only on a second network that the bastion also sits on, so you have no route to it. The file ~/.ssh/config has a block for each, and the target names the bastion with ProxyJump:

$ cat ~/.ssh/config
Host bastion
    HostName bastion.example.test
    User deploy

Host target
    HostName target.example.test
    User deploy
    ProxyJump bastion

Host *
    StrictHostKeyChecking accept-new

The first attempt goes straight to the target. It fails, and the failure is the point: from this machine the name does not even resolve.

$ ssh deploy@target.example.test hostname
ssh: Could not resolve hostname target.example.test: Name or service not known

Jump with -J

Name the bastion with -J. The user in front of the bastion’s address is the user on the bastion, and the user in front of the target is the user on the target:

$ ssh -J deploy@bastion.example.test deploy@target.example.test hostname
Warning: Permanently added 'bastion.example.test' (ED25519) to the list of known hosts.
Warning: Permanently added 'target.example.test' (ED25519) to the list of known hosts.
target

The command printed target, the host name of the inner server. ssh logged in to the bastion, asked it to open a connection to the target and talked to the target through it. Both host keys were new to this machine, so you see two warnings. Each server is checked against your own known_hosts, not the bastion’s.

The connection from you to the target is encrypted from end to end. The bastion carries the bytes and cannot read them.

Put it in the config file

Typing the jump every time gets old. The target’s block in the file already has ProxyJump bastion, so the short form works:

$ ssh target hostname
target

ssh -G shows the option that did it:

$ ssh -G target | grep -E '^(hostname|user|proxyjump) '
user deploy
hostname target.example.test
proxyjump bastion

ProxyJump bastion uses the block named bastion for the first hop, so its address, user, port and key come from that block. Several jumps are separated by commas, as in ProxyJump first,second, and ssh uses them in that order.

What ssh runs for you

Before OpenSSH 7.3 you wrote the same thing by hand with ProxyCommand ssh -W %h:%p bastion. The debug output shows that ProxyJump still does exactly this:

$ ssh -v target true 2>&1 | grep -E 'Executing proxy command|Authenticated to target'
debug1: Executing proxy command: exec ssh -v -W '[target.example.test]:22' bastion
Authenticated to target.example.test (via proxy) using "publickey".

The -W option asks the first ssh to forward its standard input and output to the target’s port 22. Nothing runs on the bastion except its normal sshd.

Other tools follow the same config

scp, sftp and rsync read the file too, so the jump comes with the name:

$ scp -q target:/etc/hostname /tmp/target-hostname
$ cat /tmp/target-hostname
target

If you have no config block, scp -J deploy@bastion.example.test deploy@target.example.test:/etc/hostname . works the same way.

Mistakes people make

You forward your agent with -A to reach the inner server. You do not need to. With a jump, your keys never leave your machine, and a bastion that is compromised cannot use your agent. Agent forwarding gives the bastion that chance.

The bastion refuses to forward. If its sshd has AllowTcpForwarding no, the jump fails after the login to the bastion succeeded. Ask for forwarding on that account, or use a bastion that allows it.

The users differ. One user in front of the target is not enough when the bastion needs another one. Write both: -J bastionuser@bastion targetuser@target, or set User in each block.

The port belongs to the target only. ssh -p 2222 -J bastion target uses 2222 for the target. Give the bastion’s port in the jump itself, as -J deploy@bastion:2200.

A jump set for every host. If ProxyJump bastion sits in Host *, ssh tries to reach the bastion through the bastion. Put the option in the blocks of the inner servers, or turn it off for one host with ProxyJump none.

A changed key on the bastion looks like a problem with the target, because the message comes while you connect to the target. The message names the host. If it is the bastion, the host key fix applies to it first. The structure of the file itself is in the config file article, and tunnels that carry other ports are in port forwarding.

In Valakes

Valakes has the same idea. Set Jump host in the host editor to the host that forwards you. On the first connection it asks about the host key of every hop that is not trusted yet, labeled “Hop 1 of 2” and so on, and the shell opens on the last host.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.