SSH 反向端口转发-R

近期和同学聊到了他遇到的一个问题:

本地机器A,远程开发机C机器A需要通过ssh连接开发机C进行开发,ssh直连不通,SCP传输文件的话就比较费劲,需要通过跳板机B,经过跳板机这一步骤可用,但没那么方便,是不是有什么办法可以绕过跳板机

SSH的反向端口转发可以实现

ssh(1) - Linux man page,有一个R参数

-R

bind_address:\]port:host:hostport Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side. This works by allocating a socket to listen to port on the remote side, and whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the local machine

下面这句话的意思是,指定远程主机的某个端口,让其监听的数据转发到本地指定的主机和端口

Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.

整个过程如下:

  1. C执行ssh -R,建立一条到Assh隧道;
  2. A起socket,监听本地端口,等待连接;
  3. A本地端口监听到数据,将数据通过ssh隧道转发到C

C执行如下命令:

bash 复制代码
ssh -R 2022:localhost:22 -Nf root@192.168.1.16

对应参数的解释:

  • 2022remote-port,A主机下端口,也就是A下socket需要监听的端口;
  • localhost:22local-host:local-port,C主机下IP和端口。由于命令是从C发起,这里即为localhost:22,这里是A端口转发的目的地
  • root@192.168.1.16user@remote-host,A用户和IP
  • -Nf:使其使用后台模式运行

C机器配置完成后,此时在A机器执行ssh,通过-p指定端口

bash 复制代码
[root@localhost opt]# ssh -p 2022 root@localhost
root@localhost's password:
Last login: Sun Nov 24 18:27:26 2024 from ::1

此时可以顺利登录C机器

A机器继续测试scp,需要使用大写字母P指定端口

bash 复制代码
[root@localhost opt]# touch test.md
[root@localhost opt]# scp -P 2022 test.md root@localhost:/opt
root@localhost's password:
test.md                                                                           100%    0     0.0KB/s   00:00
[root@localhost opt]#

C机器查看,也成功获取文件

如何关掉ssh隧道

C机器执行

bash 复制代码
ps aux | grep ssh

找到我们建立的隧道,kill掉

此时回到A主机,查询端口情况,发现监听也已经关闭

C让A端口转发A就转?

不是的,需要配置

AllowTcpForwarding Specifies whether TCP forwarding is permitted. The default is ''yes''. Note that disabling TCP forwarding does not improve security unless users are also denied shell access, as they can always install their own forwarders.

打开配置文件/etc/ssh/ssh_config,默认是看不到AllowTcpForwarding属性的,且其值为yes(测试环境为centos 7)。如果需显式设置,增加如下配置项

bash 复制代码
AllowTcpForwarding yes

为何叫反向端口转发

通常情况下端口转发,你在本地建了一个ssh连接转发到远程机器,连接是在本地建立的,数据也是通过本地ssh隧道发到远程,普普通通

bash 复制代码
ssh -L <local-port>:<remote-host>:<remote-port> <user>@<remote-server>

反向就不一样了,不再是本地端口转到远程机器,而是从远程机器转到本地端口。对应到开头的例子,连接是在本地C建立的,转发是在远程A执行的,数据是从A转到本地C

一个是本地到远程,一个远程到本地

相关推荐
snowfoootball13 分钟前
(自用)Linux 常用命令自查文档
linux·运维·服务器
Web极客码15 分钟前
在Ubuntu 20.04的服务器上查找的服务器的IP地址
服务器·tcp/ip·ubuntu
墨染 殇雪27 分钟前
webshell及冰蝎双击无法打开?
运维·服务器·webshell·webshell管理工具
搬码临时工30 分钟前
使用自定义固定公网URL地址远程访问公司内网OA办公系统,本地无需公网IP和专线让外网访问
网络·网络协议·tcp/ip
逻辑羊驼1 小时前
VSCode+MobaXterm+X11可视化界面本地显示
运维·服务器·ubuntu·3d
Zacks_xdc2 小时前
【前端】使用Vercel部署前端项目,api转发到后端服务器
运维·服务器·前端·安全·react.js
星马梦缘2 小时前
计算机网络6 第六章 应用层——解决“怎么发请求、怎么回响应”的问题(邮件整体传输流程)
网络·计算机网络·域名·ftp·dns·dhcp
pound1272 小时前
Linux
linux·运维·服务器
@CLoudbays_Martin112 小时前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php
东哥说-MES|从入门到精通4 小时前
Mazak MTF 2025制造未来参观总结
大数据·网络·人工智能·制造·智能制造·数字化