站点工具


服务器技术:linux:ssh的端口转发

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

后一修订版
前一修订版
服务器技术:linux:ssh的端口转发 [2018/06/13 13:57]
admin 创建
服务器技术:linux:ssh的端口转发 [2018/09/10 18:02] (当前版本)
行 1: 行 1:
-<p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​实战 SSH 端口转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)">​https://​www.ibm.com/​deve<​a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)">/​l-c</​a>​loperworks/​cn/​linux/​l-cn-sshforward/​</a></​p><​h2 style=";​padding:​0px;​line-height:37px"><​a></a><​a ​target="​_blank"​ style="​color:rgb(106,​57,​6)"></a><a target="​_blank"​ href="​http://​blog.sina.com.cn/​s/​blog_704836f40100lwxh.html"​ style="​color:​rgb(21,​80,​103)">​SSH的端口转发:​本地转发Local &​nbsp;​Forward和远程转发Remote Forward</​a></​h2><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a><​span style="​font-size:​13px;​line-height:​23px">​关于使用ssh portforwarding来进行翻墙的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下。</​span></​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先要记住一件事情就是:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​SSH 端口转发自然需要 SSH 连接,而 SSH 连接是有方向的,从 SSH Client 到 SSH Server 。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而我们所要访问的应用也是有方向的,应用连接的方向也是从应用的 Client 端连接到应用的 Server 端。比如需要我们要访问Internet上的Web站点时,Http应用的方向就是从我们自己这台主机(Client)到远处的Web Server。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果SSH连接和应用的连接这两个连接的方向一致,那我们就说它是本地转发。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;&​lt;​local&​nbsp;​port&​gt;:&​lt;​remote&​nbsp;​host&​gt;:&​lt;​remote&​nbsp;​port&​gt;&​nbsp;&​lt;​SSH&​nbsp;​hostname&​gt;</​pre><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869397988942.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果SSH连接和应用的连接这两个连接的方向不同,那我们就说它是远程转发。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-R&​nbsp;&​lt;​local&​nbsp;​port&​gt;:&​lt;​remote&​nbsp;​host&​gt;:&​lt;​remote&​nbsp;​port&​gt;&​nbsp;&​lt;​SSH&​nbsp;​hostname&​gt;</​pre><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869398300272.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​本地转发在本地这台机器上监听一个端口,然后所有访问这个端口的数据都会通过ssh 隧道传输到远端的对应端口上,下面是例子。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​本地端口转发绑定的是 lookback 接口,这意味着只有 localhost 或者 127.0.0.1 才能使用本机的端口转发 , &​nbsp;​其他机器发起的连接只会得到“ connection refused. &​nbsp;​”。要想让其他机器也能共享这个隧道来转发的话需要在配置文件中使用GatewayPorts 关键字,或者直接命令行下使用&​quot;​-g&​quot;​参数。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​另外本地转发命令中的 &​lt;​remote host&​gt;​ 和 &lt;SSH hostname&​gt;​ 可以是不同的主机。</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869398887271.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​远程转发与本地转发正好相反,打开ssh隧道以后,在远端服务器监听一个端口,所有访问远端服务器指定端口都会通过隧道传输到本地的对应端口上,下面是例子。</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869399841356.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里是一篇很好的介绍SSH PortForwarding的文章<​a target="​_blank"​ href="​http://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(30,​91,​119)">​http://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​可以将远端服务器一个端口remote_port绑定到本地端口port,其中-C是进行数据压缩,-f是后台操作,只有当提示用户名密码的时候才转向前台。-N是不执行远端命令,在只是端口转发时这条命令很有用处。-g &​nbsp;​是允许远端主机连接本地转发端口。-R表明是将远端主机端口映射到本地端口。如果是-L,则是将本地端口映射到远端主机端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的三个强大的端口转发命令:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​转发到远端:ssh -C -f -N -g -L 本地端口:​目标IP:​目标端口 用户名@目标IP</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​转发到本地:ssh -C -f -N -g –R 本地端口:​目标IP:​目标端口 用户名@目标IP</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g -D listen_port user@Tunnel_Host</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-C:压缩数据传输。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-f :后台认证用户/​密码,通常和-N连用,不用登录到远程主机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N :不执行脚本或命令,通常与-f连用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-g :在-L/​-R/​-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-L 本地端口:​目标IP:​目标端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 同时远程主机和 host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R本地端口:​目标IP:​目标端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的,​ 远程主机上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和 host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. IPv6 地址用另一种格式说明:​ &​nbsp;​port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-p :被登录的ssd服务器的sshd服务端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-D port</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​指定一个本地机器 “动态的&#​39;​’ 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, &​nbsp;​将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​应用举例</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.将发往本机的80端口访问转发到174.139.9.66的8080端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g<span style="​font-size:​18px;​line-height:​32px"><​strong>&​nbsp;​-L</​strong></​span>&​nbsp;​80:<​strong><​span style="​font-size:​18px;​color:​rgb(255,​0,​0);​line-height:​32px">​174.139.9.66:​8080&​nbsp;&​nbsp;&​nbsp;​ &​nbsp;</​span></​strong><​a target="​_blank"​ href="​mailto:​master@174.139.9.66"​ style="​color:​rgb(21,​80,​103)">​master@174.139.9.66</​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2.将发往174.139.9.66的8080访问转发到本机的80端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g<​strong><​span style="​font-size:​18px;​line-height:​32px">&​nbsp;​-R</​span></​strong>&​nbsp;​80:​174.139.9.66:​8080&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;<​a target="​_blank"​ href="​mailto:​master@174.139.9.66"​ style="​color:​rgb(21,​80,​103)">​master@174.139.9.66</​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N - 不使用Shell窗口,纯做转发的时候用,如果你在映射完成后继续在服务器上输入命令,去掉这个参数即可</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​例子A:​我们想远程管理服务器上的MySQL,​那么使用下面命令</​strong></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;​3306:​127.0.0.1:​3306&​nbsp;&​nbsp;&​nbsp;​user@emlog-vps&​nbsp;&​nbsp;&​nbsp;&​nbsp;​-N</​pre><​p></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">&​nbsp;<​br/></​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​运行这个命令之后,ssh将会自动将服务器的3306映射到本机的3306端口,我们就可以使用任意MySQL客户端连接 localhost:​3306即可访问到服务器上的MySQL了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​例子B:​一次同时映射多个端口</​strong></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;​8888:​www.host.com:​80&​nbsp;​-L&​nbsp;​110:​mail.host.com:​110&​nbsp;​-L&​nbsp;&​nbsp;&​nbsp;&​nbsp;​25:​mail.host.com:​25&​nbsp;​user@host&​nbsp;​-N</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这个命令将自动把服务器的80,110,25端口映射到本机的8888,110和25端口 以上命令在ubuntu 9.10 上测试通过...</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h1 style=";​padding:​0px;​line-height:​50px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​端口映射让外部用户同时访问内部多个应用</​h1><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ 如下图所示,这是企业部署NAT服务器的典型案例。如果现在企业内部有两台服务器,分别为WEB服务器(192.168.0.3)与ERP服务器(192.168.0.2)。现在企业想让出差在外的员工,也可以通过<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​访问企业内部的这两个应用,该如何设计呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="​http://​img.ph.126.net/​mg8vaCplqo1apzBLkoAlCw==/​1524186998888700710.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;&​nbsp;&​nbsp;<​strong>&​nbsp;​ 一、基于端口访问的一般原理。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  在讲解NAT服务器的具体端口映射配置之前,笔者觉得读者有必要先了解一下基于端口访问的一般原理。这有利于读者了解下面的具体配置。下面笔者以员工访问WEB服务器为例,谈谈基于端口访问的基本步骤。假设现在某个用户需要访问WEB服务器,则客户端的浏览器(假设采用的端口为3000)与WEB服务器(假设采用的端口为80)是如何来进行通信的呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第一步:发起连接请求。当用户需要查看公司网页时,用户所用的主机会像WEB服务器发送请求。在这个发起请求的过程中,会有一些参数传递。如在发送请求的过程中,用户所用的主机需要知道WEB服务器的<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​、所采用的协议(HTTP)等。另外就是两个端口信息。一是用户浏览器所用的端口,即为3000。二是WEB服务器网站所用的端口(如果采用的是HTTP服务,则默认为80端口)。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第二步:WEB服务器转发请求。当服务器接受到这个请求后,会分析数据包。他经过分析包发现,这个请求是要求端口为80的应用软件来负责的。服务器确定了负责人之后,就会把这个请求转发给具体的负责人,即WEB应用服务器来处理。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第三步:服务器会把从WEB应用服务那边反馈回来的网页传送给用户所用的计算机。在传送的时候,其也会制定要把这个网页给你的那个3000端口(浏览器)对应的应用软件。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第四步:用户的计算机收到数据包之后,也会对这个数据包进行分析,来判断需要把这个数据包转发给哪个端口(应用服务)。分析后发现是转交给3000端口的,就会把这个数据包转发给浏览器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​二、基于端口映射的NAT服务器配置。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  其实基于端口映射的NAT服务器其工作原理就跟上面这个基于端口访问的工作过程类似。只是这里的NAT服务器就相当于用户主机,而用户相当于内部的服务器。由于用户需要通过外网访问企业内部应用的话,通常情况下要求内网服务器也要求有合法的外网IP。只有私网<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​的服务器是无法直接跟外部客户进行通信的。为此,如果没有端口映射的话,用户如果要同时访问内部的WEB服务器与ERP服务器,则就需要有两个公网IP地址。但是,大家都知道现在公网IP地址严重缺乏。在没有足够多的IP地址的情况下,该如何实现这种需求呢?​那就知道通过端口映射来完成。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  如上图所示,用户需要访问企业内部的WEB服务与ERP服务器,他只需要知道NAT服务地址与WEB服务器与ERP服务器所采用的端口即可。如果</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​WEB服务器采用的端口为80,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ERP服务器所采用的端口为5050。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而NAT服务器的IP地址假设为202.96.92.100的话,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​那么当用户访问WEB服务器时,只需要输入http://​ &​nbsp;​202.96.92.100即可。由于HTTP协议默认采用的是80端口,故这里不用配置端口号。如果WEB服务器中把这个端口改为了3000,则在访问的时候就需要使用http://​ &nbsp; 202.96.92.100:​3000(地址+端口号)的形式了。用户要访问内部的ERP服务器也是类似的,只要把NAT服务器的地址以及ERP服务器所采用的端口在客户端上进行配置即可。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  可见通过NAT服务器的端口映射功能,可以让外部用户同时访问企业内部的WEB服务器与ERP服务器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​18px;​color:​rgb(255,​0,​0);​line-height:​32px">​那要实现这个需求具体该如何配置呢?</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  NAT服务器让内部的局域网用户可以连接到<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​,以便用户能够收发互联网邮件、浏览互联网上的网站等等。但是默认情况下外部用户是不能够访问内部的服务器。如果需要实现以上功能的话,就需要进行专门的配置。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  如上图中,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​内部局域网计算机(192.168.0.2)为WEB服务器,其采用的端口为80。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果现在外部用户需要访问这个WEB服务器站点,则用户只知道这个WEB服务器的地址为202.96.92.100(NAT服务器的地址)。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而WEB服务器默认采用的端口为80。当外部用户通过浏览器访问企业内部的WEB服务器时,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​他会在浏览器中输入http://​ 202.96.92.100路经来连接网站。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​NAT服务器会将此请求发送给内部的计算机WEB服务器(<​strong>​192.168.0.2),</​strong>​并由WEB服务器将这个请求转发给端口为80的软件来负责。WEB服务器会将网页传送给NAT服务器,再由NAT服务器负责将网页传送给外部用户的计算机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果用户要访问内部的ERP服务器,其过程也是如此。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​那么现在的问题是,NAT服务器怎么判断要把用户HTTP请求发送给服务器(192.168.0.2)而不是发送给(192.168.0.3)呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  这主要是根据端口来判断的。也就是说在NAT服务器中有一个端口映射的规则,只要用户访问的目的端口是80,就把这个请求转发给WEB服务器;​若请求的端口是5050,则就把这个请求发送给ERP服务器。这就是端口映射的实质。了解这个内容之后,那么了解下面的配置也就简单多了。打开路由和远程访问主控制窗口,选择要配置的服务器。然后选择IP路由选择、NAT/​基本防火墙。并双击右方对外连接的网络接口(注意这里不要选择连接内网的网络接口),选择服务和端口标签。然后直接从服务列表中选取要对外开发的服务,如选择WEB服务器。在公用地址中选择在此接口,表示由互联网服务提供商指派的NAT服务器的公网IP地址。然后再设置,如果把这个服务请求转发给内部的IP地址为192.168.0.2的WEB服务器。通过这个简单的配置,就实现基于端口的映射。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​三、改善NAT服务器的性能与便利性。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  可见这个端口映射配置是很简单的。但是其难点在于如何提高这个NAT服务器的性能,如何让用户使用的更加方便。这是网络管理员所需要考虑的。对此笔者有如下几个建议可供大家参考。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  1、利用域名来代替<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​。当用户访问企业内部的网站服务器时,如果让他们输入服务器的IP地址,对普通用户来说可能有点难度。要记住这202.96.92.100十一个没有丝毫关系的数字有很大的难度。如果能够把这个IP地址转换为有实际意义的网址那就最好了。在企业内部,可以自己部属一个DNS服务器,把IP地址转换为网址。那么内部员工可以直接通过网址来访问WEB服务器。但是在<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​上,企业可不能自己随便定义域名。为了提高用户访问的便捷性,企业网络管理员最好能够像当地的域名注册机构,申请一个合适的域名并关联到自己的NAT服务器公网IP地址。那么以后外部用户访问内部WEB服务器时,只需要输入像新浪那样的地址就可以了。而不用输入这些难以记忆的地址。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  2、如果企业可以申请到多个IP地址的话,那么最好能够分开来部署。即企业可以申请两个<​a target="​_blank"​ href="​http://​www.vlan9.com/​adsl/​index.html"​ style="​color:​rgb(21,​80,​103)">​ADSL</​a>​帐号。其中一个专门用来连接内部的服务器;​而另外一个用来内部员工跟互联网的通信。这么做主要是为了减轻NAT服务器的压力,提高其性能。如企业只有一个ADSL帐号的话,那么无论是外部用户访问企业内部服务器;​还是企业内部员工访问互联网,其数据流都要通过这台NAT服务器来处理。为此,如果企业数据流量比较大或者NAT服务器配置不怎么理想的话,那么很可能这个NAT服务器就成为了企业网络访问的瓶颈资源。如果把他们分开,则可以减轻NAT服务器的压力,提高外部用户访问企业内部服务器的效率。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  3、配置合适的ICMP数据包策略,以加强阻挡黑客利用ICMP的攻击行为。使用ICMP攻击的原理实际上就是通过Ping大量的数据包使得NAT服务器的CPU使用率居高不下而崩溃。一般情况下黑客通常在一个时段内连续向计算机发出大量请求而导致CPU占用率太高而死机。而且会使用多台肉鸡同时向NAT服务器发送数据包。基于ICMP的攻击可以分为两大类,一是ICMP攻击导致拒绝服务;​另外一个是基于重定向的路由欺骗技术。无论是哪一类攻击,都会给NAT服务器产生致命的影响。为此为了NAT服务器的安全,最好配置合适的ICMP包策略。如可以拒绝响应别人的ICMP请求。如此攻击者就无法对NAT服务器发起ICMP攻击了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​SSH隧道与端口转发及内网穿透</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多。不过其实除了这个功能,它的隧道转发功能更是吸引人。下面是个人根据自己的需求以及在网上查找的资料配合自己的实际操作所得到的一些心得。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​SSH/​plink命令的基本资料:</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先,认识下这三个非常强大的命令:</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g -L listen_port:​DST_Host:​DST_port user@Tunnel_Host&​nbsp;<​br/>​ ssh -C -f -N -g -R listen_port:​DST_Host:​DST_port user@Tunnel_Host&​nbsp;<​br/>​ ssh -C -f -N -g -D listen_port user@Tunnel_Host</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​相关参数的解释:&​nbsp;<​br/>​ -f Fork into background after authentication.&​nbsp;<​br/>​ 后台认证用户/​密码,通常和-N连用,不用登录到远程主机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-L port:​host:​hostport&​nbsp;<​br/>​ <​strong><​span style="​font-size:​18px;​color:​rgb(0,​0,​255);​line-height:​32px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口</​span></​strong>​. &​nbsp;​工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ &​nbsp;​同时远程主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. &​nbsp;​IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R port:​host:​hostport&​nbsp;<​br/>​ <​strong><​span style="​font-size:​18px;​color:​rgb(0,​0,​255);​line-height:​32px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口.&​nbsp;</​span></​strong>​工作原理是这样的,​ &​nbsp;​远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和 host &​nbsp;​的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. &​nbsp;​IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-D port&​nbsp;<​br/>​ 指定一个本地机器 “动态的’’ 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, &​nbsp;​将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-C Enable compression.&​nbsp;<​br/>​ 压缩数据传输。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N Do not execute a shell or command.&​nbsp;<​br/>​ 不执行脚本或命令,通常与-f连用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-g Allow remote hosts to connect to forwarded ports.&​nbsp;<​br/>​ 在-L/​-R/​-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。注:这个参数我在实践中似乎始终不起作用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​以上摘录自:</​em><​a target="​_blank"​ title="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ href="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ style="​color:​rgb(21,​80,​103)"><​em><​span style="​color:​rgb(244,​122,​14)">​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html</​span></​em></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​建立本地SSH隧道例子</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在我们计划建立一个本地SSH隧道之前,我们必须清楚下面这些数据:</​p><​ol style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​中间服务器d的IP地址</​p></​li><​li><​p>​要访问服务器c的IP地址</​p></​li><​li><​p>​要访问服务器c的端口</​p></​li></​ol><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,我们把上面这张图变得具体一些,给这些机器加上IP地址。并且根据下面这张图列出我们的计划:</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869400146519.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​ol style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​需要访问234.234.234.234的FTP服务,也就是端口21</​p></​li><​li><​p>​中间服务器是123.123.123.123</​p></​li></​ol><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​18px;​line-height:​32px">​现在我们使用下面这条命令来达成我们的目的(<​span style="​color:​rgb(0,​0,​255)">​192.168.0.100上执行</​span>​)</​span></​strong></​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​18px;​line-height:​32px">​1.ssh -N -f&​nbsp;&​nbsp;​ -L&​nbsp;&​nbsp;​ &​nbsp;<​strong><​span style="​color:​rgb(0,​0,​255)">​2121</​span></​strong>:<​strong><​span style="​color:​rgb(255,​0,​0)">​234.234.234.234:​21</​span></​strong>&​nbsp;&​nbsp;&​nbsp;​ 123.123.123.123</​span></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​18px;​line-height:​32px">​2.ftp://​localhost:​2121&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>#​ 现在访问本地2121端口,就能连接234.234.234.234的21端口了</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里我们用到了SSH客户端的三个参数,下面我们一一做出解释:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​-N 告诉SSH客户端,这个连接不需要执行任何命令。仅仅做端口转发</​p></​li><​li><​p>​-f 告诉SSH客户端在后台运行</​p></​li><​li><​p>​-L 做本地映射端口,被冒号分割的三个部分含义分别是</​p></​li><​ul style="​list-style-type:​ square;"​ class="​ list-paddingleft-2"><​li><​p><​span style="​color:​rgb(0,​0,​255)">​需要使用的本地端口号</​span></​p></​li><​li><​p><​span style="​color:​rgb(255,​0,​0)">​需要访问的目标机器IP地址</​span>​(IP:​ 234.234.234.234)</​p></​li><​li><​p><​span style="​color:​rgb(255,​0,​0)">​需要访问的目标机器端口</​span>​(端口:​ 21)</​p></​li></​ul><​li><​p>​最后一个参数是我们用来建立隧道的中间机器的IP地址(IP:​ 123.123.123.123)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​我们再重复一下-L参数的行为。-L X:​Y:​Z的含义是,将IP为Y的机器的Z端口通过中间服务器映射到本地机器的X端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在这条命令成功执行之后,我们已经具有绕过公司防火墙的能力,并且成功访问到了我们喜欢的一个FTP服务器了。</​p><​h5 style="​line-height:​21px"​><a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​如何建立远程SSH隧道</​h5><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过建立本地SSH隧道,我们成功地绕过防火墙开始下载FTP上的资源了。那么当我们在家里的时候想要察看下载进度怎么办呢?大多数公司的网络是通过路由器接入互联网的,公司内部的机器不会直接与互联网连接,也就是不能通过互联网直接访问。通过线路D-B-A访问公司里的机器a便是不可能的。也许你已经注意到了,虽然D-B-A这个方向的连接不通,但是A-B-D这个方向的连接是没有问题的。那么,我们能否利用一条已经连接好的<​strong><​span style="​font-size:​24px;​color:​rgb(255,​0,​0);​line-height:​43px">​A-B-D</​span></​strong>​方向的连接来完成D-B-A方向的访问呢?答案是肯定的,这就是远程SSH隧道的用途。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​与本地SSH一样,我们在建立远程SSH隧道之前要清楚下面几个参数:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​需要访问内部机器的远程机器的IP地址(这里是123.123.123.123)</​p></​li><​li><​p>​需要让远程机器能访问的内部机器的IP地址(这里因为是想把本机映射出去,因此IP是127.0.0.1)</​p></​li><​li><​p>​需要让远程机器能访问的内部机器的端口号(端口:​22)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在清楚了上面的参数后,我们使用下面的命令来建立一个远程SSH隧道</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -N -f&​nbsp;<​strong><​span style="​font-size:​24px;​color:​rgb(255,​0,​0);​line-height:​43px">​-R</​span></​strong>&​nbsp;​2222:<​strong>​127.0.0.1:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​strong>​123.123.123.123</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,在IP是123.123.123.123的机器上我们用下面的命令就可以登陆公司的IP是192.168.0.100的机器了。</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -p 2222 localhost</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N,-f 这两个参数我们已经在本地SSH隧道中介绍过了。我们现在重点说说参数-R。该参数的三个部分的含义分别是:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​远程机器使用的端口(2222)</​p></​li><​li><​p>​需要映射的内部机器的IP地址(127.0.0.1)</​p></​li><​li><​p>​需要映射的内部机器的端口(22)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​例如:-R X:Y:Z 就是把我们内部的Y机器的Z端口映射到远程机器的X端口上。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​建立SSH隧道的几个技巧</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​自动重连</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​隧道可能因为某些原因断开,例如:机器重启,长时间没有数据通信而被路由器切断等等。因此我们可以用程序控制隧道的重新连接,例如一个简单的循环或者使用&​nbsp;<​a target="​_blank"​ href="​http://​cr.yp.to/​daemontools.html"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​djb’s daemontools</​span></​a>&​nbsp;​. &​nbsp;​不管用哪种方法,重连时都应避免因输入密码而卡死程序。关于如何安全的避免输入密码的方法,请参考我的&​nbsp;<​a target="​_blank"​ href="​http://​blog.jianingy.com/​node/​73"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​如何实现安全的免密码ssh登录</​span></​a>&​nbsp;​。这里请注意,如果通过其他程序控制隧道连接,应当避免将SSH客户端放到后台执行,也就是去掉-f参数。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​保持长时间连接</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​有些路由器会把长时间没有通信的连接断开。SSH客户端的TCPKeepAlive选项可以避免这个问题的发生,默认情况下它是被开启的。如果它被关闭了,可以在ssh的命令上加上-o TCPKeepAlive=yes来开启。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​另一种方法是,去掉-N参数,加入一个定期能产生输出的命令。例如:​ top或者vmstat。下面给出一个这种方法的例子:</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -R 2222:​localhost:​22 123.123.123.123 &​quot;​vmstat 30&​quot;</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​检查隧道状态</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​有些时候隧道会因为一些原因通信不畅而卡死,例如:由于传输数据量太大,被路由器带入stalled状态。这种时候,往往SSH客户端并不退出,而是卡死在那里。一种应对方法是,使用SSH客户端的ServerAliveInterval和ServerAliveCountMax选项。 &​nbsp;​ServerAliveInterval会在隧道无通信后的一段设置好的时间后发送一个请求给服务器要求服务器响应。如果服务器在 &​nbsp;​ServerAliveCountMax次请求后都没能响应,那么SSH客户端就自动断开连接并退出,将控制权交给你的监控程序。这两个选项的设置方法分别是在ssh时加入-o &​nbsp;​ServerAliveInterval=n和-o ServerAliveCountMax=m。其中n,​ m可以自行定义。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​如何将端口绑定到外部地址上</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​使用上面的方法,映射的端口只能绑定在127.0.0.1这个接口上。也就是说,只能被本机自己访问到。如何才能让其他机器访问这个端口呢?我们可以把这个映射的端口绑定在0.0.0.0的接口上,方法是加上参数-b &​nbsp;​0.0.0.0。同时还需要打开SSH服务器端的一个选项-GatewayPorts。默认情况下它应当是被打开的。如果被关闭的话,可以在/​etc &​nbsp;/​sshd_config中修改GatewayPorts no为GatewayPorts yes来打开它。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​通过SSH隧道建立SOCKS服务器</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果我们需要借助一台中间服务器访问很多资源,一个个映射显然不是高明的办法(事实上,高明确实没有用这个方法)。幸好,SSH客户端为我们提供了通过SSH隧道建立SOCKS服务器的功能。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过下面的命令我们可以建立一个通过123.123.123.123的SOCKS服务器。</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -N -f -D 1080 123.123.123 # 将端口绑定在127.0.0.1上</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2.ssh -N -f -D 0.0.0.0:​1080 123.123.123.123 # 将端口绑定在0.0.0.0上</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过SSH建立的SOCKS服务器使用的是SOCKS5协议,在为应用程序设置SOCKS代理的时候要特别注意。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​以上摘录自:</​em><​a target="​_blank"​ title="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ href="​http://​blog.jianingy.com/​content/​%E2%80%A6"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​http://​blog.jianingy.com/​content/​…</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​@@@@@@@@@@@@@@@@@@@@@@@@@@@</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1 &​gt;&​nbsp;​反向隧道技术</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景:节假日需要回公司加班。但是公司是内网,使用NAT,所以没办法连回去。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在公司机器(LAN_ip)上执行</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -NfR 2222:​localhost:​22 home_ip</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R : 建立反向连接 将 home_ip port转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,到home_ip上面<​br/>​ ssh localhost -p 2222<​br/>​ 就跑在NAT后面的公司机器。就是后门了。灰鸽子木马用的也是反向链接。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​Destination (<​em>​LAN_ip</​em>​) &lt;- |NAT| &lt;- Source (<​em>​home_ip</​em>​)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2&​gt;​端口转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景:本机不允许访问www.xxx.com这个网站,但是远程主机(<​strong><​span style="​font-size:​18px;​line-height:​32px">​remote_ip</​span></​strong>​)可以。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -f -N -L 31609:​www.xxx.com:​80&​nbsp;<​em>​user@</​em><​strong><​span style="​font-size:​18px;​line-height:​32px">​remote_ip</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在我们就可以在本地打开&​nbsp;​http://​localhost:​31609&​nbsp;​访问www.xxx.com了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​3&​gt;&​nbsp;​SOCKS代理</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景: 本机不允许访问某些网站,但是远程主机(remote_ip)可以,并且公司没有组织你连接remote_ip。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -NfD 8888&​nbsp;<​em>​user@</​em>​remote_ip</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在在浏览器socks 5 proxy设置为localhost:​8888,​所有之前无法访问的网站现在都可以访问了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​@@@@@@@@@@@@@@@@@@@</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​古人以“夜不闭户”、“路不拾遗”来形容大治之世。他们认为大治时,人们的道德水平已经很高了。而早期Internet的形成似乎也是符合这种思想的。<​strong>​Internet的初衷是相互信任和相互分享</​strong>​,它的这种“信任”建立在“大治”甚至“天下大同”的假想上。随着互联网的商业化和平民化,Internet的安全性就显得越来越脆弱了,不仅网络攻击、网络诈骗盛行,也有<​strong>​一些网络管理者肆意检测、更改、阻断用户通信的行为</​strong>​。<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​SSH</​span></​a>​的一些特性在很多场合可以解决这些问题。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的基本用法在<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​命令行也强大之ssh</​span></​a>​一文中做了最基本的介绍。ssh是安全的远程登录工具,在与远程主机进行交互时,ssh协议把原始数据进行了加密并压缩,从而保证了数据在网络传输过程的安全。利用这一原理,我们也可以用ssh把其它的明文传输的TCP数据流通过ssh的连接传输,这就是本文要讲到的ssh转发/​隧道技术。下图表示一个ssh连接(图片来自<​a target="​_blank"​ href="​http://​www.ssh.com/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​www.ssh.com</​span></​a>​)<​br/>​ <br/> <img alt="​ssh连接"​ src="​http://​www.ssh.com/​support/​documentation/​online/​ssh/​adminguide/​32/​tunnel1-1.gif"/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的转发有四类:<​strong>​远程转发、本地转发、动态转发和X转发</​strong>​。把TCP流重定向到ssh连接叫做“转发”,这个ssh连接叫做“隧道”。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设场景:受限网络内主机A,受限网络防火墙F,受限网络外主机B,受限网络外主机C、D、E。用户为主机A的管理者且拥有主机B的ssh账号,B、C、D、E可以正常连接。因为防火墙F的策略,A与C、D、E完全不能连接,A与B受限连接(当A与B的连接内有关键字“SB”则阻断连接,且只能由A与B连接,不能由B主动连接,可类比为NAT)。<​br/>​ 下图为场景图(用Dia v0.96.1绘制)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​a target="​_blank"​ href="​http://​blog.lilinux.net/​wp-content/​uploads/​2010/​02/​stage.png"​ style="​color:​rgb(21,​80,​103)"><​img alt="​场景"​ src="​http://​blog.lilinux.net/​wp-content/​uploads/​2010/​02/​stage.png"/></​a></​p><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​一、远程转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的,​ 远程主机上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口.--摘自ssh的中文Manual<​br/>​ 命令格式为:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-R&​nbsp;::</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设主机A提供了HTTP服务,即在受限网络内部可以访问的网页服务器。但这个服务因为防火墙策略或者NAT路由的原因导致在Internet无法访问。那么我们就可以将本地的HTTP服务远程转发到Internet上的主机B</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgR&​nbsp;​80:​localhost:​80&​nbsp;​root@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后在Internet上可以通过访问 http://​host-B来访问主机A上的HTTP服务了</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果没有主机B的root账号,则只能远程转发到1024以后的端口号</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgR&​nbsp;​8080:​localhost:​80&​nbsp;​lige@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后在Internet上可以通过访问http://​host-B:​8080来访问主机A上的HTTP服务。<​br/>​ 值得注意的是搭建了<​a target="​_blank"​ title="​wordpress"​ href="​http://​blog.lilinux.net/​tag/​wordpress/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​wordpress</​span></​a>​的主机只能运行在80端口,如果转发后的端口不是80,那么将无法使用,这应该算是wp的bug吧</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而且因为主机A到主机B之间是通过隧道连接的,即使网页中含有关键字“SB”,也无法被大防火墙检测到(泪流满面,终于讲到关键了)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​附加参数简要说明:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​-N&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​不执行远程命令.&​nbsp;​用于转发端口.&​nbsp;&​nbsp;​-n&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​把&​nbsp;​stdin&​nbsp;​重定向到&​nbsp;/​dev/​null&​nbsp;​(实际上防止从&​nbsp;​stdin&​nbsp;​读取数据).&​nbsp;&​nbsp;​ssh在后台运行时一定会用到这个选项.&​nbsp;​它的常用技巧是远程运行&​nbsp;​X11&​nbsp;​程序.&​nbsp;&​nbsp;​-f&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​要求&​nbsp;​ssh&​nbsp;​在执行命令前退至后台.&​nbsp;​它用于当&​nbsp;​ssh&​nbsp;​准备询问口令或密语,​但是用户希望它在后台进行.&​nbsp;​该选项隐含了&​nbsp;​-n&​nbsp;​选项.&​nbsp;​在远端机器上启动X11&​nbsp;​程序的推荐手法就是类似于&​nbsp;​ssh&​nbsp;​-f&​nbsp;​host&​nbsp;​xterm&​nbsp;​的命令.&​nbsp;&​nbsp;​-C&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​要求进行数据压缩&​nbsp;&​nbsp;​-g&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​允许远端主机连接本地转发的端口</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​关于建立隧道和使用隧道的网络原理,见文章末尾</​p><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​二、本地转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 同时远程主机和 host的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. --摘自ssh的中文Mannul</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​它的命令格式为</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;::</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​和远程转发原理类似但方向相反。假设主机B有HTTP服务,但因为受限网络内其它主机因憎恶“大防火墙”F而不愿意直接访问主机B(这种心理我也有)。那就可以通过本地转发了</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgL&​nbsp;​80:​localhost:​80&​nbsp;​root@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​因为和远程转发类似,后面就不再详述。</​p><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​三、动态转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​指定一个本地机器 “动态的” 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS 协议, ssh &​nbsp;​将充当 SOCKS 服务器.只有 root 才能转发特权端口. --摘自ssh的中文manual</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​可以这样理解,远程转发和本地转发都是在隧道上传输一个服务,比如例子中的HTTP数据流。而动态转发则是为了在隧道上传输多种不确定的网络服务。它的表现就把远端ssh服务器当作了一个安全的代理服务器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​命令格式为</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-D</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设主机A想要访问受限网络所限制访问的主机C、D、E等,或者想要访问主机B上的“SB”内容。则可有D选项建立动态隧道</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-CfNg&​nbsp;​-D&​nbsp;​8888&​nbsp;​lige@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后你就有了一个安全的socks 5代理。这个代理ip是127.0.0.1,端口为刚才指定的8888。如何使用socks 5代理在本文就不再讲了。大防火墙可能还存在一些技术来防止socks 5代理,可以参考<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​socks%e4%bb%a3%e7%90%86%e7%9a%84%e8%bf%9c%e7%ab%afdns%e8%a7%a3%e6%9e%90/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​socks代理的远端DNS解析</​span></​a>​一文</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​还有一个plink工具,包含在PuTTY软件包,可以在windows和<​a target="​_blank"​ title="​linux"​ href="​http://​blog.lilinux.net/​tag/​linux/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​linux</​span></​a>​下使用,它的格式为:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​CMD</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​PLINK.EXE&​nbsp;​-C&​nbsp;​-D&​nbsp;​127.0.0.1:​7777&​nbsp;​-N&​nbsp;​-pw&​nbsp;​SSH密码&​nbsp;​SSH用户名@SSH的IP地址</​pre>​ </​td></​tr></​tbody></​table><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​四、X转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​X转发是一个令人惊奇但实际上用处不大的功能。因为我所能管理的远端主机都是无图形界面的,所以本部分内容我不保证正确。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​大家都知道GUI软件与X之间也是通过TCP连接的,所以X转发与其它转发也没有本质区别,甚至它简单得都不应该和其它几种转发相提并论。X转发用的选项为”-X”</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先要在客户端指定X Server的位置</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​export&​nbsp;​DISPLAY=:​.</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后执行</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">&​nbsp;​ssh&​nbsp;​-X&​nbsp;​lige@hot-B</​pre>​ </​td></​tr></​tbody></​table><​h2 style=";​padding:​0px;​line-height:​37px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​补充:可能的问题:</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1. ssh的基本用法<​br/>​ <a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​blog.lilinux.net/​2010/​02/​command-ssh/</​span></​a>​以及ssh的man文档</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2. windows下的ssh客户端<​br/>​ PuTTY和其中的plink</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​3. 如何查看隧道是否建立完善<​br/>​ 查看进程:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ps&​nbsp;​aux&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​查看端口:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​netstat&​nbsp;​-ntl</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​4. 因为F,连接还是不稳定,隧道经常无法使用<​br/>​ 去除-N,​-n,​-f选项,进入远程shell,<​strong>​输入一些需要频繁交互数据的命令,使连接一直处于活动状态</​strong>​。比如</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​top</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​我更推荐用这样的命令,对系统的负载消耗更小(而且更加美观)</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​watch&​nbsp;​-n1&​nbsp;&​quot;​date&​quot;</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​5. 更多的实例</​strong><​br/>​ 本文写的例子都是假设场景,近期将写一个实例应用。预计网址为<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​ssh-instance/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​blog.lilinux.net/​2010/​02/​ssh-instance/</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ </​p><​h1 style=";​padding:​0px;​line-height:​50px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a><​strong>​附ssh隧道原理</​strong></​h1><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​对网络原理不太懂或不感兴趣的可以忽略此部分<​br/>​ </​em><​br/>​ 总的来说:Tunnel实际上是一个TCP连接,这个连接和普通的SSH登录的连接并没有本质上的区别</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设有两台主机A和B,IP分别是IP_A和IP_B,是要把主机A的22端口映射到主机B的10022端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​连接的建立过程是这样的:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​1. 建立TUNNEL (在A主机上执行命令)<​br/>​ </​strong>​ssh命令,把A的端口映射到B主机(ssh -NfR 10022:​localhost:​22 USER_B@IP_B)<​br/>​ 这个步骤实际上是这样的:<​br/>​ (1)ssh命令是ssh客户端,这条命令是指在A主机上登录B主机,并且A主机请求将A主机的22号端口映射到B主机的10022端口。<​br/>​ (2)B主机接受远程登录请求后,检测发现用户的权限足以创建10022端口,且10022端口并未使用。则B主机开启一个监听端口:10022<​br/>​ (3)这条命令成功结束后,A主机有了一个后台进程登录到B主机,这个登录的连接正是“TUNNEL”。<​br/>​ 而B主机相应也有这么一个后台进程,此外B主机还增开了一个监听端口:10022<​br/>​ (4)在这个过程中,B是Server,A是Client。那么A是以一个任意端口连接到B的ssh监听端口。所设这个连接是<​br/>​ IP_A:12345 —— IP_B:​22</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​2. 连接映射端口 (在B主机上执行命令)<​br/>​ </​strong>​连接映射端口 ( ssh USER_A@localhost -p 10022)<​br/>​ 这个步骤实际上是这样的:<​br/>​ (1)主机B连接自己的一个监听端口10022<​br/>​ 于是主机B以一个任意的端口连接10022: IP_B:23456 ——- IP_B:​10022<​br/>​ (2)10022端口实际是由主机B的sshd来维护的,sshd发现有连接到10022的端口的数据。<​br/>​ 将数据封装成ssh包,通过已经建立好的TUNNEL传输到对端主机A的12345端口(TUNNEL建立时的初始端口)<​br/>​ (3)主机A的12345端口收到数据后,经过TUNNEL的建立进程的处理(解开ssh包),把数据转到主机A的端口22(建立TUNNEL时指定的端口)<​br/>​ (4)因此,这个反向的连接就建立起来了</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​3. TUNNEL与TCP连接的关系<​br/>​ </​strong>​(1)TUNNEL本质是一个TCP连接<​br/>​ (2)TUNNEL的作用用于传输其它TCP连接的数据<​br/>​ (3)对用户而言,TUNNEL就是它们可见的TCP连接传输的一个“隧道”<​br/>​ </​p><​p><​em><​strong><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869405568124.png"/></​strong></​em>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em><​strong>&​nbsp;​如需转载,请注明出处:</​strong></​em><​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​ssh-tunnel/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)"><​em><​strong>​http://​blog.lilinux.net/​2010/​02/​ssh-tunnel/</​strong></​em></​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​题目有点长<​br/>​ 其实来自于一个朋友的问题<​br/>​ 他的oracle跑在linux服务器上<​br/>​ 远程的linux服务器并没有跑在图形界面下(runlevel=5)<​br/>​ 而是跑在字符模式下(runlevel=3)<​br/>​ 而他又想用图形界面来管理oracle<​br/>​ 我翻了翻资料<​br/>​ 找出一种方法<​br/>​ <span style="​color:​rgb(0,​0,​255)"><​strong>​在一台图形界面的linux下(虚拟机也行,其实不是linux也行,关键是本地得有X server)起一个控制台<​br/>​ 用命令行ssh加参数”-X”连上远程的linux服务器<​br/>​ 像这样<​br/>​ ssh -X oracle@1.1.1.1<​br/>​ 这里”1.1.1.1″是远程linux服务器的ip地址<​br/>​ 这样连上去之后<​br/>​ 直接就可以用以前只有在图形界面上才能用的命令<​br/>​ 如oracle管理的图形程序”dbca”</​strong></​span></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这个原理很简单<​br/>​ 因为远程的sshd支持x11 forward<​br/>​ 那么他起x程序的时候<​br/>​ 就会把x的包通过ssh转回来到ssh客户端的机器上来<​br/>​ 而ssh客户端这边跑得有x server<​br/>​ 所以就能正确接收x包并显示出来</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​其实在windows下也可以通过安装x server软件来实现一样的效果<​br/>​ 比如x manager就可以<​br/>​ 具体用法不在详述</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里的关键在于<​br/>​ 远程的sshd需要支持x11 forward<​br/>​ 不过不要担心<​br/>​ 一般情况下<​br/>​ 缺省是支持的</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h1 style=";​padding:​0px;​line-height:​50px"​><​a></​a><​a target="​_blank"​ style="​color:​rgb(106,​57,​6)"></​a>​SSH端口转发(本地端口转发、远程端口转发、动态端口转发)工作原理、应用详解</​h1><​p>​Posted on&​nbsp;<​a target="​_blank"​ title="​3:​19 pm" href="​http://​lesca.me/​blog/?​p=514"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(30,​144,​255)">​March 1, 2011</​span></​a>&​nbsp;​by&​nbsp;<​a target="​_blank"​ title="​View all posts by lesca" href="​http://​lesca.me/​blog/?​author=1"​ style="​color:​rgb(21,​80,​103)">​lesca</​a>​ &​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​实验条件:</​strong><​br/>​ 1.本文所述之实验,本地主机操作系统基于Ubuntu 10.04,远程主机操作系统基于BSD发行版<​br/>​ 2.本地主机安装了ssh client(ssh),也安装了ssh server(sshd)<​br/>​ <​strong>​基础知识:</​strong><​br/>​ 文章中客户端指ssh,用于发出连接请求;服务端指sshd,监听端口22。<​br/>​ 安全通道是指ssh与sshd之间已建立的连接通道,该连接是加密的。<​br/>​ <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​SSH 端口转发格式<​sup style="​line-height:​27px">​[1]</​sup></​span></​p><​p style=";​margin-bottom:​10px;​margin-left:​4em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(0,​119,​0)">​[-L [bind_address:​]port:​host:​hostport]<​br/>​ [-R [bind_address:​]port:​host:​hostport]<​br/>​ [-D [bind_address:​]port]</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​动态端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-D &​lt;​port&​gt;</​span><​br/>​ 这会使本地主机监听端口<​strong>​port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道发送到远程主机。远程主机的sshd会根据数据包的应用层协议(如HTTP)自动建立对应的连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-D&​nbsp;​7070&​nbsp;​sisca@216.194.70.6</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​127.0.0.1:​7070&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​5205/​ssh&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;::​1:​7070&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​5205/​ssh</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​(什么都没有)</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:我们发现,动态端口转发的时候确实是本地主机的ssh在监听端口7070的<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-L &​lt;​local_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;​ &​lt;​ssh_server&​gt;</​span><​br/>​ 这会使本地主机监听端口<​strong>​local_port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道,同时<​strong>​远程主机</​strong>​会与主机<​strong>​host</​strong>​的端口<​strong>​host_port</​strong>​建立连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-L&​nbsp;​7001:​localhost:​7070&​nbsp;​sisca@216.194.70.6</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​127.0.0.1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​3475/​ssh&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;::​1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​3475/​ssh</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​(什么都没有)</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:我们从中可以看出,本地端口转发的时候确实是本地主机的ssh在监听端口7001<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​远程端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-R &​lt;​remote_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;​ &​lt;​ssh_server&​gt;</​span><​br/>​ 这会使<​strong>​ssh_server</​strong>​(远程端)监听端口<​strong>​remote_port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道,并且<​strong>​本地主机</​strong>​会与主机<​strong>​host</​strong>​的端口<​strong>​host_port</​strong>​建立连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-R&​nbsp;​7001:​localhost:​7070&​nbsp;​sisca@216.194.70.6</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​sisca&​nbsp;&​nbsp;&​nbsp;&​nbsp;​sshd&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​66196&​nbsp;​7&​nbsp;&​nbsp;​tcp4&​nbsp;&​nbsp;&​nbsp;​127.0.0.1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​*:​*</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:很明显,使用远程端口转发时,本地主机的端口监听并没有发生变化,相反远程主机却开始监听我们指定的7001端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小提示:<​br/>​ 这里的<​em>​remote_port</​em>,<​em>​local_port</​em>​可以归并为<​strong>​listen_port</​strong>​,因此我们得到这样的助记格式:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​[-L|-R]&​nbsp;&​lt;​listen_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地端口转发应用:<​sup style="​line-height:​27px">​[2]</​sup></​span><​br/>​ <span style="​color:​rgb(0,​0,​51)"><​strong>​背景:</​strong>​在实验室里有一台 LDAP 服务器(LDAP Server),但是限制了只有本机上部署的应用才能直接连接此 LDAP 服务器。现在我们想临时从本地机器(LDAP Client)直接连接到这个 LDAP 服务器,只需要在<​strong>​LDAP Client</​strong>​上运用本地端口转发:</​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-L&​nbsp;​7001:​localhost:​389&​nbsp;​LdapServerHost</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​图1 本地端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd01.jpg"​ width="​300"​ height="​208"/><​br/>​ <​strong>​数据流向:</​strong><​br/>​ 该命令首先使本地主机(LDAP Client)与远程主机(LDAP Server)建立一个SSH安全连接(安全通道)<​br/>​ 然后本地主机会监听端口7001(注意是<​span style="​color:​blue">​ssh</​span>​在监听)<​br/>​ 一旦本地LDAP客户端程序将数据发送到端口7001(即建立连接),远程主机上的<​span style="​color:​blue">​sshd</​span>​将主动与端口389(LDAP服务的端口)建立连接<​br/>​ 从而来自LDAP客户端的请求转发到该端口389上(当然是通过安全通道)<​br/>​ <​strong>​注意:</​strong><​br/>​ 本地主机有两个客户端:<​br/>​ 1) ssh – 用于与远程主机建立ssh连接<​br/>​ 2) LDAP客户端 – 用于与远程主机的LDAP服务程序进行通讯<​br/>​ 远程主机对应地有两个服务进程:<​br/>​ 1) sshd – 监听端口22<​br/>​ 2) ldapd -监听端口389<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​远程端口转发应用:<​sup style="​line-height:​27px">​[2]</​sup></​span><​br/>​ <span style="​color:​rgb(0,​0,​51)"><​strong>​背景:</​strong>​这次假设由于网络或防火墙的原因我们不能用 SSH 直接从 LDAP Client 连接到 LDAP 服务器(LDAP Server),但是反向连接却是被允许的。那此时我们的选择自然就是远程端口转发了。我们这次在<​strong>​LDAP Server</​strong>​上运用远程端口转发,因此LDAP Client必须安装了sshd</​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-R&​nbsp;​7001:​localhost:​389&​nbsp;​LdapClientHost</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​图2 远程端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd02.jpg"​ width="​300"​ height="​201"/><​br/>​ <​strong>​数据流向:</​strong><​br/>​ 该命令首先使LDAP Server与 LDAP Client 建立一个SSH安全连接(安全通道)<​br/>​ 然后LDAP Client开始监听端口7001(注意是<​span style="​color:​blue">​sshd</​span>​在监听)<​br/>​ 一旦LDAP Client将数据发送到端口7001(即建立连接),LDAP Server上的<​span style="​color:​blue">​ssh</​span>​将主动与端口389(LDAP服务端口)建立连接<​br/>​ 从而来自LDAP客户端的请求转发到该端口389上(当然是通过安全通道)<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地转发与远程转发的对比与分析</​span><​br/>​ 本地端口转发:ssh客户端发起ssh连接,并且监听指定端口<​br/>​ 远程端口转发:ssh客户端发起ssh连接,但是<​strong>​由远程主机上的sshd监听指定端口<​br/>​ <br/> <br/> 多主机转发应用<​sup style="​line-height:​27px">​[2]</​sup></​strong><​br/>​ <span style="​color:​rgb(0,​0,​51)">​图3 多主机端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd03.jpg"​ width="​300"​ height="​212"/></​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-g&​nbsp;​-L&​nbsp;​7001:&​lt;​B&​gt;:​389&​nbsp;&​lt;​D&​gt;</​pre><​p></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​注意:我们在命令中指定了“ -g ”参数以保证机器(A)能够使用机器(C)建立的本地端口转发。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​13px;​color:​rgb(102,​102,​102);​line-height:​23px">​References:<​br/>​ [1] man 1 ssh<​br/>​ [2] IBM DeveloperWorks –&​nbsp;</​span><​a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)"><​span style="​font-size:​13px;​color:​rgb(30,​144,​255);​line-height:​23px">​实战 &​nbsp;​SSH 端口转发</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p><​span style="​color:​rgb(0,​136,​0)">​Please support us if you feel this post is useful.<​br/>​ <br/> </​span>​ <span style="​color:​rgb(0,​136,​0)"><​br/>​ </​span>​ <span style="​color:​rgb(0,​136,​0)"></​span>​ </​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​名为</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;​secure &​nbsp;​shell,&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​是专门用来进行安全远程访问的协议。它无论从功能上、系统兼容性上、还是从安全上和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Telnet</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​RSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​相比都是足以取而代之。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial;​text-indent:​48px"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​在这个</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​ARP</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​欺骗横行,网上盗窃泛滥的年代,还有什么比安全更为重要的?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial;​text-indent:​48px"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​在这个遍地都是防火墙的年代,还有什么比通过一个端口就能访问所有业务更为重要的呢?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ &​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​在矛和盾同样发达的年代,</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可以给我们什么样的方便?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​现代的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UNIX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​类操作系统,无一例外地将</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,作为默认系统软件包进行安装。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​最</​span><​span style="​line-height:​1.5;​font-family:​宋体">​基本的连接方式:<​span style="​line-height:​1.5">​SSH &​nbsp;​Hostname</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​提供了多种不同的接入认证方式:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ Password</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ Hostkey</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ .....</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ .....</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​其中常用的有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;​password</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,</​span><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​host &​nbsp;​public key</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,</​span><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​如果使用</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​password</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,则会产生用户交互的密码输入,如果使用<​span style="​line-height:​1.5">​host &​nbsp;​key</​span>​, 则可以直接登录或直接执行远程命令。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ SSH Client&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Server</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的交互,能实现很多其他非常有用的功能:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​最基本的功能有如下选项:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​会话内容的压缩,可以使用<​span style="​line-height:​1.5">​gzip</​span>​压缩级别进行指定,以提高在慢速线路上数据传输效率。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​本地端口转发,可以将本地某个端口和远程<​span style="​line-height:​1.5">​SSH</​span>​服务器上、或<​span style="​line-height:​1.5">​SSH</​span>​远程服务器可达的地址端口建立一种映射关系,使对本地端口的访问请求会透明地转发到远程服务器上。从远程服务器上所看到的来访请求源地址将是<​span style="​line-height:​1.5">​SSH</​span>​服务器的地址。</​span></​span></​p><​p style=";​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px"></​span></​p><​p><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px"><​img alt="​Secure Network (ssh端口映射)-----图详细可以使用 - zhuzhu - 五事九思 ​ (大连Linux主机维护)"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869407643601.gif"/></​span>​ <span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px">&​nbsp;</​span>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5"></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​远程端口反向转发,与上列相反, 远程端口反向转发建立后,远程客户机访问远程<​span style="​line-height:​1.5">​SSH</​span>​服务器的某端口,<​span style="​line-height:​1.5">​SSH</​span>​服务器将把来访请求通过<​span style="​line-height:​1.5">​SSH</​span>​安全通道转发到<​span style="​line-height:​1.5">​SSH</​span>​客户端或<​span style="​line-height:​1.5">​SSH</​span>​客户端可达的服务器地址端口上,从而使远程客户机,可以正常访问内部服务器所提供的服务。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"></​span>&​nbsp;</​p><​p><​span style="​font-family:​宋体;​background-color:​rgb(245,​247,​248);​font-size:​16px"><​img alt="​Secure Network (ssh端口映射)-----图详细可以使用 - zhuzhu - 五事九思 ​ (大连Linux主机维护)"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869407359564.gif"/></​span><​span style="​color:​rgb(0,​136,​0)"></​span>​ </​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">​这样,SSH客户端D,就可以通过访问SSH服务器B的端口D,从而达到访问业务服务器C上端口D的目的。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​高级功能<​span style="​line-height:​1.5">/</​span>​用法有:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​端口转发的接续。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">​由于现代的<​span style="​line-height:​1.5">​UNIX</​span>​系统都默认带有<​span style="​line-height:​1.5">​SSH</​span>​服务和客户端,所以当网络变得比较复杂、层次很多的时候,使用多台机器的端口转发接续,可以将在层次很深的内网服务,在不改变当前网络结构和防火墙配置的情况下,层层转发出来从而可以使外网用户很方便地使用该内网服务,而无须对现有网络构架和甚至防火墙配置作任何变动。而对最外层的防火墙只需要开放一个<​span style="​line-height:​1.5">​22</​span>​端口(默认<​span style="​line-height:​1.5">​ssh</​span>​服务端口)就能使授权的客户端访问全网业务。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​动态端口转发。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSHv2</​span><​span style="​line-height:​1.5;​font-family:​宋体">​,同时还支持内置的<​span style="​line-height:​1.5">​SockS4 &​nbsp;​Server</​span>​,将<​span style="​line-height:​1.5">​ssh&​nbsp;</​span>​客户端的指定端口,作为一个远程<​span style="​line-height:​1.5">​SOCKS4 Server</​span>​的端口。对<​span style="​line-height:​1.5">​SockS4Server</​span>​的访问请求将通过,<​span style="​line-height:​1.5">​SSH &​nbsp;​Protocol&​nbsp;</​span>​传输到&​nbsp;<​span style="​line-height:​1.5">​SSH&​nbsp;</​span>​服务器,&​nbsp;<​span style="​line-height:​1.5">​SSH</​span>​服务器将请求转发到用户应用的真正目的服务器。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​的&​nbsp;<​span style="​line-height:​1.5">​PPP</​span>​通道。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​结合<​span style="​line-height:​1.5">​UNIX PPPD</​span>​,还可以在<​span style="​line-height:​1.5">​SSH</​span>​的<​span style="​line-height:​1.5">&​nbsp;​PTS&​nbsp;</​span>​通道上建立<​span style="​line-height:​1.5">​PPP &​nbsp;​LINK</​span>​从而非常灵活的实现&​nbsp;<​span style="​line-height:​1.5">​IP over SSH&​nbsp;</​span>​的<​span style="​line-height:​1.5">​VPN</​span>​。这种方法,可谓<​span style="​line-height:​1.5">​SSH</​span>​的终极用法,因为它解决了所有由于地址翻译而带来的应用层协议不兼容的问题(多播问题是否能解决尚未测试)。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH</​span><​span style="​line-height:​1.5;​font-family:​宋体">​产品实战。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SecureCRT</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VanDyke &​nbsp;​Software&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的经典力作,几乎囊括了所有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端的功能,在此,</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的标准功能就不多说了。说一些</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​windows</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​上的特色功能使</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​变得非常灵活。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​1、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​它拥有端口转发安全过虑器,可以灵活地允许或禁止其他客户机访问被</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​所转发的本机端口。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​2、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​同时还拥有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VBSCript/​Jscript/​Perl&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​脚本功能,可以通过这三类脚本语言和远程服务器进行自动交互式执行。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​3、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可以自动和服务器上的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Zmodem</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​进行交互,实现基于</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​TTY/​PTS</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Zmodem&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​文件传输(这个功能可在任何类型的会话中进行,并不局限于</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​类型的会话)。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​4、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​自动和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureFX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​匹配实现</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SFTP</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的文件传输功能(无需输入用户名密码,因为是基于已建立的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH &​nbsp;​Session</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的,所以登录速度是非常快的)由于SecureFx带有拖放功能,和类似文件夹的拷贝粘贴功能,所以会给文件传输带来很大方便性(FTP还是很不方便的)。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​5、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​支持命令行方式,可在</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​CMD</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​中实现一步登录并将端口转发作为参数输入(和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UNIX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​基本没区别了)。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​功能太多了,就不一一列举了。详细请查看</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VanDyke Software</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的主页和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的帮助文件。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​UniTTY</​span></​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;</​span>​A Pure Java Based SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端。居然实现了几乎所有的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​功能。而且更牛的是,实现了端口转发的动态加载。而且默认实现了</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SFTP &​nbsp;​over SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的文件传输。只是字体有待改进。看惯了</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的优雅美观的字体,再用</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UniTTY</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可能就有些不习惯了。它的多协议支持能力也很特别,居然将</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VNC</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的客户端协议也加进来了。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​UNIX SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​完全基于OpenSSH,最标准的客户端。能完成所有SSH协议支持的功能,同时结合UNIX的脚本,能完成其他操作系统无法完成的很多任务。或配合完成前面所提到的一些用法!</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​NetSarang XSHELL</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ 韩国人写的多协议客户端,和他另外一个在国内很流行的产品(Xmanager)配合使用很好。默认支持TAB窗口的SFTP,使用也比较方便,也同时支持双向的端口转发。</​span></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ MindTerm</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ 纯java的ssh客户端,非常著名的开源软件mindterm。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​mindterm,居然提供了源代码真是不错,以后可以将expect也扔了。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​而且该有的都有了(sockserver,​ &​nbsp;​端口转发,反向转发,scp,telnet,zlib compression,keepalive等),还额外提供了ftp &​nbsp;​协议桥接sftp的功能,就是有一点不方便,在哑终端方式启动,需要X11Display支持。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">&​nbsp;&​nbsp;&​nbsp;​ zenith 2007.9.10</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​br/>​ </​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​br/>​ </​span></​span></​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​24px;​color:​rgb(0,​0,​255);​line-height:​43px">​网的朋友苦于没有合法 ip,​不能对外提供 internet 服务。解决方案很多,可以通过在网关做端口映射,或其他的辅助软件等。</​span></​strong></​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​24px;​color:​rgb(0,​0,​255);​line-height:​43px">​本文介绍两种比较简单实用的方法,利用 ssh 这个强大的工具。<​br/>​ (以下方法不分平台,都适用)</​span></​strong></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​案例一、<​br/>​ 内网主机 A ,开了 http,ftp ,http ,​vnc,​sshd,​socks5,​cvs 等服务。无合法 ip 地址。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px"><​br/>​ 外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px"><​br/>​ 我们的目的是让 B 能访问 A 上的各种服务。</​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​span style="​line-height:​27px;​color:​rgb(59,​59,​59);​font-size:​15px">​步骤:</​span><​br/>​ <​strong><​span style="​font-size:​18px;​line-height:​32px"><​span style="​color:​rgb(59,​59,​59)">​1、</​span><​span style="​color:​rgb(255,​0,​0)">​A</​span><​span style="​color:​rgb(59,​59,​59)">​(内网服务器) &​nbsp;​知道&​nbsp;</​span><​span style="​color:​rgb(0,​0,​255)">​B</​span><​span style="​color:​rgb(59,​59,​59)">&​nbsp;​ip &​nbsp;​后,先用 ssh client 连上 B,命令如下:</​span><​br/>​ <span style="​color:​rgb(59,​59,​59)">​ssh -R 1234:</​span><​span style="​color:​rgb(255,​0,​0)">​localhost:​21&​nbsp;</​span><​span style="​color:​rgb(59,​59,​59)">&​nbsp;​ &nbsp; &​nbsp;​-l root &​nbsp;</​span><​span style="​color:​rgb(0,​0,​255)">​218.xxx.xxx.xxx</​span></​span></​strong></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​解释:<​br/>​ 关于 ssh 的参数,请看 ssh --help<​br/>​ -L listen-port:​host:​port Forward local port to remote address<​br/>​ -R listen-port:​host:​port Forward remote port to local address<​br/>​ -L local (本地) -R :remote (远程)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​-R 1234:​localhost:​21 其实做了个“端口转发(forward)&​quot;​。<​br/>​ 意思是主机 A 把本地的 21端口(对应ftp服务)映射为 B 的1234 端口(任意未被占用),同时 A 监听 B 的1234 端口。<​br/>​ 在 B 上用 netstat -al | grep 1234 ,你能看到这个监听连接。<​br/>​ 任何发送到 B 1234 端口的请求将被传送到 A的 21 端口。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​2、B 用 ftp 工具(任意,​如gftp) 连本地的 1234 端口,输入 A 的 ftp 用户和密码。<​br/>​ ftp localhost 1234</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​千万不要觉的奇怪,为什么连的是本地的地址。<​br/>​ 举个不恰当例子,相当于 A 在 B 的房间里装了个***(监听端口),那么 B 在房间里说的话就通过***传送到了 A。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​3、推广:<​br/>​ 如果 B 没占用 21 端口的话,那么可以写成:<​br/>​ A使用: ssh -R 21:​localhost:​21 -l root 218.xxx.xxx.xxx<​br/>​ B使用: ftp localhost</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​如果你想使用 A 上的 http 或其他服务,只需改变服务端口:<​br/>​ http服务 :<​br/>​ A使用:ssh -R 1234:​localhost:​80 -l root 218.xxx.xxx.xxx<​br/>​ B使用:w3m&​nbsp;<​a target="​_blank"​ href="​http://​localhost:​1234/"​ style="​color:​rgb(0,​113,​179)">​http://​localhost:​1234</​a></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​sshd服务:<​br/>​ A使用:ssh -R 1234:​localhost:​22 -l root 218.xxx.xxx.xxx<​br/>​ B使用:ssh localhost -p 1234</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​vnc 服务:&​nbsp;<​br/>​ A使用:ssh -R 1234:​localhost:​5901(其他) -l root 218.xxx.xxx.xxx<​br/>​ B使用:vncviewer localhost:​1</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​socks5服务:<​br/>​ A使用:ssh -R 1234:​localhost:​1080 -l root 218.xxx.xxx.xxx<​br/>​ B 略</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​cvs 服务:<​br/>​ A使用:ssh -R 1234:​localhost:​2401 -l root 218.xxx.xxx.xxx<​br/>​ B使用:cvs -d :​pserver:<​a target="​_blank"​ href="​mailto:​root@localhost"​ style="​color:​rgb(0,​113,​179)">​root@localhost</​a>:​1234/​home/​cvsroot login</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​这里是否一定要用 root ,​涉及到权限问题,具体还得靠大家来总结经验。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​案例二、<​br/>​ 部分朋友会问了,这样的话只是两台机器的互相通讯,如何让广域网的人都能访问呢?<​br/>​ 聪明的你,这时候可能已经有了答案。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​内网主机 A ,开了 http,ftp ,http ,​vnc,​sshd,​socks5,​cvs等服务。无合法 ip 地址。<​br/>​ 外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx<​br/>​ 我们的目的是让 internet 上的任何主机能访问 A 上的各种服务。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​步骤:<​br/>​ 1、首先,B 的sshd 服务端做点小小的设置:<​br/>​ vi /​etc/​ssh/​sshd.config<​br/>​ 加入 GatewayPorts yes<​br/>​ 然后重启 sshd 服务: /etc /init.d/ssh restart 或 /​etc/​init.d/​sshd restart<​br/>​ (解释:<​br/>​ 不加,默认会把监听端口绑定在 localhost 或 lo(127.0.0.1),这样除了 B自身别人是没法访问监听端口的。<​br/>​ 加入 GatewayPorts yes,把监听端口绑定到 0.0.0.0 ,​这样外部的所有机器都能访问到这个监听端口。<​br/>​ 主要是考虑安全性问题,默认情况,只允许本地访问。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​这里才是真正的难点,实验了一个晚上,累人呀!给点鼓励吧 :)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​2、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:<​br/>​ ssh -R 21:​localhost:​21 -l root 218.xxx.xxx.xxx<​br/>​ (事先确定 B 的21 端口未被占用)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​3、分布在 internet 的其它客户机使用 ftp 工具(任意),连 B 21端口。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​ftp 218.xxx.xxx.xxx 21</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​你会发现自己连上了内网 A 的ftp 服务。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​此法和案例一完全一样。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​internet ---------&​gt;&​gt;​ B 21 端口-----------&​gt;&​gt;​A 21端口<​br/>​ 可以叫做端口转发,或隧道技术,也可以称之为跳板(B),或反* 。呵呵,我瞎说的。。。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​可能遇到的问题:</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​Country:/​etc#​ ssh localhost -p 1234<​br/>​ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<​br/>​ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @<​br/>​ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<​br/>​ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!<​br/>​ Someone could be eavesdropping on you right now (man-in-the-middle attack)!<​br/>​ It is also possible that the RSA host key has just been changed.<​br/>​ The fingerprint for the RSA key sent by the remote host is<​br/>​ be:​5f:​d2:​45:​66:​4d:​0c:​9e:​2b:​6b:​45:​65:​a7:​b2:​85:​28.<​br/>​ Please contact your system administrator.<​br/>​ Add correct host key in /​root/​.ssh/​known_hosts to get rid of this message.<​br/>​ Offending key in /​root/​.ssh/​known_hosts:​11<​br/>​ RSA host key for localhost has changed and you have requested strict checking.<​br/>​ Host key verification failed.<​br/>​ Country:/​etc#​ ssh localhost -p 1234<​br/>​ <a target="​_blank"​ href="​mailto:​root@localhost'​s"​ style="​color:​rgb(0,​113,​179)">​root@localhost&#​39;​s</​a>&​nbsp;​password:<​br/>​ Last login: Mon May 5 02:39:53 2003 from localhost<​br/>​ localhost root #</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​如上问题,请删除 ~/​.ssh/​known_hosts,然后再试。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​点评:<​br/>​ 当然 ssh 还有很多的功能没有用,如先用 ssh 连接 上去后,可以用 scp命令来存取文件,等等。<​br/>​ scp -P xxx&​nbsp;<​a target="​_blank"​ href="​mailto:​user@host:​path/​file"​ style="​color:​rgb(0,​113,​179)">​user@host:​path/​file</​a></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​其它突破网关传送文件的方式也千变万化。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​优点是:<​br/>​ 可以突破网关,一般情况下,向网管要求在网关上给你做端口映射是不现实的,但用此法你可以让要好的朋友给你做。<​br/>​ 使用方案一:比较点对点传送文件比较方便,或使用ssh进行远程控制内网。<​br/>​ ssh本身是加密,保证安全可靠。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​缺点也不少:<​br/>​ 使用 ssh 加密,势必影响性能,可以用 -C 选项调节压缩率。<​br/>​ 如果象方案二使用额外的服务器,数据都要服务器中转(我是这样认为,没跟踪过),势必影响速度。<​br/>​ 公网的服务器不好找。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​建议:恳请编程高手们根据类似得原理,做个端口转发小工具,效果会更好。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​本文,雕虫小计,漏洞无数。旨在提供一种如何解决问题的思路,请大侠不要笑话。也请大家多多支持,共同提高。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​特别感谢 :sleeve ,x11 兄,​是他们帮助一起实现的。以及 irc.linuxfire.com 中的大伙:)</​p><​p><​br/></​p>​+<p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​实战 SSH 端口转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)">​https://​www.ibm.com/​deve</a><a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)">/​l-c</​a>​loperworks/​cn/​linux/​l-cn-sshforward/</​p><​style="​;​margin-bottom:​10px;padding-top:0px;padding-bottom:0px"><​a ​href="​https:​//​blog.csdn.net/​a351945755/​article/​details/​21785647" ​target="​_blank"​>​l来源:https://​blog.csdn.net/​a351945755/​article/​details/​21785647</​a></​p><​h2 ​style="​;padding:0px;​line-height:​37px"><​a target="​_blank"​ href="​http://​blog.sina.com.cn/​s/​blog_704836f40100lwxh.html"​ style="​color:​rgb(21,​80,​103)">​SSH的端口转发:​本地转发Local &​nbsp;​Forward和远程转发Remote Forward</​a></​h2><​h2 style=";​padding:​0px;​line-height:​37px"><​span style="​font-size:​13px;​line-height:​23px">​关于使用ssh portforwarding来进行翻墙的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下。</​span></​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先要记住一件事情就是:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​SSH 端口转发自然需要 SSH 连接,而 SSH 连接是有方向的,从 SSH Client 到 SSH Server 。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而我们所要访问的应用也是有方向的,应用连接的方向也是从应用的 Client 端连接到应用的 Server 端。比如需要我们要访问Internet上的Web站点时,Http应用的方向就是从我们自己这台主机(Client)到远处的Web Server。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果SSH连接和应用的连接这两个连接的方向一致,那我们就说它是本地转发。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;&​lt;​local&​nbsp;​port&​gt;:&​lt;​remote&​nbsp;​host&​gt;:&​lt;​remote&​nbsp;​port&​gt;&​nbsp;&​lt;​SSH&​nbsp;​hostname&​gt;</​pre><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869397988942.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果SSH连接和应用的连接这两个连接的方向不同,那我们就说它是远程转发。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-R&​nbsp;&​lt;​local&​nbsp;​port&​gt;:&​lt;​remote&​nbsp;​host&​gt;:&​lt;​remote&​nbsp;​port&​gt;&​nbsp;&​lt;​SSH&​nbsp;​hostname&​gt;</​pre><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869398300272.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​本地转发在本地这台机器上监听一个端口,然后所有访问这个端口的数据都会通过ssh 隧道传输到远端的对应端口上,下面是例子。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​本地端口转发绑定的是 lookback 接口,这意味着只有 localhost 或者 127.0.0.1 才能使用本机的端口转发 , &​nbsp;​其他机器发起的连接只会得到“ connection refused. &​nbsp;​”。要想让其他机器也能共享这个隧道来转发的话需要在配置文件中使用GatewayPorts 关键字,或者直接命令行下使用&​quot;​-g&​quot;​参数。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​另外本地转发命令中的 &​lt;​remote host&​gt;​ 和 &lt;SSH hostname&​gt;​ 可以是不同的主机。</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869398887271.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​远程转发与本地转发正好相反,打开ssh隧道以后,在远端服务器监听一个端口,所有访问远端服务器指定端口都会通过隧道传输到本地的对应端口上,下面是例子。</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869399841356.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里是一篇很好的介绍SSH PortForwarding的文章<​a target="​_blank"​ href="​http://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(30,​91,​119)">​http://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​可以将远端服务器一个端口remote_port绑定到本地端口port,其中-C是进行数据压缩,-f是后台操作,只有当提示用户名密码的时候才转向前台。-N是不执行远端命令,在只是端口转发时这条命令很有用处。-g &​nbsp;​是允许远端主机连接本地转发端口。-R表明是将远端主机端口映射到本地端口。如果是-L,则是将本地端口映射到远端主机端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的三个强大的端口转发命令:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​转发到远端:ssh -C -f -N -g -L 本地端口:​目标IP:​目标端口 用户名@目标IP</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​转发到本地:ssh -C -f -N -g –R 本地端口:​目标IP:​目标端口 用户名@目标IP</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g -D listen_port user@Tunnel_Host</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-C:压缩数据传输。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-f :后台认证用户/​密码,通常和-N连用,不用登录到远程主机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N :不执行脚本或命令,通常与-f连用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-g :在-L/​-R/​-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-L 本地端口:​目标IP:​目标端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 同时远程主机和 host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R本地端口:​目标IP:​目标端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的,​ 远程主机上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和 host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. IPv6 地址用另一种格式说明:​ &​nbsp;​port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-p :被登录的ssd服务器的sshd服务端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-D port</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​指定一个本地机器 “动态的&#​39;​’ 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, &​nbsp;​将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​应用举例</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.将发往本机的80端口访问转发到174.139.9.66的8080端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g<span style="​font-size:​18px;​line-height:​32px"><​strong>&​nbsp;​-L</​strong></​span>&​nbsp;​80:<​strong><​span style="​font-size:​18px;​color:​rgb(255,​0,​0);​line-height:​32px">​174.139.9.66:​8080&​nbsp;&​nbsp;&​nbsp;​ &​nbsp;</​span></​strong><​a target="​_blank"​ href="​mailto:​master@174.139.9.66"​ style="​color:​rgb(21,​80,​103)">​master@174.139.9.66</​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2.将发往174.139.9.66的8080访问转发到本机的80端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g<​strong><​span style="​font-size:​18px;​line-height:​32px">&​nbsp;​-R</​span></​strong>&​nbsp;​80:​174.139.9.66:​8080&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;<​a target="​_blank"​ href="​mailto:​master@174.139.9.66"​ style="​color:​rgb(21,​80,​103)">​master@174.139.9.66</​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N - 不使用Shell窗口,纯做转发的时候用,如果你在映射完成后继续在服务器上输入命令,去掉这个参数即可</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​例子A:​我们想远程管理服务器上的MySQL,​那么使用下面命令</​strong></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;​3306:​127.0.0.1:​3306&​nbsp;&​nbsp;&​nbsp;​user@emlog-vps&​nbsp;&​nbsp;&​nbsp;&​nbsp;​-N</​pre><​p><br/></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">&​nbsp;<​br/></​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​运行这个命令之后,ssh将会自动将服务器的3306映射到本机的3306端口,我们就可以使用任意MySQL客户端连接 localhost:​3306即可访问到服务器上的MySQL了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​例子B:​一次同时映射多个端口</​strong></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;​8888:​www.host.com:​80&​nbsp;​-L&​nbsp;​110:​mail.host.com:​110&​nbsp;​-L&​nbsp;&​nbsp;&​nbsp;&​nbsp;​25:​mail.host.com:​25&​nbsp;​user@host&​nbsp;​-N</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这个命令将自动把服务器的80,110,25端口映射到本机的8888,110和25端口 以上命令在ubuntu 9.10 上测试通过...</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h1 style=";​padding:​0px;​line-height:​50px">​端口映射让外部用户同时访问内部多个应用</​h1><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ 如下图所示,这是企业部署NAT服务器的典型案例。如果现在企业内部有两台服务器,分别为WEB服务器(192.168.0.3)与ERP服务器(192.168.0.2)。现在企业想让出差在外的员工,也可以通过<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​访问企业内部的这两个应用,该如何设计呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="​http://​img.ph.126.net/​mg8vaCplqo1apzBLkoAlCw==/​1524186998888700710.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;&​nbsp;&​nbsp;<​strong>&​nbsp;​ 一、基于端口访问的一般原理。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  在讲解NAT服务器的具体端口映射配置之前,笔者觉得读者有必要先了解一下基于端口访问的一般原理。这有利于读者了解下面的具体配置。下面笔者以员工访问WEB服务器为例,谈谈基于端口访问的基本步骤。假设现在某个用户需要访问WEB服务器,则客户端的浏览器(假设采用的端口为3000)与WEB服务器(假设采用的端口为80)是如何来进行通信的呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第一步:发起连接请求。当用户需要查看公司网页时,用户所用的主机会像WEB服务器发送请求。在这个发起请求的过程中,会有一些参数传递。如在发送请求的过程中,用户所用的主机需要知道WEB服务器的<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​、所采用的协议(HTTP)等。另外就是两个端口信息。一是用户浏览器所用的端口,即为3000。二是WEB服务器网站所用的端口(如果采用的是HTTP服务,则默认为80端口)。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第二步:WEB服务器转发请求。当服务器接受到这个请求后,会分析数据包。他经过分析包发现,这个请求是要求端口为80的应用软件来负责的。服务器确定了负责人之后,就会把这个请求转发给具体的负责人,即WEB应用服务器来处理。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第三步:服务器会把从WEB应用服务那边反馈回来的网页传送给用户所用的计算机。在传送的时候,其也会制定要把这个网页给你的那个3000端口(浏览器)对应的应用软件。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  第四步:用户的计算机收到数据包之后,也会对这个数据包进行分析,来判断需要把这个数据包转发给哪个端口(应用服务)。分析后发现是转交给3000端口的,就会把这个数据包转发给浏览器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​二、基于端口映射的NAT服务器配置。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  其实基于端口映射的NAT服务器其工作原理就跟上面这个基于端口访问的工作过程类似。只是这里的NAT服务器就相当于用户主机,而用户相当于内部的服务器。由于用户需要通过外网访问企业内部应用的话,通常情况下要求内网服务器也要求有合法的外网IP。只有私网<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​的服务器是无法直接跟外部客户进行通信的。为此,如果没有端口映射的话,用户如果要同时访问内部的WEB服务器与ERP服务器,则就需要有两个公网IP地址。但是,大家都知道现在公网IP地址严重缺乏。在没有足够多的IP地址的情况下,该如何实现这种需求呢?​那就知道通过端口映射来完成。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  如上图所示,用户需要访问企业内部的WEB服务与ERP服务器,他只需要知道NAT服务地址与WEB服务器与ERP服务器所采用的端口即可。如果</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​WEB服务器采用的端口为80,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ERP服务器所采用的端口为5050。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而NAT服务器的IP地址假设为202.96.92.100的话,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​那么当用户访问WEB服务器时,只需要输入http://​ &​nbsp;​202.96.92.100即可。由于HTTP协议默认采用的是80端口,故这里不用配置端口号。如果WEB服务器中把这个端口改为了3000,则在访问的时候就需要使用http://​ &nbsp; 202.96.92.100:​3000(地址+端口号)的形式了。用户要访问内部的ERP服务器也是类似的,只要把NAT服务器的地址以及ERP服务器所采用的端口在客户端上进行配置即可。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  可见通过NAT服务器的端口映射功能,可以让外部用户同时访问企业内部的WEB服务器与ERP服务器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​18px;​color:​rgb(255,​0,​0);​line-height:​32px">​那要实现这个需求具体该如何配置呢?</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  NAT服务器让内部的局域网用户可以连接到<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​,以便用户能够收发互联网邮件、浏览互联网上的网站等等。但是默认情况下外部用户是不能够访问内部的服务器。如果需要实现以上功能的话,就需要进行专门的配置。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  如上图中,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​内部局域网计算机(192.168.0.2)为WEB服务器,其采用的端口为80。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果现在外部用户需要访问这个WEB服务器站点,则用户只知道这个WEB服务器的地址为202.96.92.100(NAT服务器的地址)。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而WEB服务器默认采用的端口为80。当外部用户通过浏览器访问企业内部的WEB服务器时,</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​他会在浏览器中输入http://​ 202.96.92.100路经来连接网站。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​NAT服务器会将此请求发送给内部的计算机WEB服务器(<​strong>​192.168.0.2),</​strong>​并由WEB服务器将这个请求转发给端口为80的软件来负责。WEB服务器会将网页传送给NAT服务器,再由NAT服务器负责将网页传送给外部用户的计算机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果用户要访问内部的ERP服务器,其过程也是如此。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​那么现在的问题是,NAT服务器怎么判断要把用户HTTP请求发送给服务器(192.168.0.2)而不是发送给(192.168.0.3)呢?</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  这主要是根据端口来判断的。也就是说在NAT服务器中有一个端口映射的规则,只要用户访问的目的端口是80,就把这个请求转发给WEB服务器;​若请求的端口是5050,则就把这个请求发送给ERP服务器。这就是端口映射的实质。了解这个内容之后,那么了解下面的配置也就简单多了。打开路由和远程访问主控制窗口,选择要配置的服务器。然后选择IP路由选择、NAT/​基本防火墙。并双击右方对外连接的网络接口(注意这里不要选择连接内网的网络接口),选择服务和端口标签。然后直接从服务列表中选取要对外开发的服务,如选择WEB服务器。在公用地址中选择在此接口,表示由互联网服务提供商指派的NAT服务器的公网IP地址。然后再设置,如果把这个服务请求转发给内部的IP地址为192.168.0.2的WEB服务器。通过这个简单的配置,就实现基于端口的映射。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​三、改善NAT服务器的性能与便利性。</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  可见这个端口映射配置是很简单的。但是其难点在于如何提高这个NAT服务器的性能,如何让用户使用的更加方便。这是网络管理员所需要考虑的。对此笔者有如下几个建议可供大家参考。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  1、利用域名来代替<​a target="​_blank"​ href="​http://​www.vlan9.com/​ip/​a/​index.html"​ style="​color:​rgb(21,​80,​103)">​IP地址</​a>​。当用户访问企业内部的网站服务器时,如果让他们输入服务器的IP地址,对普通用户来说可能有点难度。要记住这202.96.92.100十一个没有丝毫关系的数字有很大的难度。如果能够把这个IP地址转换为有实际意义的网址那就最好了。在企业内部,可以自己部属一个DNS服务器,把IP地址转换为网址。那么内部员工可以直接通过网址来访问WEB服务器。但是在<​a target="​_blank"​ href="​http://​www.vlan9.com/​telecom/​c34/​index.html"​ style="​color:​rgb(21,​80,​103)">​互联网</​a>​上,企业可不能自己随便定义域名。为了提高用户访问的便捷性,企业网络管理员最好能够像当地的域名注册机构,申请一个合适的域名并关联到自己的NAT服务器公网IP地址。那么以后外部用户访问内部WEB服务器时,只需要输入像新浪那样的地址就可以了。而不用输入这些难以记忆的地址。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  2、如果企业可以申请到多个IP地址的话,那么最好能够分开来部署。即企业可以申请两个<​a target="​_blank"​ href="​http://​www.vlan9.com/​adsl/​index.html"​ style="​color:​rgb(21,​80,​103)">​ADSL</​a>​帐号。其中一个专门用来连接内部的服务器;​而另外一个用来内部员工跟互联网的通信。这么做主要是为了减轻NAT服务器的压力,提高其性能。如企业只有一个ADSL帐号的话,那么无论是外部用户访问企业内部服务器;​还是企业内部员工访问互联网,其数据流都要通过这台NAT服务器来处理。为此,如果企业数据流量比较大或者NAT服务器配置不怎么理想的话,那么很可能这个NAT服务器就成为了企业网络访问的瓶颈资源。如果把他们分开,则可以减轻NAT服务器的压力,提高外部用户访问企业内部服务器的效率。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​  3、配置合适的ICMP数据包策略,以加强阻挡黑客利用ICMP的攻击行为。使用ICMP攻击的原理实际上就是通过Ping大量的数据包使得NAT服务器的CPU使用率居高不下而崩溃。一般情况下黑客通常在一个时段内连续向计算机发出大量请求而导致CPU占用率太高而死机。而且会使用多台肉鸡同时向NAT服务器发送数据包。基于ICMP的攻击可以分为两大类,一是ICMP攻击导致拒绝服务;​另外一个是基于重定向的路由欺骗技术。无论是哪一类攻击,都会给NAT服务器产生致命的影响。为此为了NAT服务器的安全,最好配置合适的ICMP包策略。如可以拒绝响应别人的ICMP请求。如此攻击者就无法对NAT服务器发起ICMP攻击了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h2 style=";​padding:​0px;​line-height:​37px">​SSH隧道与端口转发及内网穿透</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多。不过其实除了这个功能,它的隧道转发功能更是吸引人。下面是个人根据自己的需求以及在网上查找的资料配合自己的实际操作所得到的一些心得。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​SSH/​plink命令的基本资料:</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先,认识下这三个非常强大的命令:</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -C -f -N -g -L listen_port:​DST_Host:​DST_port user@Tunnel_Host&​nbsp;<​br/>​ ssh -C -f -N -g -R listen_port:​DST_Host:​DST_port user@Tunnel_Host&​nbsp;<​br/>​ ssh -C -f -N -g -D listen_port user@Tunnel_Host</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​相关参数的解释:&​nbsp;<​br/>​ -f Fork into background after authentication.&​nbsp;<​br/>​ 后台认证用户/​密码,通常和-N连用,不用登录到远程主机。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-L port:​host:​hostport&​nbsp;<​br/>​ <​strong><​span style="​font-size:​18px;​color:​rgb(0,​0,​255);​line-height:​32px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口</​span></​strong>​. &​nbsp;​工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ &​nbsp;​同时远程主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. &​nbsp;​IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R port:​host:​hostport&​nbsp;<​br/>​ <​strong><​span style="​font-size:​18px;​color:​rgb(0,​0,​255);​line-height:​32px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口.&​nbsp;</​span></​strong>​工作原理是这样的,​ &​nbsp;​远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和 host &​nbsp;​的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. &​nbsp;​IPv6 地址用另一种格式说明:​ port/​host/​hostport</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-D port&​nbsp;<​br/>​ 指定一个本地机器 “动态的’’ 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, &​nbsp;​将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-C Enable compression.&​nbsp;<​br/>​ 压缩数据传输。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N Do not execute a shell or command.&​nbsp;<​br/>​ 不执行脚本或命令,通常与-f连用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-g Allow remote hosts to connect to forwarded ports.&​nbsp;<​br/>​ 在-L/​-R/​-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。注:这个参数我在实践中似乎始终不起作用。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​以上摘录自:</​em><​a target="​_blank"​ title="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ href="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ style="​color:​rgb(21,​80,​103)"><​em><​span style="​color:​rgb(244,​122,​14)">​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html</​span></​em></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​建立本地SSH隧道例子</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在我们计划建立一个本地SSH隧道之前,我们必须清楚下面这些数据:</​p><​ol style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​中间服务器d的IP地址</​p></​li><​li><​p>​要访问服务器c的IP地址</​p></​li><​li><​p>​要访问服务器c的端口</​p></​li></​ol><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,我们把上面这张图变得具体一些,给这些机器加上IP地址。并且根据下面这张图列出我们的计划:</​p><​p><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869400146519.jpg"/>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​ol style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​需要访问234.234.234.234的FTP服务,也就是端口21</​p></​li><​li><​p>​中间服务器是123.123.123.123</​p></​li></​ol><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​18px;​line-height:​32px">​现在我们使用下面这条命令来达成我们的目的(<​span style="​color:​rgb(0,​0,​255)">​192.168.0.100上执行</​span>​)</​span></​strong></​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​18px;​line-height:​32px">​1.ssh -N -f&​nbsp;&​nbsp;​ -L&​nbsp;&​nbsp;​ &​nbsp;<​strong><​span style="​color:​rgb(0,​0,​255)">​2121</​span></​strong>:<​strong><​span style="​color:​rgb(255,​0,​0)">​234.234.234.234:​21</​span></​strong>&​nbsp;&​nbsp;&​nbsp;​ 123.123.123.123</​span></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​18px;​line-height:​32px">​2.ftp://​localhost:​2121&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>#​ 现在访问本地2121端口,就能连接234.234.234.234的21端口了</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里我们用到了SSH客户端的三个参数,下面我们一一做出解释:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​-N 告诉SSH客户端,这个连接不需要执行任何命令。仅仅做端口转发</​p></​li><​li><​p>​-f 告诉SSH客户端在后台运行</​p></​li><​li><​p>​-L 做本地映射端口,被冒号分割的三个部分含义分别是</​p></​li><​ul style="​list-style-type:​ square;"​ class="​ list-paddingleft-2"><​li><​p><​span style="​color:​rgb(0,​0,​255)">​需要使用的本地端口号</​span></​p></​li><​li><​p><​span style="​color:​rgb(255,​0,​0)">​需要访问的目标机器IP地址</​span>​(IP:​ 234.234.234.234)</​p></​li><​li><​p><​span style="​color:​rgb(255,​0,​0)">​需要访问的目标机器端口</​span>​(端口:​ 21)</​p></​li></​ul><​li><​p>​最后一个参数是我们用来建立隧道的中间机器的IP地址(IP:​ 123.123.123.123)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​我们再重复一下-L参数的行为。-L X:​Y:​Z的含义是,将IP为Y的机器的Z端口通过中间服务器映射到本地机器的X端口。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在这条命令成功执行之后,我们已经具有绕过公司防火墙的能力,并且成功访问到了我们喜欢的一个FTP服务器了。</​p><​h5 style="​line-height:​21px">​如何建立远程SSH隧道</​h5><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过建立本地SSH隧道,我们成功地绕过防火墙开始下载FTP上的资源了。那么当我们在家里的时候想要察看下载进度怎么办呢?大多数公司的网络是通过路由器接入互联网的,公司内部的机器不会直接与互联网连接,也就是不能通过互联网直接访问。通过线路D-B-A访问公司里的机器a便是不可能的。也许你已经注意到了,虽然D-B-A这个方向的连接不通,但是A-B-D这个方向的连接是没有问题的。那么,我们能否利用一条已经连接好的<​strong><​span style="​font-size:​24px;​color:​rgb(255,​0,​0);​line-height:​43px">​A-B-D</​span></​strong>​方向的连接来完成D-B-A方向的访问呢?答案是肯定的,这就是远程SSH隧道的用途。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​与本地SSH一样,我们在建立远程SSH隧道之前要清楚下面几个参数:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​需要访问内部机器的远程机器的IP地址(这里是123.123.123.123)</​p></​li><​li><​p>​需要让远程机器能访问的内部机器的IP地址(这里因为是想把本机映射出去,因此IP是127.0.0.1)</​p></​li><​li><​p>​需要让远程机器能访问的内部机器的端口号(端口:​22)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​在清楚了上面的参数后,我们使用下面的命令来建立一个远程SSH隧道</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -N -f&​nbsp;<​strong><​span style="​font-size:​24px;​color:​rgb(255,​0,​0);​line-height:​43px">​-R</​span></​strong>&​nbsp;​2222:<​strong>​127.0.0.1:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​strong>​123.123.123.123</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,在IP是123.123.123.123的机器上我们用下面的命令就可以登陆公司的IP是192.168.0.100的机器了。</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -p 2222 localhost</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-N,-f 这两个参数我们已经在本地SSH隧道中介绍过了。我们现在重点说说参数-R。该参数的三个部分的含义分别是:</​p><​ul style="​margin:​5px 0px 5px 40px;​padding:​0px"​ class="​ list-paddingleft-2"><​li><​p>​远程机器使用的端口(2222)</​p></​li><​li><​p>​需要映射的内部机器的IP地址(127.0.0.1)</​p></​li><​li><​p>​需要映射的内部机器的端口(22)</​p></​li></​ul><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​例如:-R X:Y:Z 就是把我们内部的Y机器的Z端口映射到远程机器的X端口上。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​建立SSH隧道的几个技巧</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​自动重连</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​隧道可能因为某些原因断开,例如:机器重启,长时间没有数据通信而被路由器切断等等。因此我们可以用程序控制隧道的重新连接,例如一个简单的循环或者使用&​nbsp;<​a target="​_blank"​ href="​http://​cr.yp.to/​daemontools.html"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​djb’s daemontools</​span></​a>&​nbsp;​. &​nbsp;​不管用哪种方法,重连时都应避免因输入密码而卡死程序。关于如何安全的避免输入密码的方法,请参考我的&​nbsp;<​a target="​_blank"​ href="​http://​blog.jianingy.com/​node/​73"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​如何实现安全的免密码ssh登录</​span></​a>&​nbsp;​。这里请注意,如果通过其他程序控制隧道连接,应当避免将SSH客户端放到后台执行,也就是去掉-f参数。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​保持长时间连接</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​有些路由器会把长时间没有通信的连接断开。SSH客户端的TCPKeepAlive选项可以避免这个问题的发生,默认情况下它是被开启的。如果它被关闭了,可以在ssh的命令上加上-o TCPKeepAlive=yes来开启。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​另一种方法是,去掉-N参数,加入一个定期能产生输出的命令。例如:​ top或者vmstat。下面给出一个这种方法的例子:</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -R 2222:​localhost:​22 123.123.123.123 &​quot;​vmstat 30&​quot;</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​检查隧道状态</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​有些时候隧道会因为一些原因通信不畅而卡死,例如:由于传输数据量太大,被路由器带入stalled状态。这种时候,往往SSH客户端并不退出,而是卡死在那里。一种应对方法是,使用SSH客户端的ServerAliveInterval和ServerAliveCountMax选项。 &​nbsp;​ServerAliveInterval会在隧道无通信后的一段设置好的时间后发送一个请求给服务器要求服务器响应。如果服务器在 &​nbsp;​ServerAliveCountMax次请求后都没能响应,那么SSH客户端就自动断开连接并退出,将控制权交给你的监控程序。这两个选项的设置方法分别是在ssh时加入-o &​nbsp;​ServerAliveInterval=n和-o ServerAliveCountMax=m。其中n,​ m可以自行定义。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​如何将端口绑定到外部地址上</​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​使用上面的方法,映射的端口只能绑定在127.0.0.1这个接口上。也就是说,只能被本机自己访问到。如何才能让其他机器访问这个端口呢?我们可以把这个映射的端口绑定在0.0.0.0的接口上,方法是加上参数-b &​nbsp;​0.0.0.0。同时还需要打开SSH服务器端的一个选项-GatewayPorts。默认情况下它应当是被打开的。如果被关闭的话,可以在/​etc &​nbsp;/​sshd_config中修改GatewayPorts no为GatewayPorts yes来打开它。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​color:​rgb(255,​255,​255);​background-color:​rgb(47,​50,​55)">​通过SSH隧道建立SOCKS服务器</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果我们需要借助一台中间服务器访问很多资源,一个个映射显然不是高明的办法(事实上,高明确实没有用这个方法)。幸好,SSH客户端为我们提供了通过SSH隧道建立SOCKS服务器的功能。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过下面的命令我们可以建立一个通过123.123.123.123的SOCKS服务器。</​p><​blockquote style="​font-size:​1em;​margin:​auto 0px auto 35px"><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1.ssh -N -f -D 1080 123.123.123 # 将端口绑定在127.0.0.1上</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2.ssh -N -f -D 0.0.0.0:​1080 123.123.123.123 # 将端口绑定在0.0.0.0上</​p></​blockquote><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​通过SSH建立的SOCKS服务器使用的是SOCKS5协议,在为应用程序设置SOCKS代理的时候要特别注意。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​以上摘录自:</​em><​a target="​_blank"​ title="​http://​chenweiguang.blogspot.com/​2009/​03/​ssh.html"​ href="​http://​blog.jianingy.com/​content/​%E2%80%A6"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(244,​122,​14)">​http://​blog.jianingy.com/​content/​…</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​@@@@@@@@@@@@@@@@@@@@@@@@@@@</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1 &​gt;&​nbsp;​反向隧道技术</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景:节假日需要回公司加班。但是公司是内网,使用NAT,所以没办法连回去。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在公司机器(LAN_ip)上执行</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -NfR 2222:​localhost:​22 home_ip</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​-R : 建立反向连接 将 home_ip port转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在,到home_ip上面<​br/>​ ssh localhost -p 2222<​br/>​ 就跑在NAT后面的公司机器。就是后门了。灰鸽子木马用的也是反向链接。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​Destination (<​em>​LAN_ip</​em>​) &lt;- |NAT| &lt;- Source (<​em>​home_ip</​em>​)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2&​gt;​端口转发</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景:本机不允许访问www.xxx.com这个网站,但是远程主机(<​strong><​span style="​font-size:​18px;​line-height:​32px">​remote_ip</​span></​strong>​)可以。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -f -N -L 31609:​www.xxx.com:​80&​nbsp;<​em>​user@</​em><​strong><​span style="​font-size:​18px;​line-height:​32px">​remote_ip</​span></​strong></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在我们就可以在本地打开&​nbsp;​http://​localhost:​31609&​nbsp;​访问www.xxx.com了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​3&​gt;&​nbsp;​SOCKS代理</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​情景: 本机不允许访问某些网站,但是远程主机(remote_ip)可以,并且公司没有组织你连接remote_ip。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh -NfD 8888&​nbsp;<​em>​user@</​em>​remote_ip</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​现在在浏览器socks 5 proxy设置为localhost:​8888,​所有之前无法访问的网站现在都可以访问了。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​@@@@@@@@@@@@@@@@@@@</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​古人以“夜不闭户”、“路不拾遗”来形容大治之世。他们认为大治时,人们的道德水平已经很高了。而早期Internet的形成似乎也是符合这种思想的。<​strong>​Internet的初衷是相互信任和相互分享</​strong>​,它的这种“信任”建立在“大治”甚至“天下大同”的假想上。随着互联网的商业化和平民化,Internet的安全性就显得越来越脆弱了,不仅网络攻击、网络诈骗盛行,也有<​strong>​一些网络管理者肆意检测、更改、阻断用户通信的行为</​strong>​。<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​SSH</​span></​a>​的一些特性在很多场合可以解决这些问题。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的基本用法在<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​命令行也强大之ssh</​span></​a>​一文中做了最基本的介绍。ssh是安全的远程登录工具,在与远程主机进行交互时,ssh协议把原始数据进行了加密并压缩,从而保证了数据在网络传输过程的安全。利用这一原理,我们也可以用ssh把其它的明文传输的TCP数据流通过ssh的连接传输,这就是本文要讲到的ssh转发/​隧道技术。下图表示一个ssh连接(图片来自<​a target="​_blank"​ href="​http://​www.ssh.com/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​www.ssh.com</​span></​a>​)<​br/>​ <br/> <img alt="​ssh连接"​ src="​http://​www.ssh.com/​support/​documentation/​online/​ssh/​adminguide/​32/​tunnel1-1.gif"/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ssh的转发有四类:<​strong>​远程转发、本地转发、动态转发和X转发</​strong>​。把TCP流重定向到ssh连接叫做“转发”,这个ssh连接叫做“隧道”。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设场景:受限网络内主机A,受限网络防火墙F,受限网络外主机B,受限网络外主机C、D、E。用户为主机A的管理者且拥有主机B的ssh账号,B、C、D、E可以正常连接。因为防火墙F的策略,A与C、D、E完全不能连接,A与B受限连接(当A与B的连接内有关键字“SB”则阻断连接,且只能由A与B连接,不能由B主动连接,可类比为NAT)。<​br/>​ 下图为场景图(用Dia v0.96.1绘制)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​a target="​_blank"​ href="​http://​blog.lilinux.net/​wp-content/​uploads/​2010/​02/​stage.png"​ style="​color:​rgb(21,​80,​103)"><​img alt="​场景"​ src="​http://​blog.lilinux.net/​wp-content/​uploads/​2010/​02/​stage.png"/></​a></​p><​h2 style=";​padding:​0px;​line-height:​37px">​一、远程转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的,​ 远程主机上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转向出去,​ 同时本地主机和host 的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口.--摘自ssh的中文Manual<​br/>​ 命令格式为:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-R&​nbsp;::</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设主机A提供了HTTP服务,即在受限网络内部可以访问的网页服务器。但这个服务因为防火墙策略或者NAT路由的原因导致在Internet无法访问。那么我们就可以将本地的HTTP服务远程转发到Internet上的主机B</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgR&​nbsp;​80:​localhost:​80&​nbsp;​root@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后在Internet上可以通过访问 http://​host-B来访问主机A上的HTTP服务了</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​如果没有主机B的root账号,则只能远程转发到1024以后的端口号</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgR&​nbsp;​8080:​localhost:​80&​nbsp;​lige@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后在Internet上可以通过访问http://​host-B:​8080来访问主机A上的HTTP服务。<​br/>​ 值得注意的是搭建了<​a target="​_blank"​ title="​wordpress"​ href="​http://​blog.lilinux.net/​tag/​wordpress/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​wordpress</​span></​a>​的主机只能运行在80端口,如果转发后的端口不是80,那么将无法使用,这应该算是wp的bug吧</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​而且因为主机A到主机B之间是通过隧道连接的,即使网页中含有关键字“SB”,也无法被大防火墙检测到(泪流满面,终于讲到关键了)</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​附加参数简要说明:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​-N&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​不执行远程命令.&​nbsp;​用于转发端口.&​nbsp;&​nbsp;​-n&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​把&​nbsp;​stdin&​nbsp;​重定向到&​nbsp;/​dev/​null&​nbsp;​(实际上防止从&​nbsp;​stdin&​nbsp;​读取数据).&​nbsp;&​nbsp;​ssh在后台运行时一定会用到这个选项.&​nbsp;​它的常用技巧是远程运行&​nbsp;​X11&​nbsp;​程序.&​nbsp;&​nbsp;​-f&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​要求&​nbsp;​ssh&​nbsp;​在执行命令前退至后台.&​nbsp;​它用于当&​nbsp;​ssh&​nbsp;​准备询问口令或密语,​但是用户希望它在后台进行.&​nbsp;​该选项隐含了&​nbsp;​-n&​nbsp;​选项.&​nbsp;​在远端机器上启动X11&​nbsp;​程序的推荐手法就是类似于&​nbsp;​ssh&​nbsp;​-f&​nbsp;​host&​nbsp;​xterm&​nbsp;​的命令.&​nbsp;&​nbsp;​-C&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​要求进行数据压缩&​nbsp;&​nbsp;​-g&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​允许远端主机连接本地转发的端口</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​关于建立隧道和使用隧道的网络原理,见文章末尾</​p><​h2 style=";​padding:​0px;​line-height:​37px">​二、本地转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​ 该连接就经过安全通道转发出去,​ 同时远程主机和 host的 hostport 端口建立连接. &​nbsp;​可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. --摘自ssh的中文Mannul</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​它的命令格式为</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-L&​nbsp;::</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​和远程转发原理类似但方向相反。假设主机B有HTTP服务,但因为受限网络内其它主机因憎恶“大防火墙”F而不愿意直接访问主机B(这种心理我也有)。那就可以通过本地转发了</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-fNgL&​nbsp;​80:​localhost:​80&​nbsp;​root@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​因为和远程转发类似,后面就不再详述。</​p><​h2 style=";​padding:​0px;​line-height:​37px">​三、动态转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​指定一个本地机器 “动态的” 应用程序端口转发. 工作原理是这样的,​ 本地机器上分配了一个 socket 侦听 port 端口, &​nbsp;​一旦这个端口上有了连接,​该连接就经过安全通道转发出去,​ 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS 协议, ssh &​nbsp;​将充当 SOCKS 服务器.只有 root 才能转发特权端口. --摘自ssh的中文manual</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​可以这样理解,远程转发和本地转发都是在隧道上传输一个服务,比如例子中的HTTP数据流。而动态转发则是为了在隧道上传输多种不确定的网络服务。它的表现就把远端ssh服务器当作了一个安全的代理服务器。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​命令格式为</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-D</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设主机A想要访问受限网络所限制访问的主机C、D、E等,或者想要访问主机B上的“SB”内容。则可有D选项建立动态隧道</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​-CfNg&​nbsp;​-D&​nbsp;​8888&​nbsp;​lige@host-B</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后你就有了一个安全的socks 5代理。这个代理ip是127.0.0.1,端口为刚才指定的8888。如何使用socks 5代理在本文就不再讲了。大防火墙可能还存在一些技术来防止socks 5代理,可以参考<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​socks%e4%bb%a3%e7%90%86%e7%9a%84%e8%bf%9c%e7%ab%afdns%e8%a7%a3%e6%9e%90/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​socks代理的远端DNS解析</​span></​a>​一文</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​还有一个plink工具,包含在PuTTY软件包,可以在windows和<​a target="​_blank"​ title="​linux"​ href="​http://​blog.lilinux.net/​tag/​linux/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​linux</​span></​a>​下使用,它的格式为:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​CMD</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​PLINK.EXE&​nbsp;​-C&​nbsp;​-D&​nbsp;​127.0.0.1:​7777&​nbsp;​-N&​nbsp;​-pw&​nbsp;​SSH密码&​nbsp;​SSH用户名@SSH的IP地址</​pre>​ </​td></​tr></​tbody></​table><​h2 style=";​padding:​0px;​line-height:​37px">​四、X转发</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​X转发是一个令人惊奇但实际上用处不大的功能。因为我所能管理的远端主机都是无图形界面的,所以本部分内容我不保证正确。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​大家都知道GUI软件与X之间也是通过TCP连接的,所以X转发与其它转发也没有本质区别,甚至它简单得都不应该和其它几种转发相提并论。X转发用的选项为”-X”</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​首先要在客户端指定X Server的位置</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​export&​nbsp;​DISPLAY=:​.</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​然后执行</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">&​nbsp;​ssh&​nbsp;​-X&​nbsp;​lige@hot-B</​pre>​ </​td></​tr></​tbody></​table><​h2 style=";​padding:​0px;​line-height:​37px">​补充:可能的问题:</​h2><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​1. ssh的基本用法<​br/>​ <a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​command-ssh/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​blog.lilinux.net/​2010/​02/​command-ssh/</​span></​a>​以及ssh的man文档</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​2. windows下的ssh客户端<​br/>​ PuTTY和其中的plink</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​3. 如何查看隧道是否建立完善<​br/>​ 查看进程:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ps&​nbsp;​aux&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​查看端口:</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​netstat&​nbsp;​-ntl</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​4. 因为F,连接还是不稳定,隧道经常无法使用<​br/>​ 去除-N,​-n,​-f选项,进入远程shell,<​strong>​输入一些需要频繁交互数据的命令,使连接一直处于活动状态</​strong>​。比如</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​top</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​我更推荐用这样的命令,对系统的负载消耗更小(而且更加美观)</​p><​p><​sup style="​line-height:​21px"><​a target="​_blank"​ title="​WP-CodeBox HowTo?"​ href="​http://​www.ericbess.com/​ericblog/​2008/​03/​03/​wp-codebox/#​examples"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(153,​204,​0)">?</​span></​a></​sup><​span style="​color:​rgb(0,​0,​0)">​[Copy &​nbsp;​to clipboard]</​span><​span style="​color:​rgb(0,​0,​0)">​View Code</​span>&​nbsp;​BASH</​p><​table><​tbody><​tr class="​firstRow"><​td><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​watch&​nbsp;​-n1&​nbsp;&​quot;​date&​quot;</​pre>​ </​td></​tr></​tbody></​table><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​5. 更多的实例</​strong><​br/>​ 本文写的例子都是假设场景,近期将写一个实例应用。预计网址为<​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​ssh-instance/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)">​http://​blog.lilinux.net/​2010/​02/​ssh-instance/</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ </​p><​h1 style=";​padding:​0px;​line-height:​50px"><​strong>​附ssh隧道原理</​strong></​h1><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em>​对网络原理不太懂或不感兴趣的可以忽略此部分<​br/>​ </​em><​br/>​ 总的来说:Tunnel实际上是一个TCP连接,这个连接和普通的SSH登录的连接并没有本质上的区别</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​假设有两台主机A和B,IP分别是IP_A和IP_B,是要把主机A的22端口映射到主机B的10022端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​连接的建立过程是这样的:</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​1. 建立TUNNEL (在A主机上执行命令)<​br/>​ </​strong>​ssh命令,把A的端口映射到B主机(ssh -NfR 10022:​localhost:​22 USER_B@IP_B)<​br/>​ 这个步骤实际上是这样的:<​br/>​ (1)ssh命令是ssh客户端,这条命令是指在A主机上登录B主机,并且A主机请求将A主机的22号端口映射到B主机的10022端口。<​br/>​ (2)B主机接受远程登录请求后,检测发现用户的权限足以创建10022端口,且10022端口并未使用。则B主机开启一个监听端口:10022<​br/>​ (3)这条命令成功结束后,A主机有了一个后台进程登录到B主机,这个登录的连接正是“TUNNEL”。<​br/>​ 而B主机相应也有这么一个后台进程,此外B主机还增开了一个监听端口:10022<​br/>​ (4)在这个过程中,B是Server,A是Client。那么A是以一个任意端口连接到B的ssh监听端口。所设这个连接是<​br/>​ IP_A:12345 —— IP_B:​22</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​2. 连接映射端口 (在B主机上执行命令)<​br/>​ </​strong>​连接映射端口 ( ssh USER_A@localhost -p 10022)<​br/>​ 这个步骤实际上是这样的:<​br/>​ (1)主机B连接自己的一个监听端口10022<​br/>​ 于是主机B以一个任意的端口连接10022: IP_B:23456 ——- IP_B:​10022<​br/>​ (2)10022端口实际是由主机B的sshd来维护的,sshd发现有连接到10022的端口的数据。<​br/>​ 将数据封装成ssh包,通过已经建立好的TUNNEL传输到对端主机A的12345端口(TUNNEL建立时的初始端口)<​br/>​ (3)主机A的12345端口收到数据后,经过TUNNEL的建立进程的处理(解开ssh包),把数据转到主机A的端口22(建立TUNNEL时指定的端口)<​br/>​ (4)因此,这个反向的连接就建立起来了</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​3. TUNNEL与TCP连接的关系<​br/>​ </​strong>​(1)TUNNEL本质是一个TCP连接<​br/>​ (2)TUNNEL的作用用于传输其它TCP连接的数据<​br/>​ (3)对用户而言,TUNNEL就是它们可见的TCP连接传输的一个“隧道”<​br/>​ </​p><​p><​em><​strong><​img alt="​Chapter 8 Secure Network (ssh端口映射) - linux-ul - unix-EA"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869405568124.png"/></​strong></​em>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​em><​strong>&​nbsp;​如需转载,请注明出处:</​strong></​em><​a target="​_blank"​ href="​http://​blog.lilinux.net/​2010/​02/​ssh-tunnel/"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(0,​102,​255)"><​em><​strong>​http://​blog.lilinux.net/​2010/​02/​ssh-tunnel/</​strong></​em></​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​题目有点长<​br/>​ 其实来自于一个朋友的问题<​br/>​ 他的oracle跑在linux服务器上<​br/>​ 远程的linux服务器并没有跑在图形界面下(runlevel=5)<​br/>​ 而是跑在字符模式下(runlevel=3)<​br/>​ 而他又想用图形界面来管理oracle<​br/>​ 我翻了翻资料<​br/>​ 找出一种方法<​br/>​ <span style="​color:​rgb(0,​0,​255)"><​strong>​在一台图形界面的linux下(虚拟机也行,其实不是linux也行,关键是本地得有X server)起一个控制台<​br/>​ 用命令行ssh加参数”-X”连上远程的linux服务器<​br/>​ 像这样<​br/>​ ssh -X oracle@1.1.1.1<​br/>​ 这里”1.1.1.1″是远程linux服务器的ip地址<​br/>​ 这样连上去之后<​br/>​ 直接就可以用以前只有在图形界面上才能用的命令<​br/>​ 如oracle管理的图形程序”dbca”</​strong></​span></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这个原理很简单<​br/>​ 因为远程的sshd支持x11 forward<​br/>​ 那么他起x程序的时候<​br/>​ 就会把x的包通过ssh转回来到ssh客户端的机器上来<​br/>​ 而ssh客户端这边跑得有x server<​br/>​ 所以就能正确接收x包并显示出来</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​其实在windows下也可以通过安装x server软件来实现一样的效果<​br/>​ 比如x manager就可以<​br/>​ 具体用法不在详述</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​这里的关键在于<​br/>​ 远程的sshd需要支持x11 forward<​br/>​ 不过不要担心<​br/>​ 一般情况下<​br/>​ 缺省是支持的</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">&​nbsp;</​p><​h1 style=";​padding:​0px;​line-height:​50px">​SSH端口转发(本地端口转发、远程端口转发、动态端口转发)工作原理、应用详解</​h1><​p>​Posted on&​nbsp;<​a target="​_blank"​ title="​3:​19 pm" href="​http://​lesca.me/​blog/?​p=514"​ style="​color:​rgb(21,​80,​103)"><​span style="​color:​rgb(30,​144,​255)">​March 1, 2011</​span></​a>&​nbsp;​by&​nbsp;<​a target="​_blank"​ title="​View all posts by lesca" href="​http://​lesca.me/​blog/?​author=1"​ style="​color:​rgb(21,​80,​103)">​lesca</​a>​ &​nbsp;</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​strong>​实验条件:</​strong><​br/>​ 1.本文所述之实验,本地主机操作系统基于Ubuntu 10.04,远程主机操作系统基于BSD发行版<​br/>​ 2.本地主机安装了ssh client(ssh),也安装了ssh server(sshd)<​br/>​ <​strong>​基础知识:</​strong><​br/>​ 文章中客户端指ssh,用于发出连接请求;服务端指sshd,监听端口22。<​br/>​ 安全通道是指ssh与sshd之间已建立的连接通道,该连接是加密的。<​br/>​ <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​SSH 端口转发格式<​sup style="​line-height:​27px">​[1]</​sup></​span></​p><​p style=";​margin-bottom:​10px;​margin-left:​4em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(0,​119,​0)">​[-L [bind_address:​]port:​host:​hostport]<​br/>​ [-R [bind_address:​]port:​host:​hostport]<​br/>​ [-D [bind_address:​]port]</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​动态端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-D &​lt;​port&​gt;</​span><​br/>​ 这会使本地主机监听端口<​strong>​port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道发送到远程主机。远程主机的sshd会根据数据包的应用层协议(如HTTP)自动建立对应的连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-D&​nbsp;​7070&​nbsp;​sisca@216.194.70.6</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​127.0.0.1:​7070&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​5205/​ssh&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;::​1:​7070&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​5205/​ssh</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​(什么都没有)</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:我们发现,动态端口转发的时候确实是本地主机的ssh在监听端口7070的<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-L &​lt;​local_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;​ &​lt;​ssh_server&​gt;</​span><​br/>​ 这会使本地主机监听端口<​strong>​local_port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道,同时<​strong>​远程主机</​strong>​会与主机<​strong>​host</​strong>​的端口<​strong>​host_port</​strong>​建立连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-L&​nbsp;​7001:​localhost:​7070&​nbsp;​sisca@216.194.70.6</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​127.0.0.1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​3475/​ssh&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;::​1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​3475/​ssh</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​(什么都没有)</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:我们从中可以看出,本地端口转发的时候确实是本地主机的ssh在监听端口7001<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​远程端口转发</​span><​br/>​ <span style="​color:​blue;​margin-left:​4em">​-R &​lt;​remote_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;​ &​lt;​ssh_server&​gt;</​span><​br/>​ 这会使<​strong>​ssh_server</​strong>​(远程端)监听端口<​strong>​remote_port</​strong>​,一旦与该端口建立连接,经过该端口的数据会被转发到安全通道,并且<​strong>​本地主机</​strong>​会与主机<​strong>​host</​strong>​的端口<​strong>​host_port</​strong>​建立连接。</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-R&​nbsp;​7001:​localhost:​7070&​nbsp;​sisca@216.194.70.6</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆前本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后本地主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sudo&​nbsp;​netstat&​nbsp;​-lnput&​nbsp;​|&​nbsp;​grep&​nbsp;​ssh&​nbsp;&​nbsp;​tcp&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;​0.0.0.0:​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0.0.0.0:​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd&​nbsp;&​nbsp;​tcp6&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​0&​nbsp;:::​22&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;:::​*&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​LISTEN&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​927/​sshd</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​登陆后远程主机端口监听状态:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​sockstat&​nbsp;​-4l&​nbsp;&​nbsp;​USER&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​COMMAND&​nbsp;&​nbsp;&​nbsp;&​nbsp;​PID&​nbsp;&​nbsp;&​nbsp;​FD&​nbsp;​PROTO&​nbsp;&​nbsp;​LOCAL&​nbsp;​ADDRESS&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​FOREIGN&​nbsp;​ADDRESS&​nbsp;&​nbsp;​sisca&​nbsp;&​nbsp;&​nbsp;&​nbsp;​sshd&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​66196&​nbsp;​7&​nbsp;&​nbsp;​tcp4&​nbsp;&​nbsp;&​nbsp;​127.0.0.1:​7001&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​*:​*</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小结:很明显,使用远程端口转发时,本地主机的端口监听并没有发生变化,相反远程主机却开始监听我们指定的7001端口</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​小提示:<​br/>​ 这里的<​em>​remote_port</​em>,<​em>​local_port</​em>​可以归并为<​strong>​listen_port</​strong>​,因此我们得到这样的助记格式:</​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​ssh&​nbsp;​[-L|-R]&​nbsp;&​lt;​listen_port&​gt;:&​lt;​host&​gt;:&​lt;​host_port&​gt;</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/>​ <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地端口转发应用:<​sup style="​line-height:​27px">​[2]</​sup></​span><​br/>​ <span style="​color:​rgb(0,​0,​51)"><​strong>​背景:</​strong>​在实验室里有一台 LDAP 服务器(LDAP Server),但是限制了只有本机上部署的应用才能直接连接此 LDAP 服务器。现在我们想临时从本地机器(LDAP Client)直接连接到这个 LDAP 服务器,只需要在<​strong>​LDAP Client</​strong>​上运用本地端口转发:</​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-L&​nbsp;​7001:​localhost:​389&​nbsp;​LdapServerHost</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​图1 本地端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd01.jpg"​ width="​300"​ height="​208"/><​br/>​ <​strong>​数据流向:</​strong><​br/>​ 该命令首先使本地主机(LDAP Client)与远程主机(LDAP Server)建立一个SSH安全连接(安全通道)<​br/>​ 然后本地主机会监听端口7001(注意是<​span style="​color:​blue">​ssh</​span>​在监听)<​br/>​ 一旦本地LDAP客户端程序将数据发送到端口7001(即建立连接),远程主机上的<​span style="​color:​blue">​sshd</​span>​将主动与端口389(LDAP服务的端口)建立连接<​br/>​ 从而来自LDAP客户端的请求转发到该端口389上(当然是通过安全通道)<​br/>​ <​strong>​注意:</​strong><​br/>​ 本地主机有两个客户端:<​br/>​ 1) ssh – 用于与远程主机建立ssh连接<​br/>​ 2) LDAP客户端 – 用于与远程主机的LDAP服务程序进行通讯<​br/>​ 远程主机对应地有两个服务进程:<​br/>​ 1) sshd – 监听端口22<​br/>​ 2) ldapd -监听端口389<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​远程端口转发应用:<​sup style="​line-height:​27px">​[2]</​sup></​span><​br/>​ <span style="​color:​rgb(0,​0,​51)"><​strong>​背景:</​strong>​这次假设由于网络或防火墙的原因我们不能用 SSH 直接从 LDAP Client 连接到 LDAP 服务器(LDAP Server),但是反向连接却是被允许的。那此时我们的选择自然就是远程端口转发了。我们这次在<​strong>​LDAP Server</​strong>​上运用远程端口转发,因此LDAP Client必须安装了sshd</​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-R&​nbsp;​7001:​localhost:​389&​nbsp;​LdapClientHost</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​图2 远程端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd02.jpg"​ width="​300"​ height="​201"/><​br/>​ <​strong>​数据流向:</​strong><​br/>​ 该命令首先使LDAP Server与 LDAP Client 建立一个SSH安全连接(安全通道)<​br/>​ 然后LDAP Client开始监听端口7001(注意是<​span style="​color:​blue">​sshd</​span>​在监听)<​br/>​ 一旦LDAP Client将数据发送到端口7001(即建立连接),LDAP Server上的<​span style="​color:​blue">​ssh</​span>​将主动与端口389(LDAP服务端口)建立连接<​br/>​ 从而来自LDAP客户端的请求转发到该端口389上(当然是通过安全通道)<​br/>​ <br/> <br/> <span style="​line-height:​32px;​color:​blue;​font-size:​18px">​本地转发与远程转发的对比与分析</​span><​br/>​ 本地端口转发:ssh客户端发起ssh连接,并且监听指定端口<​br/>​ 远程端口转发:ssh客户端发起ssh连接,但是<​strong>​由远程主机上的sshd监听指定端口<​br/>​ <br/> <br/> 多主机转发应用<​sup style="​line-height:​27px">​[2]</​sup></​strong><​br/>​ <span style="​color:​rgb(0,​0,​51)">​图3 多主机端口转发<​br/>​ <img alt=""​ src="​http://​lesca.me/​blog/​wp-content/​uploads/​portfrd03.jpg"​ width="​300"​ height="​212"/></​span></​p><​pre style="​white-space:​pre-wrap;​word-wrap:​break-word">​$&​nbsp;​ssh&​nbsp;​-g&​nbsp;​-L&​nbsp;​7001:&​lt;​B&​gt;:​389&​nbsp;&​lt;​D&​gt;</​pre><​p><br/></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px">​注意:我们在命令中指定了“ -g ”参数以保证机器(A)能够使用机器(C)建立的本地端口转发。</​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​span style="​font-size:​13px;​color:​rgb(102,​102,​102);​line-height:​23px">​References:<​br/>​ [1] man 1 ssh<​br/>​ [2] IBM DeveloperWorks –&​nbsp;</​span><​a target="​_blank"​ href="​https://​www.ibm.com/​developerworks/​cn/​linux/​l-cn-sshforward/"​ style="​color:​rgb(21,​80,​103)"><​span style="​font-size:​13px;​color:​rgb(30,​144,​255);​line-height:​23px">​实战 &​nbsp;​SSH 端口转发</​span></​a></​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p><​span style="​color:​rgb(0,​136,​0)">​Please support us if you feel this post is useful.<​br/>​ <br/> </​span>​ <span style="​color:​rgb(0,​136,​0)"><​br/>​ </​span>​ <span style="​color:​rgb(0,​136,​0)"></​span>​ </​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​名为</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;​secure &​nbsp;​shell,&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​是专门用来进行安全远程访问的协议。它无论从功能上、系统兼容性上、还是从安全上和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Telnet</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​RSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​相比都是足以取而代之。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial;​text-indent:​48px"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​在这个</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​ARP</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​欺骗横行,网上盗窃泛滥的年代,还有什么比安全更为重要的?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial;​text-indent:​48px"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​在这个遍地都是防火墙的年代,还有什么比通过一个端口就能访问所有业务更为重要的呢?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ &​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​在矛和盾同样发达的年代,</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可以给我们什么样的方便?</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​现代的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UNIX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​类操作系统,无一例外地将</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,作为默认系统软件包进行安装。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​最</​span><​span style="​line-height:​1.5;​font-family:​宋体">​基本的连接方式:<​span style="​line-height:​1.5">​SSH &​nbsp;​Hostname</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​提供了多种不同的接入认证方式:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ Password</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ Hostkey</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ .....</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ .....</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​其中常用的有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;​password</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,</​span><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​host &​nbsp;​public key</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,</​span><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​如果使用</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​password</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​,则会产生用户交互的密码输入,如果使用<​span style="​line-height:​1.5">​host &​nbsp;​key</​span>​, 则可以直接登录或直接执行远程命令。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ SSH Client&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Server</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的交互,能实现很多其他非常有用的功能:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​最基本的功能有如下选项:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​会话内容的压缩,可以使用<​span style="​line-height:​1.5">​gzip</​span>​压缩级别进行指定,以提高在慢速线路上数据传输效率。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​本地端口转发,可以将本地某个端口和远程<​span style="​line-height:​1.5">​SSH</​span>​服务器上、或<​span style="​line-height:​1.5">​SSH</​span>​远程服务器可达的地址端口建立一种映射关系,使对本地端口的访问请求会透明地转发到远程服务器上。从远程服务器上所看到的来访请求源地址将是<​span style="​line-height:​1.5">​SSH</​span>​服务器的地址。</​span></​span></​p><​p style=";​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px"></​span></​p><​p><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px"><​img alt="​Secure Network (ssh端口映射)-----图详细可以使用 - zhuzhu - 五事九思 ​ (大连Linux主机维护)"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869407643601.gif"/></​span>​ <span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​28px">&​nbsp;</​span>​ </​p><​p style=";​margin-bottom:​10px;​padding-top:​0px;​padding-bottom:​0px"><​br/></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5"></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​远程端口反向转发,与上列相反, 远程端口反向转发建立后,远程客户机访问远程<​span style="​line-height:​1.5">​SSH</​span>​服务器的某端口,<​span style="​line-height:​1.5">​SSH</​span>​服务器将把来访请求通过<​span style="​line-height:​1.5">​SSH</​span>​安全通道转发到<​span style="​line-height:​1.5">​SSH</​span>​客户端或<​span style="​line-height:​1.5">​SSH</​span>​客户端可达的服务器地址端口上,从而使远程客户机,可以正常访问内部服务器所提供的服务。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"></​span>&​nbsp;</​p><​p><​span style="​font-family:​宋体;​background-color:​rgb(245,​247,​248);​font-size:​16px"><​img alt="​Secure Network (ssh端口映射)-----图详细可以使用 - zhuzhu - 五事九思 ​ (大连Linux主机维护)"​ src="/​dokuwiki/​ueditor/​php/​upload/​image/​20180613/​1528869407359564.gif"/></​span><​span style="​color:​rgb(0,​136,​0)"></​span>​ </​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">​这样,SSH客户端D,就可以通过访问SSH服务器B的端口D,从而达到访问业务服务器C上端口D的目的。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​高级功能<​span style="​line-height:​1.5">/</​span>​用法有:</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​端口转发的接续。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">​由于现代的<​span style="​line-height:​1.5">​UNIX</​span>​系统都默认带有<​span style="​line-height:​1.5">​SSH</​span>​服务和客户端,所以当网络变得比较复杂、层次很多的时候,使用多台机器的端口转发接续,可以将在层次很深的内网服务,在不改变当前网络结构和防火墙配置的情况下,层层转发出来从而可以使外网用户很方便地使用该内网服务,而无须对现有网络构架和甚至防火墙配置作任何变动。而对最外层的防火墙只需要开放一个<​span style="​line-height:​1.5">​22</​span>​端口(默认<​span style="​line-height:​1.5">​ssh</​span>​服务端口)就能使授权的客户端访问全网业务。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​动态端口转发。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSHv2</​span><​span style="​line-height:​1.5;​font-family:​宋体">​,同时还支持内置的<​span style="​line-height:​1.5">​SockS4 &​nbsp;​Server</​span>​,将<​span style="​line-height:​1.5">​ssh&​nbsp;</​span>​客户端的指定端口,作为一个远程<​span style="​line-height:​1.5">​SOCKS4 Server</​span>​的端口。对<​span style="​line-height:​1.5">​SockS4Server</​span>​的访问请求将通过,<​span style="​line-height:​1.5">​SSH &​nbsp;​Protocol&​nbsp;</​span>​传输到&​nbsp;<​span style="​line-height:​1.5">​SSH&​nbsp;</​span>​服务器,&​nbsp;<​span style="​line-height:​1.5">​SSH</​span>​服务器将请求转发到用户应用的真正目的服务器。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH&​nbsp;</​span><​span style="​line-height:​1.5;​font-family:​宋体">​的&​nbsp;<​span style="​line-height:​1.5">​PPP</​span>​通道。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​结合<​span style="​line-height:​1.5">​UNIX PPPD</​span>​,还可以在<​span style="​line-height:​1.5">​SSH</​span>​的<​span style="​line-height:​1.5">&​nbsp;​PTS&​nbsp;</​span>​通道上建立<​span style="​line-height:​1.5">​PPP &​nbsp;​LINK</​span>​从而非常灵活的实现&​nbsp;<​span style="​line-height:​1.5">​IP over SSH&​nbsp;</​span>​的<​span style="​line-height:​1.5">​VPN</​span>​。这种方法,可谓<​span style="​line-height:​1.5">​SSH</​span>​的终极用法,因为它解决了所有由于地址翻译而带来的应用层协议不兼容的问题(多播问题是否能解决尚未测试)。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SSH</​span><​span style="​line-height:​1.5;​font-family:​宋体">​产品实战。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​SecureCRT</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VanDyke &​nbsp;​Software&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的经典力作,几乎囊括了所有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端的功能,在此,</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的标准功能就不多说了。说一些</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​windows</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​上的特色功能使</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​变得非常灵活。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​1、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​它拥有端口转发安全过虑器,可以灵活地允许或禁止其他客户机访问被</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​所转发的本机端口。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​2、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​同时还拥有</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VBSCript/​Jscript/​Perl&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​脚本功能,可以通过这三类脚本语言和远程服务器进行自动交互式执行。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​3、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可以自动和服务器上的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Zmodem</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​进行交互,实现基于</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​TTY/​PTS</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​Zmodem&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​文件传输(这个功能可在任何类型的会话中进行,并不局限于</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​类型的会话)。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​4、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​自动和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureFX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​匹配实现</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SFTP</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的文件传输功能(无需输入用户名密码,因为是基于已建立的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH &​nbsp;​Session</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的,所以登录速度是非常快的)由于SecureFx带有拖放功能,和类似文件夹的拷贝粘贴功能,所以会给文件传输带来很大方便性(FTP还是很不方便的)。</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 72px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​5、</​span></​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​支持命令行方式,可在</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​CMD</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​中实现一步登录并将端口转发作为参数输入(和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UNIX</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​基本没区别了)。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">​功能太多了,就不一一列举了。详细请查看</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VanDyke Software</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的主页和</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的帮助文件。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5">&​nbsp;</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​UniTTY</​span></​span></​span></​p><​p style="​line-height:​1.5;​margin:​0 0 0 48px;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;</​span>​A Pure Java Based SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端。居然实现了几乎所有的</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​功能。而且更牛的是,实现了端口转发的动态加载。而且默认实现了</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SFTP &​nbsp;​over SSH</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的文件传输。只是字体有待改进。看惯了</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​SecureCRT</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的优雅美观的字体,再用</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​UniTTY</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​可能就有些不习惯了。它的多协议支持能力也很特别,居然将</​span><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">​VNC</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​的客户端协议也加进来了。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​UNIX SSH&​nbsp;</​span></​span><​span style="​line-height:​1.5;​font-family:​宋体">​客户端</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​完全基于OpenSSH,最标准的客户端。能完成所有SSH协议支持的功能,同时结合UNIX的脚本,能完成其他操作系统无法完成的很多任务。或配合完成前面所提到的一些用法!</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;</​span>​NetSarang XSHELL</​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​line-height:​1.5"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ 韩国人写的多协议客户端,和他另外一个在国内很流行的产品(Xmanager)配合使用很好。默认支持TAB窗口的SFTP,使用也比较方便,也同时支持双向的端口转发。</​span></​span></​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5"></​span></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ MindTerm</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​ 纯java的ssh客户端,非常著名的开源软件mindterm。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​mindterm,居然提供了源代码真是不错,以后可以将expect也扔了。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px"><​span style="​font-family:&#​39;​Times New Roman&#​39;;​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5">&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;&​nbsp;​而且该有的都有了(sockserver,​ &​nbsp;​端口转发,反向转发,scp,telnet,zlib compression,keepalive等),还额外提供了ftp &​nbsp;​协议桥接sftp的功能,就是有一点不方便,在哑终端方式启动,需要X11Display支持。</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"></​span>&​nbsp;</​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体">&​nbsp;&​nbsp;&​nbsp;​ zenith 2007.9.10</​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​br/>​ </​span></​span></​p><​p style="​line-height:​1.5;​padding-top:​5px;​padding-bottom:​0px;​font-family:​宋体,​Arial"><​span style="​font-size:​16px;​line-height:​1.5"><​span style="​line-height:​1.5;​font-family:​宋体"><​br/>​ </​span></​span></​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​24px;​color:​rgb(0,​0,​255);​line-height:​43px">​网的朋友苦于没有合法 ip,​不能对外提供 internet 服务。解决方案很多,可以通过在网关做端口映射,或其他的辅助软件等。</​span></​strong></​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​strong><​span style="​font-size:​24px;​color:​rgb(0,​0,​255);​line-height:​43px">​本文介绍两种比较简单实用的方法,利用 ssh 这个强大的工具。<​br/>​ (以下方法不分平台,都适用)</​span></​strong></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​案例一、<​br/>​ 内网主机 A ,开了 http,ftp ,http ,​vnc,​sshd,​socks5,​cvs 等服务。无合法 ip 地址。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px"><​br/>​ 外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px"><​br/>​ 我们的目的是让 B 能访问 A 上的各种服务。</​p><​p style=";​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px"><​span style="​line-height:​27px;​color:​rgb(59,​59,​59);​font-size:​15px">​步骤:</​span><​br/>​ <​strong><​span style="​font-size:​18px;​line-height:​32px"><​span style="​color:​rgb(59,​59,​59)">​1、</​span><​span style="​color:​rgb(255,​0,​0)">​A</​span><​span style="​color:​rgb(59,​59,​59)">​(内网服务器) &​nbsp;​知道&​nbsp;</​span><​span style="​color:​rgb(0,​0,​255)">​B</​span><​span style="​color:​rgb(59,​59,​59)">&​nbsp;​ip &​nbsp;​后,先用 ssh client 连上 B,命令如下:</​span><​br/>​ <span style="​color:​rgb(59,​59,​59)">​ssh -R 1234:</​span><​span style="​color:​rgb(255,​0,​0)">​localhost:​21&​nbsp;</​span><​span style="​color:​rgb(59,​59,​59)">&​nbsp;​ &nbsp; &​nbsp;​-l root &​nbsp;</​span><​span style="​color:​rgb(0,​0,​255)">​218.xxx.xxx.xxx</​span></​span></​strong></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​解释:<​br/>​ 关于 ssh 的参数,请看 ssh --help<​br/>​ -L listen-port:​host:​port Forward local port to remote address<​br/>​ -R listen-port:​host:​port Forward remote port to local address<​br/>​ -L local (本地) -R :remote (远程)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​-R 1234:​localhost:​21 其实做了个“端口转发(forward)&​quot;​。<​br/>​ 意思是主机 A 把本地的 21端口(对应ftp服务)映射为 B 的1234 端口(任意未被占用),同时 A 监听 B 的1234 端口。<​br/>​ 在 B 上用 netstat -al | grep 1234 ,你能看到这个监听连接。<​br/>​ 任何发送到 B 1234 端口的请求将被传送到 A的 21 端口。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​2、B 用 ftp 工具(任意,​如gftp) 连本地的 1234 端口,输入 A 的 ftp 用户和密码。<​br/>​ ftp localhost 1234</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​千万不要觉的奇怪,为什么连的是本地的地址。<​br/>​ 举个不恰当例子,相当于 A 在 B 的房间里装了个***(监听端口),那么 B 在房间里说的话就通过***传送到了 A。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​3、推广:<​br/>​ 如果 B 没占用 21 端口的话,那么可以写成:<​br/>​ A使用: ssh -R 21:​localhost:​21 -l root 218.xxx.xxx.xxx<​br/>​ B使用: ftp localhost</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​如果你想使用 A 上的 http 或其他服务,只需改变服务端口:<​br/>​ http服务 :<​br/>​ A使用:ssh -R 1234:​localhost:​80 -l root 218.xxx.xxx.xxx<​br/>​ B使用:w3m&​nbsp;<​a target="​_blank"​ href="​http://​localhost:​1234/"​ style="​color:​rgb(0,​113,​179)">​http://​localhost:​1234</​a></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​sshd服务:<​br/>​ A使用:ssh -R 1234:​localhost:​22 -l root 218.xxx.xxx.xxx<​br/>​ B使用:ssh localhost -p 1234</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​vnc 服务:&​nbsp;<​br/>​ A使用:ssh -R 1234:​localhost:​5901(其他) -l root 218.xxx.xxx.xxx<​br/>​ B使用:vncviewer localhost:​1</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​socks5服务:<​br/>​ A使用:ssh -R 1234:​localhost:​1080 -l root 218.xxx.xxx.xxx<​br/>​ B 略</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​cvs 服务:<​br/>​ A使用:ssh -R 1234:​localhost:​2401 -l root 218.xxx.xxx.xxx<​br/>​ B使用:cvs -d :​pserver:<​a target="​_blank"​ href="​mailto:​root@localhost"​ style="​color:​rgb(0,​113,​179)">​root@localhost</​a>:​1234/​home/​cvsroot login</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​这里是否一定要用 root ,​涉及到权限问题,具体还得靠大家来总结经验。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​案例二、<​br/>​ 部分朋友会问了,这样的话只是两台机器的互相通讯,如何让广域网的人都能访问呢?<​br/>​ 聪明的你,这时候可能已经有了答案。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​内网主机 A ,开了 http,ftp ,http ,​vnc,​sshd,​socks5,​cvs等服务。无合法 ip 地址。<​br/>​ 外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx<​br/>​ 我们的目的是让 internet 上的任何主机能访问 A 上的各种服务。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​步骤:<​br/>​ 1、首先,B 的sshd 服务端做点小小的设置:<​br/>​ vi /​etc/​ssh/​sshd.config<​br/>​ 加入 GatewayPorts yes<​br/>​ 然后重启 sshd 服务: /etc /init.d/ssh restart 或 /​etc/​init.d/​sshd restart<​br/>​ (解释:<​br/>​ 不加,默认会把监听端口绑定在 localhost 或 lo(127.0.0.1),这样除了 B自身别人是没法访问监听端口的。<​br/>​ 加入 GatewayPorts yes,把监听端口绑定到 0.0.0.0 ,​这样外部的所有机器都能访问到这个监听端口。<​br/>​ 主要是考虑安全性问题,默认情况,只允许本地访问。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​这里才是真正的难点,实验了一个晚上,累人呀!给点鼓励吧 :)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​2、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:<​br/>​ ssh -R 21:​localhost:​21 -l root 218.xxx.xxx.xxx<​br/>​ (事先确定 B 的21 端口未被占用)</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​3、分布在 internet 的其它客户机使用 ftp 工具(任意),连 B 21端口。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​ftp 218.xxx.xxx.xxx 21</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​你会发现自己连上了内网 A 的ftp 服务。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​此法和案例一完全一样。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​internet ---------&​gt;&​gt;​ B 21 端口-----------&​gt;&​gt;​A 21端口<​br/>​ 可以叫做端口转发,或隧道技术,也可以称之为跳板(B),或反* 。呵呵,我瞎说的。。。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​可能遇到的问题:</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​Country:/​etc#​ ssh localhost -p 1234<​br/>​ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<​br/>​ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @<​br/>​ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<​br/>​ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!<​br/>​ Someone could be eavesdropping on you right now (man-in-the-middle attack)!<​br/>​ It is also possible that the RSA host key has just been changed.<​br/>​ The fingerprint for the RSA key sent by the remote host is<​br/>​ be:​5f:​d2:​45:​66:​4d:​0c:​9e:​2b:​6b:​45:​65:​a7:​b2:​85:​28.<​br/>​ Please contact your system administrator.<​br/>​ Add correct host key in /​root/​.ssh/​known_hosts to get rid of this message.<​br/>​ Offending key in /​root/​.ssh/​known_hosts:​11<​br/>​ RSA host key for localhost has changed and you have requested strict checking.<​br/>​ Host key verification failed.<​br/>​ Country:/​etc#​ ssh localhost -p 1234<​br/>​ <a target="​_blank"​ href="​mailto:​root@localhost'​s"​ style="​color:​rgb(0,​113,​179)">​root@localhost&#​39;​s</​a>&​nbsp;​password:<​br/>​ Last login: Mon May 5 02:39:53 2003 from localhost<​br/>​ localhost root #</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​如上问题,请删除 ~/​.ssh/​known_hosts,然后再试。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​点评:<​br/>​ 当然 ssh 还有很多的功能没有用,如先用 ssh 连接 上去后,可以用 scp命令来存取文件,等等。<​br/>​ scp -P xxx&​nbsp;<​a target="​_blank"​ href="​mailto:​user@host:​path/​file"​ style="​color:​rgb(0,​113,​179)">​user@host:​path/​file</​a></​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​其它突破网关传送文件的方式也千变万化。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​优点是:<​br/>​ 可以突破网关,一般情况下,向网管要求在网关上给你做端口映射是不现实的,但用此法你可以让要好的朋友给你做。<​br/>​ 使用方案一:比较点对点传送文件比较方便,或使用ssh进行远程控制内网。<​br/>​ ssh本身是加密,保证安全可靠。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​缺点也不少:<​br/>​ 使用 ssh 加密,势必影响性能,可以用 -C 选项调节压缩率。<​br/>​ 如果象方案二使用额外的服务器,数据都要服务器中转(我是这样认为,没跟踪过),势必影响速度。<​br/>​ 公网的服务器不好找。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​建议:恳请编程高手们根据类似得原理,做个端口转发小工具,效果会更好。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​本文,雕虫小计,漏洞无数。旨在提供一种如何解决问题的思路,请大侠不要笑话。也请大家多多支持,共同提高。</​p><​p style="​line-height:​27px;​margin-bottom:​1.2em;​padding-top:​0px;​padding-bottom:​0px;​color:​rgb(59,​59,​59);​font-size:​15px">​特别感谢 :sleeve ,x11 兄,​是他们帮助一起实现的。以及 irc.linuxfire.com 中的大伙:)</​p><​p><​br/></​p>​
服务器技术/linux/ssh的端口转发.1528869424.txt.gz · 最后更改: 2018/09/10 18:01 (外部编辑)