本文是SSH端口转发功能的学习笔记,以备后续复习

ssh所有参数如下:

$ ssh -h

usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]

[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]

[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]

[-i identity_file] [-J [user@]host[:port]] [-L address]

[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]

[-Q query_option] [-R address] [-S ctl_path] [-W host:port]

[-w local_tun[:remote_tun]] destination [command]

  • ssh端口转发相关的的常用参数:

-f    后台执行ssh指令

-C    允许压缩数据

-N    不执行远程指令

-R    将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口

详细说明:

-R [bind_address:]port:host:hostport

-R [bind_address:]port:local_socket

-R remote_socket:host:hostport

-R remote_socket:local_socket

-R [bind_address:]port

Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded

to 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. When‐

ever a connection is made to this port or Unix socket, the connection is forwarded over the secure channel, and

a connection is made from the local machine to either an explicit destination specified by host port hostport,

or local_socket, or, if no explicit destination was specified, ssh will act as a SOCKS 4/5 proxy and forward

connections to the destinations requested by the remote SOCKS client.

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 ‘*’, indicates 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 ‘0’, the 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 out‐

put.

-L    本地端口转发

详细说明

-L [bind_address:]port:host:hostport

-L [bind_address:]port:remote_socket

-L local_socket:host:hostport

-L local_socket:remote_socket

Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded

to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to

either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket.

Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel,

and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote

machine.

Port forwardings can also be specified in the configuration file. Only the superuser can forward privileged

ports. IPv6 addresses can be specified by enclosing the address in square brackets.

By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit

bind_address may be used to bind the connection to a specific address. The bind_address of “localhost” indi‐

cates that the listening port be bound for local use only, while an empty address or ‘*’ indicates that the

port should be available from all interfaces.

-D    动态端口转发

详细说明:

-D [bind_address:]port

Specifies a local “dynamic” application-level port forwarding. This works by allocating a socket to listen to

port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this

port, the connection is forwarded over the secure channel, and the application protocol is then used to deter‐

mine where to connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and

ssh will act as a SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be

specified in the configuration file.

IPv6 addresses can be specified by enclosing the address in square brackets. Only the superuser can forward

privileged ports. By default, the local port is bound in accordance with the GatewayPorts setting. However,

an explicit bind_address may be used to bind the connection to a specific address. The bind_address of

“localhost” indicates that the listening port be bound for local use only, while an empty address or ‘*’ indi‐

cates that the port should be available from all interfaces.

  • 本地转发         

远程的端口转发到本地的端口,

对于使用者"Local Clients"来说,入口在本机IP

ssh -N  -L [My Computer Port]:[Remote Server IP]:[Remote Port] [SSH Server User Name]@[SSH Server IP]

本地转发是将 [Remote Server IP]:[Remote Port] 映射到 127.0.0.1:[My Computer Port]

(配图来自MobaXterm截屏)

典型的使用场景:

VNC server 因为安全性原因,配置为只接受 127.0.0.1的接入, 而服务器对外只提供ssh登录,就可以通过ssh隧道来承载VNC的访问。

先SSH连上vnc server主机,将vnc server端口映射到本地,再连接vnc。

功能类似路由器的端口映射,区别:

路由器的端口映射后,vnc viewer的ip是客户端的IP,

SSH本地转发映射端口后,vnc viewer的ip是vnc server的127.0.0.1

举例:

ssh -NL 13306:aliyuncs.com:3306 root@39.23.80.6

# 访问 127.0.0.1:13306 -> aliyuncs.com:3306

  • 远端转发

本地的端口转发到远程的端口,远程的服务器相当于提供DNAT、反向代理服务器的功能

对于使用者"Remote Clients"来说,入口在"SSH Server IP"

ssh -N  -R [Local Server]:[Local Port]:[SSH Server IP]:[SSH Server Port]   [SSH User Name]@[SSH Server IP]

注意:

1. sshd_config里要打开AllowTcpForwarding选项,否则-R远程端口转发会失败。

默认转发到远程主机上的端口绑定的是127.0.0.1,

2. 如要绑定0.0.0.0需要配置sshd_config里的GatewayPorts选项为yes。

文档:

1、是否允许远程主机连接本地的转发端口。默认值是"no"。

2、sshd(8) 默认将远程端口转发绑定到loopback地址。这样将阻止其它远程主机连接到转发端口。

3、GatewayPorts 指令可以让 sshd 将远程端口转发绑定到非loopback地址,这样就可以允许远程主机连接了。

4、"no"表示仅允许本地连接,"yes"表示强制将远程端口转发绑定到统配地址(wildcard address),

"clientspecified"表示允许客户端选择将远程端口转发绑定到哪个地址。

这个选项如果由于权限没法打开也有办法,可配合ssh -L多转发一次

典型的使用场景:

内网的服务器通过跳板机为互联网的用户提供接入服务。

类似frp等内网穿透功能

举例:

ssh -NR 8080:47.104.164.235:80 root@8.136.136.247

# 访问8.136.136.247:8080 -> 47.104.164.235:80

ssh -NR 8000:8.136.136.247:80 root@47.104.164.235

# 访问47.104.164.235:8000 -> 8.136.136.247:80

  • 动态转发

相当于SNAT。对使用者“Local Clients”来说,可以理解为代理服务器上网

ssh -N  -D [LocaHost Port]   [SSH Server User Name]@[SSh Server IP]

举例:

ssh -ND 2000 root@47.104.164.235

# 在浏览器或系统设置中将设置socks代理: 127.0.0.1:2000

然后本地所有的上网出口全部通过47.104.164.235进行转发。

动态端口转发则只是绑定了一个本地端口,而目标地址:目标端口则是不固定的

