Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 112Security · SSH

How to use a jump host (bastion) with SSH

Private servers should have no public SSH port. Admins reach them through one hardened jump host (bastion): the only server with port 22 open, with MFA, logging and few users. With ProxyJump this is one command — no copying keys to the bastion, no agent forwarding. This how-to sets it up for ssh, scp, rsync and Ansible, and hardens the bastion.

  • 5 Lessons
  • 5 Commands

$ run as a normal user# run as root (or with sudo)

112.1

The quick answer

ssh -J alice@bastion.example.com alice@10.0.1.20

Or in ~/.ssh/config, once:

~/.ssh/config
Host bastion
    HostName bastion.example.com
    User alice
 
Host 10.0.1.* *.internal.example.com
    User alice
    ProxyJump bastion

Then simply: ssh 10.0.1.20.

Commands in this lesson

112.2

Step by step

  1. 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.
  2. 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.9

With the config file, plain scp file 10.0.1.20:/tmp/ already goes through the bastion.

  1. Ansible uses your SSH config — or set it in the inventory:
inventory.ini (part)
[private:vars]
ansible_ssh_common_args='-o ProxyJump=alice@bastion.example.com'
  1. Do not use agent forwarding (ssh -A) to hop — anyone with root on the bastion could use your agent. ProxyJump needs no agent on the bastion.
  2. 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 needs AllowTcpForwarding yes (the default) — restrict it with PermitOpen to the private networks if you want.
  3. 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.)

112.3

Check that it worked

ssh 10.0.1.20 hostname
app1
ssh -G 10.0.1.20 | grep -i proxyjump
proxyjump bastion

Direct SSH to the target from the internet times out; through the bastion it works.

112.4

Common problems

ProblemCauseFix
channel 0: open failed: administratively prohibitedThe bastion forbids TCP forwarding.AllowTcpForwarding yes (or PermitOpen for the targets) on the bastion.
Permission denied (publickey) at the targetYour key is not on the target (only on the bastion).Put your public key on the target too.
Old OpenSSH without -JOpenSSH older than 7.3.ProxyCommand ssh -W %h:%p bastion.
Slow logins through the bastionDNS lookups on both hops.UseDNS no on servers; use IPs or local DNS.
Host key warnings for internal IPsMany networks reuse the same private IPs.Use names (*.internal.example.com), or separate known_hosts per environment.
Your session dies when the bastion is patchedOne bastion.Two bastions, or use cloud access (Session Manager, Azure Bastion).

112.5

Go further

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).

All commands in this chapter