利用腾讯云主机+SSH远程端口转发实现内网穿透

简介: 利用腾讯云主机+SSH远程端口转发实现内网穿透

640.jpg

SSH远程端口转发介绍


之前有介绍SSH远程端口转发,可以参考之前的文章

Linux SSH端口转发实践

利用MobaXterm图形化配置SSH端口转发

man ssh 查看-R远程端口转发的相关详细介绍


ssh -R [bind_address:]port:host:hostport
 Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the given host
 and port, or Unix socket, on the local side.  This works by allocating a socket to listen to either a TCP port or to a Unix socket
 on the remote side.  Whenever a connection is made to this port or Unix socket, the connection is forwarded over the secure channel,
 and a connection is made to either host port hostport, or local_socket, from the local machine.
 Port forwardings can also be specified in the configuration file.  Privileged ports can be forwarded only when logging in as root on
 the remote machine.  IPv6 addresses can be specified by enclosing the address in square brackets.
 By default, TCP listening sockets on the server will be bound to the loopback interface only.  This may be overridden by specifying
 a bind_address.  An empty bind_address, or the address ndicates that the remote socket should listen on all interfaces.
 Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled (see sshd_config(5)).
 If the port argument is he listen port will be dynamically allocated on the server and reported to the client at run time.
 When used together with -O forward the allocated port will be printed to the standard output.

需求场景


如下图所示

1)内网服务器CentOS7.6 IP 192.168.198.130 (该机器可以访问互联网)

2)腾讯云主机IP地址119.45.157.198


640.jpg


通过远程工具例如向日葵或者teamviewer运维起来非常卡顿  


这时可以将其SSH端口以及MySQL 3306端口通过ssh 远程端口转发到云主机上, 然后直接登录云主机后访问转发后的端口进行运维

640.png


具体步骤


登录本地内网服务器SSH后


ssh -N -f -R 8806:127.0.0.1:3306 root@119.45.157.198

640.jpg


640.jpg


在云主机端

netstat -anp | grep 8806

说明已经成功转发到8806端口了 这时可以直接访问8806端口

mysql -uroot -p -h 127.0.0.1 -P 8806

效果图所示


640.jpg


同样方法转发本地内网服务器的22端口到云主机8122端口

ssh -N -f -R 8122:127.0.0.1:22 root@119.45.157.198

640.jpg

在远程的云主机端

ssh 127.0.0.1 -p 8122


640.jpg

问题


但是这样只能先SSH登录到云主机后台,只能云主机本地访问

因为转发后的端口都是监听在127.0.0.1 loopback网卡上

要想其它机器也能访问,需要sshd服务开启GatewayPorts


方法如下

vi /etc/ssh/sshd_config
将#GatewayPorts no改为GatewayPorts yes
systemctl restart sshd.service

修改前

image.png

修改后

640.png


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.  The argument may be no to force remote port forwardings to be available to the local host only, yes to force remote
port forwardings to bind to the wildcard address, or clientspecified to allow the client to select the address to which the
forwarding is bound.  The default is no.


然后将云主机上转发生成的sshd进程kill掉,别把所有sshd服务kill掉了,不然这里ssh会话就连不上了,得云主机控制台vnc上重启sshd服务了


然后再内网机器重新配置转发命令


ssh -N -f -R 8122:127.0.0.1:22 root@119.45.157.198


640.jpg

另外云主机安全组要放开转发出来的端口8122

640.jpg

这时就可以直接访问云主机的8122,内网穿透成功


640.jpg

SecureCRT的远程端口转发


SecureCRT也可以直接配置远程端口转发

640.jpg

640.jpg

640.jpg


640.jpg


配置了转发,可以看到云主机SSHD服务已经在监听8622和8806端口

但是发现从公网访问出现如下报错


640.jpg

640.jpg

但SSH可以通过ssh 0.0.0.0 -p 8622可以访问

640.jpg


目前该问题未解决,待研究


关于SSH端口转发可以参考 https://nnc3.com/mags/Networking2/ssh/ch09_02.htm

相关文章
|
9月前
|
JSON 测试技术 API
优化你的 REST Assured 测试:设置默认主机与端口、GET 请求与断言
REST Assured 是一个强大的 Java 库,用于简化 RESTful API 测试。本文详解了其核心功能:设置默认主机和端口以减少代码重复、发起 GET 请求并验证响应结果,以及通过断言确保接口行为符合预期。同时推荐 Apipost 工具,助力开发者提升 API 测试效率,实现更高效的接口管理与团队协作。掌握这些技巧,可显著优化测试流程与代码质量。
|
10月前
|
安全 Linux 网络安全
CentOS 8下SSH连接超时与“无法加载主机密钥”错误的排查与修复
在CentOS 8系统中,宝塔面板提示“出错了,面板运行时发生错误”,导致插件无法正常显示。同时,SSH连接超时,修复面板功能失效。通过VNC连接排查,发现SSH服务安装和配置问题频发,最终通过重装SSH、调整权限并重新生成主机密钥文件解决问题,成功恢复SSH连接。
710 16
|
Ubuntu 网络安全 数据安全/隐私保护
如何在 Ubuntu 上创建一个 SSH CA 以验证主机和客户端
如何在 Ubuntu 上创建一个 SSH CA 以验证主机和客户端
336 0
|
安全 数据挖掘 Shell
SSH安全远程登录与端口转发
**SSH 概述**:SSH 是开发者必备工具,用于安全远程登录及文件传输。本文聚焦SSH的两大功能:**密钥登录**和**端口转发**。
368 0
|
网络安全 Python
Python编程--目标IP地址段主机指定端口状态扫描
Python编程--目标IP地址段主机指定端口状态扫描
199 1
|
网络协议 Linux 网络安全
在Linux中,如何将本地 80 端口的请求转发到 8080 端口?当前主机 IP 为10.0.0.104。
在Linux中,如何将本地 80 端口的请求转发到 8080 端口?当前主机 IP 为10.0.0.104。
|
网络协议 关系型数据库 MySQL
ssh端口转发
ssh端口转发
345 3
|
安全 Linux 网络安全
|
网络协议 Ubuntu Linux
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
|
安全 Linux 网络安全
在Linux中,使用rsync同步数据时,假如采用的是ssh方式,并且目标机器的sshd端端并不是默认的22端口,该如何做?
在Linux中,使用rsync同步数据时,假如采用的是ssh方式,并且目标机器的sshd端端并不是默认的22端口,该如何做?

热门文章

最新文章