bind_address in ssh -L and ssh -R: localhost, * and GatewayPorts
The optional first field of ssh -L and -R picks the address a forwarded port listens on. What localhost, * and an IP mean, and how GatewayPorts decides.
Both kinds of forward listen on a port. The optional bind_address in front of the port says on which address of that machine:
ssh -L [bind_address:]port:host:hostport user@server
ssh -R [bind_address:]port:host:hostport user@server
Local forwards (-L): the address on your machine
| You write | The port listens on | |---|---| | -L 8080:db:5432 | decided by GatewayPorts in the client config: loopback by default | | -L localhost:8080:db:5432 | loopback only: programs on this machine | | -L '*:8080:db:5432' or -L :8080:db:5432 | all interfaces: other machines can connect | | -L 192.0.2.10:8080:db:5432 | that one address |
GatewayPorts yes in ssh_config, or -g on the command line, makes forwards without a bind_address listen on all interfaces. Its default is no, which binds them to loopback. Quote * so the shell does not expand it, and put an IPv6 address in square brackets.
Remote forwards (-R): the address on the server
By default the server binds remote forwards to its loopback address only, whatever you write. A bind_address is honoured only if the server allows it: GatewayPorts in the server's sshd_config decides.
| sshd_config GatewayPorts | Result | |---|---| | no (default) | loopback only, whatever the client asks for | | yes | all interfaces, always | | clientspecified | the client's bind_address is used: -R '*:8080:localhost:3000' for all interfaces, or an address |
The client's GatewayPorts has no effect on remote forwards.
Check what is listening
On a Linux machine, ss -tln lists listening TCP sockets with their addresses. 127.0.0.1:8080 or [::1]:8080 means loopback only; 0.0.0.0:8080, [::]:8080 or *:8080 means all interfaces. Add -p to see which process owns each one.
With *, other machines that can reach that address can use the forward, and through it reach the target.
This app has no bind address setting: its remote forward always asks the server to listen on 127.0.0.1, and its local forward has no bind address field. See Port forwarding.
Related
- Port forwarding: local, remote, dynamic
- SSH "remote port forwarding failed for listen port": why ssh -R is refused
- ssh -R with port 0: let the server pick the port, and find out which one
- SSH port forwarding through a jump host: -J with -L