What is the difference in functionality between the two? I'm a bit confused by it.
Local forwarding makes a remote port locally available.
Remote forwarding makes a local port remotely available.
But this 'availability' will work in both directions... or does it?
E.g. the following (issued from a host 'home')
ssh -R 1234:localhost:2345 user@workThis will establish a secure tunnel between work::1234 and home::2345, right?
If I put in anything on one end, it will come out on the other end.
But then, I can achieve the same by the following call from the host 'work':
ssh -L 1234:localhost:2345 user@homeSo, the only difference is the where I call it from, correct?
15 Answers
The major practical difference, is that if connecting 2 computers A and B, and B is behind a firewall or NAT Router that you don't control, and it's blocking incoming.. You're sitting at A. You can't get A to connect to B. But B won't block outgoing.. So you get B to connect to A.
--added clarification--
The above, which the questioner understood.. means the major practical difference between local and remote forwarding. ssh -L and ssh -R when you'd use each. I wasn't commenting on the specific example commands he gave, where he switches -L and -R, and which sshd server he connects to. But now I will attempt to comment on it.. With the ssh commands he gave, from the perspective of the regular client and regular server, there appears to be no difference,as it doesn't say "ah this is an ssh client and this is an ssh server.." it doesn't know ssh , and which is the client/server aspect of ssh is irrelevant and unknown to the regular client and regular server too. They just care about who is listening, and from their perspective, it looks the same. The work computer is listening and on 1234. They don't notice that in one case it's an sshd.exe ssh server, and in the other case it's an ssh.exe, ssh client. By the way, where the ssh client is, is considered local.
2Yes, if I understand it correctly, local port forwarding from a to b should be identical to remote port forwarding from b to a (and vice versa). An outgoing tunnel from a to b (viewed from a) should be equal to an incoming tunnel from a to b (viewed from b).
Local port forwarding creates an outgoing tunnel which can be used to bring a public internet computer to local machine. A local user can access a remote host:port combination on a local host, because the given port on the local (client) host is forwarded to the given host and port on the remote side:
ssh -L local_port:remote_host:remote_port user@hostnameRemote port forwarding creates an incoming tunnel which can be used to bring a local computer into the public internet. An internet user can access a certain local host:port combination on a remote host. The given port on the remote (server) host is forwarded to the given host and port on the local side:
ssh -R local_port:remote_host:remote_port user@hostname 3 With local port forwarding you (the client) open a listening socket on your computer and connect your application-level protocol client to this socket. Now the connection is forwarded over SSH to the server. The server connects to the remote host and tunnels the data from your protocol client to the final destination.
With remote port forwarding the server opens a listening socket on the server host. Some remote application connects to this host and sends information which is transferred to your client computer. Here the connection is established to the final destination (some application-level protocol server running on your computer or on your network) and the data is transferred from remote application to the final destination.
1This is best understood with small examples. In these exmples the connections are structured like this:
Local Server--(LAN)--Terminal-----(SSH,usually via Internet)-----Tunnel Endpoint--(LAN)--Remote Machine
You use local port forwarding if you want to tunnel to a certain remote machine/port, reachable by the tunnel endpoint you have ssh access to. Therby, that remote machine's port is also accessible locally on your own terminal, i.e. http:// localhost:terminal_port /.
That is done using the following syntax:
ssh -L terminal_port:remote_machine_ip:remote_service_port -p tunnel_endpoint_ssh_port ssh-login@tunnel_endpoint_ipYou may use remote port forwarding if you want to enable ANYONE(!) remote, who is able to reach the tunnel endpoint's listening port to be able to remotely access an ip/port in your local LAN. To your local server, it looks like the connection from the remote machine are initiated by the terminal.
The Syntax is:
ssh -R tunnel_endpoint_listen_port:local_server_ip:local_server_port -p tunnel_endpoint_ssh_port ssh-login@tunnel_endpoint_ip The manpage for socat clarifies this very well. Yes, I know ssh and socat are two entirely different things - but socat's documentation is just very good.