随便bb
在远程开发过程中,可能遇到这样的问题:通过自己所在局域网连接 公司或者学校的内网,如果你在学校/公司内网或者vpn还行,否则是无法访问的。对此可以利用ssh的远程转发。
现假设本地机为A,公网跳板机为B,目标局域网机为C。目标是A 通过 B ssh连接C
两个ssh链接
本机A >> 公网跳板机B
目标主机C >> 公网跳板机B
#生成秘钥 在~/.ssh/id_rsa 以及 id_rsa.pub
ssh-keygen
#把公钥扔到目标机 第一次要输下密码
ssh-copy-id <user>@<ip>
开启公网跳板机B权限
vim /etc/ssh/sshd_config
# 修改 GateWayPort yes
service sshd restart
# 然后记得重启ssh-server
在公网跳板机安全组放行一个端口供ssh隧道使用。如下
# 在C机启动ssh远程转发之后 如果成功可以看到 B机防火墙显示隧道端口正常,否则是未使用状态
# 也可以通过lsof -i:<port> 查看
目标主机C远程转发
# -M 监控
# -f 后台运行
# -C 压缩
# -N 非登录
# -R 远程转发
# -L 本地转发
autossh -M <监控端口> -fCNR <远程ip>:<远程端口>:<本地ip localhost>:<本地端口> <remote-server 例:root@xx.xx.xx.xx>
#青春版
ssh -R <远程ip>:<远程端口>:<本地ip localhost>:<本地端口> <remote-server 例:root@xx.xx.xx.xx>
本地ssh目标主机
ssh <目标主机username>@<跳板机ip> -p <ssh隧道端口>