rescueshell Start trial

Who can connect to your ssh -R forwarded port

The tunnel is healthy. The device dialled in, the forward is up, and ssh -p 2222 localhost on the jump host drops you at the device's login prompt. That is the whole point of the setup, and it is also the moment to ask a plain question: who else can type that command?

Our earlier post on what a reverse SSH tunnel leaves open covers a jump host that decides who is trusted. This one is about the listener while everything works, and what a connection to it is worth.

One note on method. We have no OpenSSH server in the environment we wrote this in, so we did not run the commands below on a live pair of hosts. What we say about OpenSSH comes from its documented behaviour. Check it on your version before you rely on it.

The forward asks nobody

A command like this, run on the device, is the usual shape:

ssh -N -R 2222:localhost:22 tunnel@jump.example.net

It asks sshd on the jump host to listen on port 2222 and to hand every connection to that port back down the tunnel, to the device's own sshd. The listener belongs to the tunnel user's sshd process, but it does not check who connects to it. A TCP port has no idea which key opened the tunnel, and no idea which user is knocking. If you can open a connection to it, you get the device's SSH banner.

So the answer to "who can connect" is set by where the port is bound.

Loopback is not private

By default sshd binds a remote forward to the loopback address only. The setting that controls this, GatewayPorts, defaults to no, and in that state a client asking for a wider bind is ignored. Many people read "loopback only" as "safe". It means something narrower: only processes on the jump host can connect.

On a jump host that is not a lot of comfort. Loopback ports have no owner and no file mode. Any account on the machine can connect to port 2222: every engineer with a login, a build agent, a monitoring job, a contractor's service account, and any process an attacker has managed to start under any of them. The tunnel user's own session is not special. Your devices end up reachable by everyone who can run a command on that host, not only by the people you meant.

The other two values change the audience a lot.

Both settings are chosen once, on the host, and forgotten. The forward you open next year inherits them.

What a connection to it reaches

The connection arrives at the device's sshd, before any login. Three things follow from that.

The device's own settings are the only gate. If password login is on, anyone who can reach the port can guess passwords against a machine that sits in somebody else's building. A device image with an old OpenSSH, or a default account nobody changed, is as exposed as it would be on a public address, only with a smaller audience. Key-only login and a short list of allowed accounts matter more here than anywhere else, because there is no other layer.

Your usual source controls do not work. The forward is made by the ssh client on the device, which connects to its own sshd on loopback. So sshd on the device sees every attempt as coming from the device itself, whoever started it on the jump host. A rule that limits logins by source address cannot tell the jump host's users apart, and a ban tool that bans the address it sees would ban loopback, which is the path your own tunnel arrives on. We have not tried every ban tool, so check yours.

It is many devices, not one. One port per device is a list. A user who is allowed to reach one device with a ticket can try the neighbouring port as easily. Which port belongs to which device is a convention kept in a script, and nothing enforces it unless you add permitlisten per key, as described below.

Narrowing it, without moving off the model

If you run your own jump host, there are real controls, and they are free.

None of this is exotic, and a team that does all of it has most of the way there. The cost is the bookkeeping: sockets, directories, modes, one more thing to check each time a device is added.

What a device that opens no port changes

The RescueShell agent does not use a forward. It dials out to our relay over HTTPS, and the device opens no port. When a shell is asked for, the agent connects to the device's own SSH server on the device's own loopback address, and to nothing else. Nobody on a middle host has a listener to try. The how it works page describes the same model.

The part that matters for this post is what the sshd on the device is told to accept. A session's key is made available only after your key has signed a fresh challenge. That key is written as a short-lived file, and it carries restrictions: no port forwarding, no agent forwarding, no X11 forwarding, and it is accepted only for a connection that comes from the device's own loopback address, which is the agent's own connection. A login attempt arriving any other way cannot use it. The agent also refuses password sessions, and the device keeps its own list of trusted keys, which our servers cannot add to. See the security page for the plain statement of each.

There is no port per device and no list of ports to keep straight, so there is no neighbouring port to try.

Where this does not fit


RescueShell: a shell on any machine you shipped. Hosted in the EU, exceptions listed. Try it

All posts · RSS