Contents
- Describe the tunnel according to the listening location
- Preparation: verify account, destination, and access rules
- Local forwarding: database accessible only from the server
- Remote forwarding: a service on the client accessible from the server
- Dynamic forwarding: SOCKS proxy for a specific application
- What a server must allow and what an individual key can restrict
- Diagnostics: a created port does not mean a functional application
- Operation, termination and verification of the result
- Frequently asked questions
Describe the tunnel according to the listening location
An SSH tunnel carries the connection over an already authenticated SSH connection. A typical example is a database that is only available on the server localhost or an internal website that you cannot access directly from your laptop. Before entering the command, write down three places: SSH client, SSH server, and target service. The most important question is which machine will create the listening port and which machine will establish the final connection to the target.
- Local forwarding (-L): the client listens, the SSH server side connects to the target service.
- Remote forwarding (-R): the SSH server side listens, the client side connects to the target.
- Dynamic forwarding (-D): the client provides a local SOCKS proxy and the application requests various destination connections through it.
SSH encrypts the segment between its two ends. If the SSH server establishes another plain TCP connection to another database or website, this additional segment is not automatically encrypted by SSH. Therefore, maintain TLS or other appropriate security for the target application according to your infrastructure. The tunnel also does not provide logins to the database: its accounts, permissions, and auditing still apply.
Preparation: verify account, destination, and access rules
The examples use the OpenSSH client and your own servers where you have permission to use forwarding. Your administrator may intentionally restrict forwarding. Do not use the tunnel as a way to bypass your company's firewall or service isolation rules. First, log in to the SSH server normally, check its fingerprint, and verify that the target is reachable from the party that will be connecting to it.
ssh spravce@bastion.example
# Na bastionu: zkontrolujte zamýšlenou cílovou službu a síťovou cestu.
# Pokud služba běží přímo na bastionu:
ss -lntFor the local port, choose an unprivileged number that is not already in use by another process. The examples explicitly bind the listen to 127.0.0.1, which is IPv4 loopback. This makes the tunnel available locally and does not automatically expose it to the entire Wi-Fi. However, local users on the same computer can still access the port; loopback is not a separate user privilege.
Keep the first attempt in the foreground. You will see port busy and server rejection errors, and you can easily terminate the tunnel with Ctrl-C. The -N option does not run a remote command. -T does not create a pseudo-terminal. ExitOnForwardFailure helps catch failures to create a forward, but it does not prove the availability of the target service on each subsequent connection.
Local forwarding: database accessible only from the server
Let's assume that PostgreSQL is running on the same machine as the SSH server and is listening only on its address 127.0.0.1:5432. On the laptop, you create port 15432. The database client connects to the laptop's localhost, and the SSH server makes the final connection to its localhost. So the two 127.0.0.1 addresses in one command refer to two different computers.
ssh -N -T -o ExitOnForwardFailure=yes \
-L 127.0.0.1:15432:127.0.0.1:5432 \
spravce@db-server.exampleThen set the host 127.0.0.1 and port 15432 in the database client. Use a regular database account with the necessary minimum rights. If the database is running on another internal machine, replace the target part with, for example, db.internal:5432. The name db.internal must be resolvable by the SSH server side and must have a network path to it. Such a name may not be resolvable from a laptop.
When verifying the database certificate, a connection to localhost may encounter a certificate name mismatch. Do not solve this by disabling TLS authentication. Use the client's options to separate the destination address and the authenticated name, or its documented way of working with the tunnel. A similar problem occurs with HTTPS: the availability of a TCP port does not ensure the correct Host, SNI, or service identity.
Remote forwarding: a service on the client accessible from the server
With -R the direction is reversed. A listening port is created on the remote server and the connection is returned via the SSH client to its destination. The example makes a local development website accessible from a laptop on the loopback of the remote server. It is suitable for checking from a specific approved machine; without further configuration it does not create a public website for the entire internet.
ssh -N -T -o ExitOnForwardFailure=yes \
-R 127.0.0.1:18080:127.0.0.1:8080 \
spravce@kontrola.example
# Na vzdáleném serveru potom:
curl --max-time 5 http://127.0.0.1:18080/The target 127.0.0.1:8080 is evaluated here on the laptop side where the SSH client is running. The remote port 18080 remains local to the remote server. Its access from other machines depends on the GatewayPorts and firewall settings. If you need public access, this requires a separate design for authentication, TLS, and allowed clients; simply changing the bind address to 0.0.0.0 is not a security plan.
On a shared server, local processes can access the loopback port just like with local forwarding. Therefore, a development site should not rely on every request from the tunnel to be trusted. Do not expose internal administration, the debug console, or a service with secret test data in this way without authentication.
Dynamic forwarding: SOCKS proxy for a specific application
The -D switch creates a SOCKS proxy. Unlike -L, you don't specify a single target service in the command; the target is chosen by the application using the proxy. An example would be a browser for internal websites that the bastion can reach. Use a separate profile or a specific application to make it clear which traffic you are actually sending through the tunnel.
ssh -N -T -o ExitOnForwardFailure=yes \
-D 127.0.0.1:1080 spravce@bastion.example
# Příklad cíleného požadavku přes SOCKS s překladem jména na proxy straně:
curl --socks5-hostname 127.0.0.1:1080 https://intranet.example/Be aware of who is resolving the target DNS name. In the curl example above, you are passing it to a SOCKS proxy; another application may resolve the name locally before using the proxy. Also, setting SOCKS in one program does not automatically apply to system updates, other browsers, or all DNS queries. An SSH SOCKS proxy is not a full-fledged VPN with all-protocol routing.
Regular TCP forwarding does not forward arbitrary UDP packets or ICMP. A broken ping through such a tunnel does not indicate the availability of the website via SOCKS. For an application with HTTP/3, the transmission path or protocol used may change; verify the actual protocol in the client and do not assume it only based on QUIC support on the target server.
What a server must allow and what an individual key can restrict
On an SSH server, forwarding can be restricted by AllowTcpForwarding, DisableForwarding, PermitOpen, and PermitListen. Match blocks and specific key options in authorized_keys also affect the result. Therefore, a general value in the main configuration file alone may not explain the behavior of a specific account. For a tunnel-only account, design the targets and direction according to actual needs.
sudo /usr/sbin/sshd -T -C user=tunel,addr=203.0.113.10,host=klient.example \
| grep -E "^(allowtcpforwarding|disableforwarding|permitopen|permitlisten|gatewayports) "If the server reports administratively prohibited, do not blindly open forwarding to all accounts. Compare the effective configuration, key restrictions used, and server logs. Disabling port forwarding alone does not prove that a user with a full shell cannot forward traffic through another program. Security boundaries must also correspond to what commands and network operations the account is allowed to perform.
Don't confuse GatewayPorts with the client's -g. One is about server behavior for remote redirects, the other is about client listening for local redirects. Explicit address in the command and subsequent socket checking is more understandable than relying on default settings inherited from multiple configuration files.
Diagnostics: a created port does not mean a functional application
Divide the check into three steps: does SSH connect, does the correct listener exist, and does the target application respond? For local forwarding, check the listener on the client, for remote forwarding, check the listener on the server. If the port does not exist, look for a conflict, a ban, or an incorrect bind address. If it does exist but requests fail, check the path and service on the other side.
# Detailní záznam lokálního forwardingu:
ssh -vvv -N -T -o ExitOnForwardFailure=yes \
-L 127.0.0.1:18080:127.0.0.1:8080 spravce@server.example
# Linux: kontrola lokálního listeneru v druhém terminálu:
ss -lntp | grep ":18080"Connection refused from the target usually means that no one is listening at the intended address or someone is actively refusing the connection. Timeout requires checking routing and filtering. HTTP 401 or 403 may already be the response of the web application itself: the tunnel as a transport may work correctly. In case of a certificate error, check the name and trustworthiness, do not disable authentication just to achieve a 200 code.
Operation, termination and verification of the result
A long-lived tunnel needs an owner, a description of the destination, and a way to check its health. You can add ServerAliveInterval and ServerAliveCountMax to let the client know about a failed connection. However, this is not an automatic service repair or tunnel recovery. If you require persistent operation, use approved process management, auditing, and specific access rules instead of a forgotten command in a personal terminal.
CH-J Tools is used for working with the SSH server and diagnostics; the switches and configuration files here belong to the OpenSSH client. The command in the manual cannot be used to infer the same support or the same loading of ~/.ssh/config in every graphical application. Check the capabilities of the client version you are currently using and do not publish the service with another tool just because of a missing button.
- After the check is complete, terminate the tunnel process and verify that the listener is gone.
- Make sure you haven't left a public listen or broad firewall rule for the temporary test.
- Record who is allowed to connect and which section is protected by SSH and which by TLS.
- When reusing, verify the destination and port; the same number may be in use by another service.
Frequently asked questions
What does localhost in the -L command indicate?
The bind address before the local port belongs to the client. The destination address after the local port is evaluated on the SSH server side.
Is remote forwarding automatically public?
No. Public availability is determined by the bind address, server GatewayPorts, network path, and firewall. The example in the article uses a remote loopback.
Does ExitOnForwardFailure confirm database availability?
No. It confirms the success of creating the requested redirect. The target service may fail only on a later connection attempt.
Is a SOCKS tunnel a replacement for a VPN?
Not generally. It is only used by configured applications and regular SSH TCP forwarding does not forward arbitrary UDP or ICMP traffic.
Detailed documentation
OpenSSH: local, remote and dynamic redirection →
OpenSSH: client forwarding configuration →
OpenSSH: server forwarding limitations →