目标地址:目标端口是由本地发起的请求决定的

比如访问baidu.com 则相当于把这个请求首先发送到了47.104.164.235,然后由47.104.164.235进行访问后将数据转发回至本地

即: 访问百度流程变成 本地 -> 47.104.164.235 -> 百度

学习笔记:SSH端口转发的三种方式相关推荐

  1. [笔记]SSH 端口转发

    ssh 拥有很强大的能力,因为在服务器运维中一直在使用 ssh ,因此探讨 ssh 的功能能够在日常开发过程中受益颇多,这里描述下 ssh 的三种端口转发功能.灵活使用可以大大简化我们的开发过程. 准 ...

  2. Spring 学习之 二----Spring创建对象的三种方式

    最近在系统的学习Spring,现在就Spring的一些知识进行总结. 我们知道Spring是一个开放源代码的设计层面的框架,他主要解决的是业务逻辑层与其他各层之间松耦合的问题. Spring 有三个核 ...

  3. Java动物类enjoy方法打印_Java反射学习-2 - 获取Class对象的三种方式

    1 packagecn.tx.reflect;2 3 importjava.lang.reflect.Constructor;4 importjava.lang.reflect.Field;5 imp ...

  4. mongo学习笔记四:Mongodb的三种集群(Replica Set)

    Replica Set    中文翻译叫做副本集,不过我并不喜欢把英文翻译成中文,总是感觉怪怪的.其实简单来说就是集群当中包含了多份数据,保证主节点挂掉了,备节点能继续提供数据服务,提供的前提就是数据 ...

  5. iptables学习笔记:端口转发命令优化

    大约一年前,在一个x86板子系统上实现端口转发.现在又出现问题,又要抽空整理整理.虽说是另一同事在另一项目中遇到的,但中秋节前我出差之前老大叫我帮忙协助该同事排查,出差时该同事又call我,出差后老大 ...

  6. 计算机视觉与图像处理学习笔记之人脸识别的三种方法

    人脸检测是指在图像中完成人脸定位的过程,而人脸识别是在人脸检测的基础上进一步判断人的身份,OpenCV提供了三种人脸识别的方法:EigenFaces(特征脸).FisherFaces(人鱼脸).Loc ...

  7. 【MAYA动画基础学习 1】小球动画的三种方式——关键帧,刚体和布料

    基础设置 调节关键帧快捷键(避免多出很多不必要的部分) 关键帧->Set Key->根据需要进行调节(下图设置为仅设置单一关键帧) 调节播放设置(避免渲染出来的效果跟maya里播放的效果不 ...

  8. 用树莓派学Linux(三)--------SSH端口转发,跳出局域网的树莓派控制

    上一篇提到的通过SSH服务连接树莓派,是在局域网内实现的,并不能远程控制,比如树莓派在实验室,你用手机在4G网的环境下也需要操作树莓派,则需要考虑使用SSH服务的端口转发功能,实现真正的远程控制.本篇 ...

  9. ssh隧道原理及三种隧道转发模式

    ssh 是如何工作的? 在 terminal 中 ,当我们输入 ssh username@remote_host 时,terminal 程序调用 ssh client ,ssh client 发起网络 ...

  10. 【opencv学习笔记】第五篇:访问图像中像素的三种方式、ROI区域图像叠加和图像混合

    1. 访问图像中像素的三种方式 任何图像处理算法,都是从操作每个像素开始的.在OpenCV中,提供了三种访问每个像素的方法. 方法1:指针访问:C操作符[] 方法2:迭代器iterator 方法3:动 ...

最新文章

  1. python操作redis--------------数据库增删改查
  2. 基于Kubernetes 构建.NET Core 的技术体系
  3. mysql 字符集测试_MySQL多字符集备份恢复测试
  4. spanning-tree extend system-id
  5. python 为什么只执行一次_python只执行一次
  6. WPF——Expander控件(转)
  7. 10193 条票房数据告诉你《流浪地球》领跑的电影档战果如何?
  8. LeetCode 116. Populating Next Right Pointers in Each Node
  9. MediaCodec 编码H264 编码一帧后dequeueOutputBuffer为-1的问题
  10. 调用远程摄像头进行人脸识别_怎样快速搭建人脸识别通道?
  11. iphone计算机同样答案,学会这4招,iPhone搭配Windows电脑一样好用
  12. 块截断编码图像压缩技术
  13. html5做宠物饲养,说一说最适合上班族养的十大宠物
  14. excel计算结果总是为0
  15. upupoo服务器维护中是什么情况,upupoo用不了怎么办-解决upupoo打不开的方法 - 河东软件园...
  16. php7新特性ppt,2019新版PPT,不知道这7个新功能,怎么做好幻灯片?
  17. discuz7.2sql注入漏洞
  18. 数学与计算机科学研究生大学排名,计算数学专业考研院校排名
  19. 【Java并发】double-checked-locking设计模式
  20. 基于SpringBoot+vue的文件管理系统

热门文章

  1. Ubuntu18.04安装gcc——史上最全安装教程
  2. pycharm中配置Git教程
  3. 《R语言初学指南》一导读
  4. Android平台上使用SDL官方demo播放视频(使用ffmpeg最新版解码)
  5. 功能强大的pdf控件,用户无需安装任何软件即可使用
  6. MATLAB通信系统建模与仿真
  7. 【Matlab优化预测】贝叶斯网络优化LSTM预测【含源码 1158期】
  8. android模拟器模拟nfc功能吗,打开NFC模拟器不适用于Android模拟器
  9. 设备管理系统未来发展的四大趋势
  10. python遗传算法解简单整数规划与原理探究