Comprehensive guide to creating and using SSH tunnels for port forwarding, proxying, and secure communication
ssh -L 8080:target-server:80 user@ssh-server
This command forwards local port 8080 to port 80 on target-server through ssh-server. You can access the target server by connecting to localhost:8080.
ssh -L 8080:target-server-1:80 -L 8443:target-server-2:443 user@ssh-server
This command forwards local port 8080 to port 80 on target-server-1 and local port 8443 to port 443 on target-server-2 through ssh-server.
ssh -L 192.168.1.10:8080:target-server:80 user@ssh-server
This command binds the local port forwarding to a specific interface (192.168.1.10) instead of all interfaces.
ssh -fN -L 8080:target-server:80 user@ssh-server
This command runs the SSH tunnel in the background (-f) and does not execute a remote command (-N).
ssh -R 8080:localhost:80 user@ssh-server
This command forwards remote port 8080 on ssh-server to port 80 on localhost. Users on ssh-server can access your local server by connecting to localhost:8080.
ssh -R 8080:localhost:80 -R 8443:localhost:443 user@ssh-server
This command forwards remote port 8080 to port 80 on localhost and remote port 8443 to port 443 on localhost.
ssh -R 0.0.0.0:8080:localhost:80 user@ssh-server
This command binds the remote port forwarding to all interfaces on ssh-server, allowing external connections. Note that this requires GatewayPorts yes in the SSH server configuration.
ssh -D 8080 user@ssh-server
This command creates a SOCKS proxy on local port 8080 through ssh-server. You can configure your applications to use this proxy to access resources through ssh-server.
ssh -D 192.168.1.10:8080 user@ssh-server
This command binds the SOCKS proxy to a specific interface (192.168.1.10) instead of all interfaces.
# Edit /etc/ssh/sshd_config GatewayPorts no
This setting prevents remote port forwards from binding to non-loopback addresses, which could expose services to the internet.
# Generate SSH key ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 # Copy SSH key to server ssh-copy-id -i ~/.ssh/id_ed25519.pub user@ssh-server # Edit /etc/ssh/sshd_config PasswordAuthentication no
Using SSH keys instead of passwords improves security by requiring possession of the private key to authenticate.
Understanding common SSH exploitation methods and how to defend against them
Learn moreOverview of tools used for SSH reconnaissance, scanning, and exploitation
Learn more