ssh内网穿透(家庭网络ssh访问公司内网机器)

环境

一台内网机器
一台公网机器(阿里云的)
一台个人电脑

目的

个人电脑通过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
    在个人电脑上,使用内网机器的账号密码,建立信任关系

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值