ssh的三个强大的端口转发命令:
ssh -C -f -N -g -L listen_port:DST_Host:DST_port -l user Tunnel_Host
ssh -C -f -N -g -R listen_port:DST_Host:DST_port -l user Tunnel_Host
ssh -C -f -N -g -D listen_port -l user Tunnel_Host

I) 先看看linux下面ssh命令的相关帮助:
# ssh -help
……
-l user Log in using this user name.
用来登录sshd服务器的用户名,相当于user@remote-host。
-f Fork into background after authentication.
后台认证用户/密码,通常和-N连用,不用登录到远程主机。
-p port Connect to this port. Server must be on the same port.
被登录的ssd服务器的sshd服务端口。
-L listen-port:host:port Forward local port to remote address
在本地创建监听端口,一旦有连接建立到该端口,就转发到host:port。
-R listen-port:host:port Forward remote port to local address
在远程创建监听端口,一旦有连接建立到该端口,就转发到host:port。
These cause ssh to listen for connections on a port, and
forward them to the other side by connecting to host:port.
-D port Enable dynamic application-level port forwarding.
在本地建立监听端口,一旦有连接建立到该端口,动态转发,相当于创建了socks代理服务器。
-C Enable compression.
压缩数据传输。
-N Do not execute a shell or command.
不执行脚本或命令,通常与-f连用。
-g Allow remote hosts to connect to forwarded ports.
在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。注:这个参数我在实践中似乎始终不起作用,参见III)
……

II) 参见:http://cmpp.linuxforum.net/cman-html/man1/ssh.1.html
-L port:host:hostport
将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 同时远程主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-R port:host:hostport
将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的, 远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转向出去, 同时本地主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-D port
指定一个本地机器 “动态的” 应用程序端口转发. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, 将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.

III) ssh port forward相关配置
一般sshd配置文件路径为/etc/ssh/sshd_config
添加一行:
GatewayPorts Yes
则允许远程主机连接到转发端口,相当于-g

最后是我的理解:
Localhost–>Localhost:listen_port–ssh(encrypt)–>Tunnel_Host–(unencrypt)–>DST_Host:DST_port
Tunnel_Host–>Tunnel_Host:listen_port–ssh(encrypt)–>DST_Host:DST_port
Localhost–>Localhost:listen_port–ssh(encrypt)–>Tunnel_Host–(unencrypt)–>AnyHost:Any_port

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*