Six months ago, our infrastructure team began tightening security across all 40 internal servers. At the time, we still had several machines running CentOS 7 that needed migration to AlmaLinux. Simultaneously, I deployed four new CentOS Stream 9 node clusters to serve as jump hosts and staging environments.
That is when issues started cropping up. Developers kept asking to expose MySQL (3306) and Redis (6379) ports to the internet for urgent debugging. Opening public ports directly—even with IP whitelisting—carries significant security risks. On the other hand, setting up a dedicated OpenVPN or WireGuard cluster just for 3–4 remote engineers was both overly complex and tedious to maintain. That was when SSH Tunneling proved to be the quickest, cleanest, and most secure solution, which we have been running stably for the past six months.
Quick Start: Open a Tunnel to Access an Internal Database in 1 Minute
Suppose you need to connect to MySQL bound to 127.0.0.1:3306 on a CentOS Stream 9 server. Without installing any third-party tools, simply open the terminal on your local machine and run:
ssh -N -L 33306:127.0.0.1:3306 [email protected] -p 22
How does this syntax work?
-N: Do not open a remote command shell. This instructs SSH to only keep the connection open for port forwarding.-L 33306:127.0.0.1:3306: Opens port33306locally. All traffic sent here will be forwarded directly to127.0.0.1:3306on the remote server.[email protected]: The SSH user account and the public IP address of the CentOS Stream 9 server.
After running this command, the terminal will hang to maintain the connection. Now, open DBeaver or MySQL Workbench, configure Host: 127.0.0.1 and Port: 33306, and you can query the database seamlessly as if it were running directly on your local machine.
3 Port Forwarding Methods You Need to Know
At its core, SSH Tunneling encapsulates application traffic inside an encrypted SSH connection. Depending on the traffic flow direction, it is categorized into three primary types:
1. Local Port Forwarding (ssh -L): Pulling Remote Services to Local
This is the connection method shown in the Quick Start section. From your local machine, you connect to a service hidden behind a firewall or bound strictly to the loopback interface on the target server. For web-based remote administration without desktop clients, tools like Apache Guacamole to manage RDP, SSH, and VNC via your browser offer another convenient browser-first alternative.
Traffic flow diagram:
[Local Machine: port A] --(encrypted SSH channel)--> [CentOS Stream 9] ----> [Target Host: port B]
The Jump Host (Bastion Host) pattern is the most common use case in our infrastructure. The CentOS Stream 9 instance holds a public IP and acts as a gateway. Meanwhile, the production PostgreSQL cluster sits deep within the private network at 10.0.0.15:5432. In this scenario, you simply execute on your local machine:
ssh -N -L 5433:10.0.0.15:5432 [email protected]
The Bastion Host acts as the intermediary receiving packets over the SSH tunnel. It then decrypts and forwards them across the internal network to 10.0.0.15:5432. The entire private network remains fully isolated.
2. Remote Port Forwarding (ssh -R): Exposing Local Services to a Public Server
A frequent scenario: you are developing a webhook locally on port 8080 to receive payment callbacks from VNPay or Stripe. The payment gateway requires a publicly accessible callback URL. However, your laptop is behind a home router NAT, making direct external access impossible.
Instead of paying for an ngrok subscription, you can turn your CentOS Stream 9 VPS into a public proxy that forwards traffic to you.
By default, the SSH server prevents binding ports to public network interfaces. First, open the SSH configuration file on CentOS Stream 9:
sudo nano /etc/ssh/sshd_config
Modify or add the following directive to allow external port binding:
GatewayPorts yes
Restart the SSH daemon to apply the changes:
sudo systemctl restart sshd
Next, run this command from your laptop:
ssh -N -R 9000:localhost:8080 [email protected]
Any incoming request to http://203.0.113.10:9000 will be captured by the server and tunneled back over SSH straight to port 8080 on your local machine.
3. Dynamic Port Forwarding (ssh -D): Turning Your Server into a Personal SOCKS5 Proxy
When working from coffee shops on unsecured public Wi-Fi networks, the threat of eavesdropping and Man-in-the-Middle attacks is always present. Dynamic Tunneling lets you route all your web traffic securely through your remote server with minimal effort.
On your personal machine, simply run:
ssh -N -D 1080 [email protected]
This command turns port 1080 on your laptop into a local SOCKS5 proxy. Next, open a browser extension like SwitchyOmega in Chrome or your OS network settings, and set the proxy to 127.0.0.1:1080. All your web browsing will now pass through the CentOS Stream 9 server, masking your IP with the server’s public IP instead of the coffee shop Wi-Fi IP.
Maintaining Stable Tunnels Without Interruptions
Running commands manually in the terminal is fine for a quick 10–15 minute test. However, when you need an active tunnel throughout an 8-hour workday, you will frequently encounter network drops, frozen terminal sessions, or broken connections whenever you close your laptop lid.
1. Save Configurations in ~/.ssh/config
Remembering exact port numbers and command-line flags is tedious and error-prone. It is best practice to consolidate your configurations into ~/.ssh/config on your local machine:
Host staging-tunnel
HostName 203.0.113.10
User deploy
Port 22
IdentityFile ~/.ssh/id_rsa
LocalForward 33306 127.0.0.1:3306
LocalForward 6380 10.0.0.16:6379
ServerAliveInterval 30
ServerAliveCountMax 3
Next time you want to connect, you only need to run a single concise command:
ssh -f -N staging-tunnel
The -f flag instructs SSH to fork into the background immediately after successful authentication, instantly freeing up your terminal for other tasks.
2. Prevent Dropped Connections with Autossh
Under unstable network conditions, the default SSH process often hangs as a zombie connection without properly disconnecting to trigger a reconnect. The practical solution I implemented for our team is autossh:
# Install on Ubuntu / Debian
sudo apt install autossh -y
# Install on macOS
brew install autossh
# Run self-healing tunnel
autossh -M 0 -f -N -o "ServerAliveInterval 15" -o "ServerAliveCountMax 3" -L 33306:127.0.0.1:3306 [email protected]
The -M 0 flag disables autossh’s base monitoring port mechanism, relying instead on SSH’s built-in heartbeat packets (ServerAliveInterval 15). The client sends a keepalive probe every 15 seconds; if 3 consecutive probes fail (45 seconds total), autossh automatically terminates the stale connection and establishes a fresh tunnel. If you manage long-running background workers or scheduled tunnels in production, pair this with creating a systemd service or timer to ensure automated restarts after reboot.
Practical Lessons from Six Months in Production
Through operating real CentOS Stream 9 environments, here are three essential operational takeaways:
- Strictly disable shell access for port forwarding accounts: Never grant an interactive shell or
sudoprivileges to developers just for port forwarding. Instead, create a dedicated account with shell access completely disabled:sudo useradd -s /sbin/nologin -M tunneluser sudo passwd tunneluserNext, edit
/etc/ssh/sshd_configand add the following hardening block at the end of the file:Match User tunneluser AllowTcpForwarding yes X11Forwarding no AllowAgentForwarding no ForceCommand /bin/falseThis user is restricted purely to port forwarding. Even if credentials or SSH keys are compromised, an attacker cannot execute any commands on the host system. For additional layer of authentication on bastion gateways, consider configuring two-factor authentication (2FA) for SSH.
- Resolving “Address already in use” errors: This error occurs when a local port is still held open by an orphaned SSH process. You can quickly identify the PID and clean up the stale process with a single command:
lsof -ti:33306 | xargs kill -9 - Watch out for SELinux with Remote Forwarding: CentOS Stream 9 runs SELinux in Enforcing mode by default. Even when
GatewayPorts yesis configured, SELinux can block SSH from binding to unpermitted ports. If you encounter permission-denied binding errors, check authorized SSH ports viasemanage port -l | grep ssh_port_tor inspect audit logs by using Linux auditd to monitor security events at/var/log/audit/audit.logto grant the required policy exceptions rather than hastily disabling SELinux.
Mastering SSH Tunneling saves you the overhead of third-party tooling while keeping your servers securely protected against continuous internet port scanners.

