> articles

What is SSH ProxyCommand? Connecting when port 22 isn't open

ProxyCommand has ssh reach the server through another program's input and output. How it works, how to write %h and %p, how it relates to ProxyJump, and how to connect through an HTTP proxy, Cloudflare Access or AWS SSM.

Published

On this page
  1. All ssh needs is a pipe
  2. How to write it
  3. How it relates to ProxyJump
  4. Common uses
  5. In ShellRS

When you type ssh user@server, your computer connects straight to port 22 on the server. Sometimes that road is closed: the office network only lets you out through an HTTP proxy, or the server, for safety, doesn’t open port 22 at all and can only be reached through a provider’s tunnel, such as Cloudflare’s or AWS’s. That’s what ssh’s ProxyCommand is for: instead of connecting by itself, ssh runs a command you give it and lets that command carry the data to the server. Here’s how it works, how to write it, and the most common uses.

All ssh needs is a pipe

The SSH protocol doesn’t care how its data reaches the server, as long as there’s a channel that carries bytes both ways. Usually that channel is a TCP connection to port 22. With ProxyCommand, ssh starts the command and uses its standard input and output as the channel: what ssh sends goes into the command’s standard input, and whatever the command prints is the server’s reply. How the command gets the data to the server, through a proxy, a tunnel or anything else, is up to the command.

The simplest example has nc (netcat) pass the data along unchanged:

ssh -o ProxyCommand='nc %h %p' user@server

nc connects to port 22 on the server, passes on what ssh gives it and hands back what comes in, exactly as a direct connection would. Swap nc for another program and the data takes another road.

Encryption still runs between ssh and the server, so the command in the middle only handles encrypted bytes and never sees what you type or your password, and the server’s host key is checked as usual. Going through a proxy or a tunnel doesn’t make SSH itself any less secure.

How to write it

It usually goes in ~/.ssh/config, so it applies every time you connect:

Host myserver
    HostName 10.0.0.5
    User ubuntu
    ProxyCommand nc %h %p

These tokens in the command are replaced when connecting:

Token Becomes
%h the address to reach (HostName)
%p the port
%r the username to log in as
%n the name you typed on the command line (myserver here)
%% a single %

The command has to keep running for as long as the connection lasts. If it exits right after starting (it isn’t installed, you aren’t signed in, an argument is wrong), ssh gets nothing from the server and fails with a puzzling error:

kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535

UNKNOWN port 65535 almost always means the proxy command itself exited. Its error output appears in the terminal, so scroll up a few lines and the real reason is usually there.

If Host * sets a ProxyCommand and you want one machine to connect directly, give it ProxyCommand none.

How it relates to ProxyJump

Going through a jump host is what ProxyJump does (ssh -J bastion target on the command line): ssh logs in to the jump host, asks it to connect to port 22 on the target, and runs the second SSH connection over that. ProxyJump arrived in OpenSSH 7.3; before that, people wrote the same thing with ProxyCommand:

# These two do the same; write one of them
ProxyJump bastion
ProxyCommand ssh -W %h:%p bastion

ssh -W %h:%p bastion means: log in to bastion, ask it to connect to %h:%p, and join that connection to my standard input and output. So ProxyJump is really ProxyCommand’s most common use, given an option of its own. The rule of thumb: through another SSH server, use ProxyJump; any other road (a proxy, a provider’s tunnel), use ProxyCommand. When both appear, only the one written first takes effect.

Common uses

Through an HTTP or SOCKS proxy

When the office network only lets you out through an HTTP proxy, have nc connect through it:

Host work-server
    ProxyCommand nc -X connect -x proxy.example.com:8080 %h %p

-X connect is an HTTP proxy (it sends a CONNECT request) and -X 5 is a SOCKS5 proxy, as in nc -X 5 -x 127.0.0.1:1080 %h %p. That’s the OpenBSD nc, which is the one macOS ships and the netcat-openbsd package on Debian and Ubuntu. With nmap’s ncat instead, write ncat --proxy proxy.example.com:8080 --proxy-type http %h %p. On Windows, Git for Windows comes with connect.exe: connect -H proxy.example.com:8080 %h %p.

Many office proxies only allow connections to port 443 and refuse port 22. Then you’ll need the administrator to allow it, or have the server’s sshd listen on port 443 as well.

Cloudflare Access

When the server is connected through Cloudflare Tunnel and opens no ports to the outside, install cloudflared on your computer and write:

Host ssh.example.com
    ProxyCommand cloudflared access ssh --hostname %h

The first time you connect, cloudflared opens a browser for you to sign in to Cloudflare Access; after that, SSH reaches the server through Cloudflare’s network.

AWS Systems Manager (SSM)

An EC2 instance with no port 22 open and no public address can be reached through SSM. Install the AWS CLI and its Session Manager plugin, then write:

Host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

Then use the instance ID as the address: ssh ec2-user@i-0123456789abcdef0. The instance needs the SSM Agent running and an IAM role that allows SSM, and you still log in with your SSH key.

Google Cloud IAP

Google Cloud instances can be reached through an IAP tunnel, and gcloud has a flag made for ProxyCommand, --listen-on-stdin:

Host my-vm
    ProxyCommand gcloud compute start-iap-tunnel %h %p --listen-on-stdin --zone=us-central1-a

Here my-vm has to be the instance’s name. For the occasional connection, gcloud compute ssh my-vm --tunnel-through-iap is simpler.

In ShellRS

ShellRS is a free, open-source SSH client, and “Connection” in its host dialog has “Proxy command”, which works like OpenSSH’s ProxyCommand:

  • Choose “Proxy command” and enter the command; %h, %p and %r become the address, port and username, and %% is a %. Only SSH hosts have it.
  • The command runs with sh on macOS and Linux and with cmd on Windows, with no window, and when the connection ends, the command ends along with whatever it started.
  • ShellRS opened from the Finder or the Dock only has the system folders to look in, so on macOS it also looks for the command in Homebrew’s and MacPorts’ folders, ~/.local/bin and ~/go/bin; for a command installed elsewhere, enter its full path.
  • If the command exits while connecting (it isn’t installed, say, or you aren’t signed in), the error shows its exit code and the last few lines it printed, so there’s no guessing what UNKNOWN port 65535 means. Logging in through a proxy command can take up to 2 minutes, enough time to sign in to something like Cloudflare Access in the browser.
  • Importing hosts from ~/.ssh/config brings their ProxyCommand along.
  • For a plain HTTP or SOCKS5 proxy, “Proxy” under “Connection” does it without nc; to go through another SSH host, use “Jump hosts”.

More in the ShellRS manual, under “Connection” in the Hosts section. ShellRS is available from the download page.

Try ShellRS

A free, open-source SSH client: terminals, SFTP and port forwarding in one window, for macOS, Windows and Linux.