ssh -gfnNTR x.x.x.x:222:localhost:222 [email protected] -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登錄成功.