Course contentsShow
← Security
Lesson 501 of 3,05211. Network Security - VPNs and TunnelingFree lesson

Remote Port Forwarding (-R)

Setting up remote port forwards to expose local services to remote networks, including reverse tunneling scenarios.

Remote Port Forwarding (-R)

What you'll learn: How to use SSH's -R flag to expose a service running on your local machine to users on a remote network—perfect for demos, testing, or accessing home services from anywhere.


The Core Idea

Remote port forwarding is the opposite of local forwarding. Instead of bringing a remote service to your local machine, you're pushing a local service out to a remote server, making it accessible from the remote network.

Think of it like setting up a back door: you're opening a "window" on a remote server that tunnels connections back to your local machine. This is incredibly useful when you're behind a firewall or NAT and can't accept incoming connections directly, but you still want to share a service (like a web server on your laptop) with someone on the internet.

How It Works

The syntax mirrors local forwarding but reverses the flow:

ssh -R [remote_port]:localhost:[local_port] user@remote_host

Example: You're developing a web app on localhost:8080 and want your colleague on remote-server.com to test it:

ssh -R 9000:localhost:8080 user@remote-server.com

Now, anyone on remote-server.com who connects to localhost:9000 gets tunneled through SSH back to your machine's port 8080.

Reverse Tunneling for Access

This technique is also called reverse tunneling when used to bypass restrictive firewalls. If you have SSH access out to a server you control, you can expose internal services externally without opening inbound firewall rules.

Security note: By default, remote forwarded ports only bind to localhost on the remote server (for safety). To allow external connections, the remote SSH server needs GatewayPorts yes configured.


Key Takeaway: Remote port forwarding (-R) tunnels local services to remote networks, enabling reverse access through firewalls and NAT without complex network changes.