ssh 端口转发笔记

From :https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/

一. 概念 

SSH能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务。这一过程有时也被叫做“隧道”(tunneling)

二. 两功能

  1. 加密 SSH Client 端至 SSH Server 端之间的通讯数据。
  2. 突破防火墙的限制完成一些之前无法建立的 TCP 连接。

三. 分类

1. 本地端口转发

命令格式 :  ssh -L <local port>:<remote host>:<remote port> <SSH hostname>

case 1: 双机 (只有两台机器LDAP Client 和 LDAP Server)

LDAP server没有被墙

只有LDAP 服务器上部署的应用才能直接连接此 LDAP 服务器

LdapClientHost 执行:

$ssh -L 7001:localhost:389 LdapServerHost (将远程机器(LdapClientHost)上的应用直接配置到本机的 7001 端口上)

注意:

1. 在建立 SSH 连接的同时创建端口转发

2.  <remote host>  指向 LdapServertHost, 用 localhost 而不是 IP 地址或者主机名, 因限制LDAP只有本机才能访问(只允许 lookback 接口访问)。

3. LdapClientHost建立的7001端口转发不可以被其他机器使用。

本地端口转发绑定的是lookback接口,只能本机应用次啊能访问。其他机器连接会有"connection refused"错误。

GatewayPorts 关键字可以解决这个问题

ssh -g -L <local port>:<remote host>:<remote port> <SSH hostname>

case 2: 四机

LDAP Server 被墙

SSH Client(C) 执行:

$ ssh -g -L 7001:<B>:389 <D>

注意:

1. -g

2. (A)<-> (C) 以及 (B)<->(D) 之间的连接并不是安全连接,它们之间没有经过 SSH 的加密及解密

2. 远程端口转发

命令格式 :  ssh -R <local port>:<remote host>:<remote port> <SSH hostname>

case:LdapClientHost SSH不到 LdapServertHost

            LdapServertHost SSH 到  LdapClientHost OK

LdapServertHost 执行  

$ ssh -R 7001:localhost:389 LdapClientHost

本地端口转发和远程端口转发区别

本地:

LdapClientHost app client-> LdapClientHost SSH Client -> LdapServertHost SSH Server ->  LdapServertHost app server

远程:

LdapClientHost app client-> LdapServertHost SSH Server -> LdapClientHost SSH Client ->  LdapServertHost app server

3. 动态端口转发

服务器端口不固定, 如浏览器进行 Web 浏览,可能是80,可能是443,可能是...

命令格式 :  $ ssh -D <local port> <SSH Server>

sample 1: SSH Client执行:

ssh -D 7001 <SSH Server> 

选择了 7001 作为本地的端口号,SH 是创建了一个 SOCKS 代理服务。

Sample 2:

A机器执行

ssh-D 1080 root@sshserver

A机器firefox设置代理socket proxy:127.0.0.1:1080

A机器测试

root@ubuntu:~# curl --socks5 127.0.0.1:1080  www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-PH"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png
root@ubuntu:~# netstat -anp | grep 1080
tcp        0      0 127.0.0.1:1080         0.0.0.0:*               LISTEN      15287/ssh       
tcp        0      0 127.0.0.1:51592         127.0.0.1:1080         TIME_WAIT   -               
tcp6       0      0 ::1:1080               :::*                    LISTEN      15287/ssh    

A windows, 也可如下设置浏览器代理  

 

 

 

PS: 四机例子: (autossh自动检测ssh连接状态,如果关闭,自动重建连接)

1 S root      7550     1  0  80   0 -  1616 sys_pa 04:55 ?        00:00:00 autossh -M 5555 -L 5001:pln-cd1-apigw-vip.ft1core.mcloud.entsvcs.net:5001 -N  ft1_Jumper

4 S root      8456  7550  2  80   0 - 19743 poll_s 05:00 ?        00:00:00 /usr/bin/ssh -L 5555:127.0.0.1:5555 -R 5555:127.0.0.1:5556 -L 5001:pln-cd1-apigw-vip.ft1core.mcloud.entsvcs.net:5001 -N ft1_Jumper

0 S root      8467  1620  0  80   0 - 28166 pipe_w 05:00 pts/0    00:00:00 grep --color=auto ssh

1 S root     13130     1  0  80   0 -  1616 sys_pa May23 ?        00:00:00 autossh -M 5678 -ND  1080 N1_Jumper

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值