SSH Tunnel in Linux: 7 Easy Examples and Mistakes to Avoid

An SSH tunnel in Linux lets you reach a service on a machine you cannot connect to directly, by passing the traffic through a server you can log in to. The earlier version of this post had one diagram and two commands. One of those commands has a typo that OpenSSH accepts without complaint and that never works. This rewrite builds the setup in three containers and tests every command, including the failures: a wrong forward target, a busy port, a changed host key and a server that forbids forwarding.

The lab for the SSH tunnel in Linux

The setup is the one from the old post: a client, a jump server (server1, SSH on port 1111, user huupv) and a target (server2, SSH on port 22, user root). In the lab they are three Debian 13 containers on one network with the addresses 10.78.0.11 (client), 10.78.0.12 (server1) and 10.78.0.13 (server2), and OpenSSH 10.0p2. The client can reach both servers directly in this lab, which is not the case in real life; the point is to see which connection the traffic uses. Key-based login was set up on both servers beforehand.

SSH tunnel in Linux: the command from the old post

The old post used this command to open a local port 10000 that leads to port 22 on server2:

ssh -p 1111 -f huupv@10.78.0.12 -L 10000:root@10.78.0.13:22 -N

It looks fine: it exited with 0 and a listener appeared on port 10000. But the forward target must be host:port without a user name. OpenSSH treated root@10.78.0.13 as a host name, and every connection through the tunnel failed:

$ ssh -p 10000 root@127.0.0.1 true; echo "exit=$?"
kex_exchange_identification: read: Connection reset by peer
Connection reset by 127.0.0.1 port 10000
exit=255
$ grep -m1 'open failed' /tmp/tun.log
channel 2: open failed: connect failed: Name or service not known

The second line comes from the tunnel's own log (I started it with -E /tmp/tun.log). It says the jump server could not resolve the name root@10.78.0.13. The user name belongs in the second command, where you log in through the tunnel. With the user removed, the tunnel worked:

$ ssh -p 1111 -f huupv@10.78.0.12 -L 10000:10.78.0.13:22 -N
$ ssh -p 10000 root@localhost 'hostname -I; id -un; cat /root/hello.txt'
10.78.0.13
root
server2-file

Reading the options of this SSH tunnel in Linux: -L 10000:10.78.0.13:22 listens on local port 10000 and sends the traffic, through server1, to port 22 on 10.78.0.13; -N runs no remote command; -f goes to the background after authentication. Copying files works through the same tunnel, with a capital -P for scp: scp -P 10000 /tmp/up.txt root@localhost:/tmp/up.txt copied the file and a second SSH session read it back. To confirm which path was used, I listed server2's established connections. The login came from 10.78.0.12 (the jump server), not from the client:

0  0  10.78.0.13:22  10.78.0.12:43844

Port already in use and the silent failure of -f

A second problem with an SSH tunnel in Linux is a busy port. If port 10000 is taken, for example because an old tunnel is still running, OpenSSH prints an error but may still exit with 0, so a script thinks the tunnel started:

$ ssh -p 1111 -f huupv@10.78.0.12 -L 10000:10.78.0.13:22 -N; echo "exit=$?"
bind [127.0.0.1]:10000: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 10000
Could not request local forwarding.
exit=0

Add -o ExitOnForwardFailure=yes and the same command ends with exit=255. Use it in every script, and in ~/.ssh/config as ExitOnForwardFailure yes (see the ssh_config manual). The listener in the first test was bound to 127.0.0.1 and [::1] only, so other machines on the network cannot use your tunnel unless you ask for it with a bind address.

Stop an SSH tunnel in Linux cleanly

The easiest way to stop an SSH tunnel in Linux is to kill the ssh process, but with several tunnels you must pick the right one. A better method is a control socket: start the tunnel as a master with -M -S /tmp/tun.sock, then ask it about itself and to exit:

$ ssh -p 1111 -M -S /tmp/tun.sock -f -N -L 10000:10.78.0.13:22 huupv@10.78.0.12
$ ssh -S /tmp/tun.sock -O check x
Master running (pid=54)
$ ssh -S /tmp/tun.sock -O exit x
Exit request sent.
$ ss -ltn | grep -c 10000
0

The x is a placeholder host name; the socket decides which connection is addressed.

Terminal output of an SSH tunnel in Linux: the wrong root@ forward target fails and the correct one works
The old -L command fails at the jump server; the corrected one reaches server2 (real lab output).

ProxyJump: often better than a tunnel

For plain login and file copy you do not need an SSH tunnel in Linux at all. The -J option (ProxyJump) connects through the jump server in one command, with no local port to manage:

$ ssh -J huupv@10.78.0.12:1111 root@10.78.0.13 'id -un; hostname -I'
root
10.78.0.13
$ scp -J huupv@10.78.0.12:1111 /tmp/up.txt root@10.78.0.13:/tmp/up2.txt && echo copied
copied

Both worked in the lab. Use a tunnel when the application is not SSH, for example a database client that must connect to localhost:10000, and ProxyJump when you only need a shell or scp.

Host key warnings with localhost ports

Every SSH tunnel in Linux looks like localhost:10000 to your SSH client. If you use the same local port for different targets on different days, the client sees different host keys under the same name and refuses to connect. I logged in to server2 through the tunnel (the client stored the key for [localhost]:10000), then pointed the same port at server1:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

The warning is correct, since the key did change, but it is annoying. Give each target its own name with -o HostKeyAlias=server2; the client then stored the key under server2, and the login worked (alias-ok). Do not delete lines from known_hosts without checking why the key changed.

When the server forbids forwarding

An administrator can disable forwarding on the jump server, and then no SSH tunnel in Linux can pass through it. I started a second sshd on port 2222 with AllowTcpForwarding=no. The tunnel command still returned 0, because forwarding is checked when the first connection is made, and the connection through it failed:

$ ssh -p 10000 root@127.0.0.1 true; echo conn=$?
kex_exchange_identification: read: Connection reset by peer
conn=255
$ grep -m1 'open failed' /tmp/tun2.log
channel 2: open failed: administratively prohibited: open failed

The message "administratively prohibited" tells you the problem is the jump server's policy, not your command. If you also need to set up or lock down the server side, my post on enabling SSH on Ubuntu covers the service itself. The options above are documented in the OpenSSH ssh manual.

Summary of the SSH tunnel in Linux commands

  • For any SSH tunnel in Linux, write the forward as -L local_port:target_host:target_port, with no user name.
  • Add ExitOnForwardFailure=yes, because -f can return 0 when the forward failed.
  • Stop a tunnel with a control socket, not a blind pkill.
  • Use -J when you only need a shell or scp.
  • Use HostKeyAlias when a local port serves different targets.

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested