环境
一台内网机器
一台公网机器(阿里云的)
一台个人电脑
目的
个人电脑通过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 root@xxx.xxx.xxx.xxx -p 8086
xxx.xxx.xxx.xxx 是公网的ip,注意用户名和密码用的是内网机器的
或者
ssh -t root@public_ip ssh root@127.0.0.1 -p 8086设置无密码登录
ssh-copy-id root@101.132.141.202 -p 8086
在个人电脑上,使用内网机器的账号密码,建立信任关系