ssh -gfnNTR x.x.x.x:222:localhost:222 root@x.x.x.x -o ServerAliveInterval=300
Note: x.x.x.x是我的ECS服务器公网IP localhost是指ssh client自己的127.0.0.1 .
-o ServerAliveInterval=300
的意思是让ssh client每300秒就给server发个心跳,以免链路被RST.
-f Requests ssh to go to background just before command execution.
让该命令后台运行 .
-n Redirects stdin from /dev/null (actually, prevents reading from stdin).
-N Do not execute a remote command.
不执行远程命令 .
-T Disable pseudo-tty allocation.
不占用 shell .
-g Allows remote hosts to connect to local forwarded ports.
允许非本机地址(任何公网IP)连接x.x.x.x的2333端口.{see man sshd_config(5)}.
这个选项非常重要, 要让这个选项生效需要在x.x.x.x服务器(ssh server)上编辑/etc/ssh/sshd_config 添加一行GatewayPorts yes 然后保存退出并 service ssh restart.
整个命令这样理解: 公司机器告诉ECS说 你给我监听一下你的IP的2333端口,把这个2333端口的所有数据都发到我这的127.0.0.1的22端口.
然后我在家里用putty设置IP为ECS的x.x.x.x 端口为2333, 顺利连上了公司的机器 ,并且ssh登录成功.