環境
一臺內網機器
一臺公網機器(阿里雲的)
一臺個人電腦
目的
個人電腦通過ssh訪問內網機器
個人電腦—》 公網機器—-》 內網機器
步驟
開啓公網機器的8086端口(ECS服務器在安全組中設置打開8086端口)
在公網機器上加入
vi /etc/ssh/sshd_config
GatewayPorts yes
然後:systemctl restart ssh.service
注意是sshd_config, 不是ssh_config
不開啓此選項,只能公網機器自己訪問內網機器(監聽的是localhost),個人計算機無法直接ssh內網機器在內網機器上:
ssh -gfnNTR 0.0.0.0:8086:localhost:22 root@x.x.x.x -o ServerAliveInterval=300
這裏用的是公網的用戶名和密碼
Note: x.x.x.x是我的ECS服務器公網IP
-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.
在個人電腦上訪問內網機器
ssh [email protected] -p 8086
xxx.xxx.xxx.xxx 是公網的ip,注意用戶名和密碼用的是內網機器的
或者
ssh -t root@public_ip ssh [email protected] -p 8086設置無密碼登錄
ssh-copy-id [email protected] -p 8086
在個人電腦上,使用內網機器的賬號密碼,建立信任關係