You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GatewayPorts - "Specifies whether remote hosts are allowed to connect to ports forwarded for the client. By default, sshd(8) binds remote port forwardings to the loopback address. This prevents other remote hosts from connecting to forwarded ports. GatewayPorts can be used to specify that sshd should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect."
背景
出于安全考虑公网IP仅暴露了内网跳板机的22端口,想借用ssh端口转发实现简易内网穿透。
思考
可以通过ssh隧道实现tcp端口转发介绍了ssh正向转发(local port forward)
场景一:通过跳板机的 ssh 端口, 把内网局域网的端口转发到本机的8080 端口
比较适合在外网访问内网资源,如内网http服务
假设跳板机的 ssh 端口通过端口映射暴露在公网123.45.67.89:2222
ssh -L 8080:192.168.1.4:80 jump@123.45.67.89 -p 2222
可通过ssh将内网的192.168.1.4:80(服务器所在网络)转发至本机的8080
对于明文传输http也起到了加密作用,适用于不安全的网络环境
而这次的目的是将本地端口转发至内网,实现内网对本地的远程访问
如果使用 ssh key 登录,把服务器上的 localhost:8008 端口 转发到本机的 8080 端口, 可以这样运行:
ssh -i ~/data/xxx.pem -N -f -L 8080:localhost:8008 root@123.45.67.89
场景二: 将本地的80端口转发到跳板机的 8080 端口
方法
使用ssh自带的反向转发(remote port forward)
ssh -R 80:localhost:8080 jump@123.45.67.89 -p 2222
上述命令通过ssh将本地的80端口转发至跳板机的8080端口
但默认情况下转发端口仅允许本地访问,不对局域网开放,需要修改GatewayPorts参数位于/etc/ssh/sshd_config
GatewayPorts - "Specifies whether remote hosts are allowed to connect to ports forwarded for the client. By default, sshd(8) binds remote port forwardings to the loopback address. This prevents other remote hosts from connecting to forwarded ports. GatewayPorts can be used to specify that sshd should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect."
取消注释GatewayPorts并修改默认值no为yes后重启sshd服务即可生效。该设置允许转发后的端口绑定在0.0.0.0上,确保从每个网卡的ip上都能访问到8080服务。
SSH参数说明:
即使通过ssh -Nf参数后台执行,使得 ssh 会话在后台执行。上述方法缺点依然明显,session断开后端口转发随之消失。
-N:指定这个SSH连接只进行端口消息转发,不执行任何SSH远程命令;
-L:指定本地监听的地址和端口;
-f: 这个SSH会话放入后台运行,不加这个参数的话,当退出当前SSH -L指定的终端时,端口转发进程就结束了,端口转发送也就结束了。所以务必要加上-f参数。
The text was updated successfully, but these errors were encountered: