Sobes.tech
Junior

What is port forwarding?

sobes.tech AI

Answer from AI

Port forwarding is a network technique that redirects incoming network packets destined for a specific port and IP address to another IP address and port within a local network.

This allows external devices to access services hosted on internal devices, which are usually hidden behind NAT (Network Address Translation).

Main types of port forwarding:

  • Static NAT: A fixed forwarding rule that permanently associates an external port with an internal port on a specific internal IP address.
  • Dynamic NAT: More commonly used for outbound traffic, where a group of internal IP addresses is mapped to one or more external IP addresses and ports dynamically. In the context of "forwarding," it usually refers to static forwarding for incoming traffic.
  • SSH Port Forwarding: Using SSH tunnels for secure traffic redirection. It can be:
    • Local: Redirects a port from the local machine to a remote service via an SSH server.
    • Remote: Redirects a port from a remote machine to a local service via an SSH server.
    • Dynamic (SOCKS proxy): Creates a SOCKS proxy, allowing tunneling traffic to multiple addresses and ports.

Used for:

  • Accessing web servers, SSH, FTP, game servers, and other services within a local network from outside.
  • Implementing VPN.
  • Circumventing firewalls and NAT in certain scenarios (e.g., with SSH).

Example of a rule on a router (conceptually):

Incoming Interface External IP External Port Protocol Internal IP Internal Port
WAN 203.0.113.10 80 TCP 192.168.1.100 80
WAN 203.0.113.10 2222 TCP 192.168.1.101 22

Example of SSH local port forwarding:

# Local port 8080 is forwarded to port 80 on webserver.example.com
ssh -L 8080:webserver.example.com:80 user@ssh.example.com

Example of SSH remote port forwarding:

# Remote port 8080 on ssh.example.com is forwarded to port 80 on the local machine
ssh -R 8080:localhost:80 user@ssh.example.com