SSH端口转发
 

一:什么是端口转发?

    SSH 会自动加密和解密所有SSH 客户端与服务端之间的网络数据。但是,SSH 还能够将其他TCP 端口的网络数据通过SSH 链接来转发,并且自动提供了相应的加密及解密服务。这一过程也被叫做“隧道”(tunneling),这是因为SSH 为其他TCP 链接提供了一个安全的通道来进行传输而得名。例如,Telnet,SMTP,LDAP 这些TCP 应用均能够从中得益,避免了用户名,密码以及隐私信息的明文传输。而与此同时,如果工作环境中的防火墙限制了一些网络端口的使用,但是允许SSH 的连接,也能够通过将TCP 端口转发来使用SSH 进行通讯
 

二:SSH 端口转发能够提供两大功能:

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

三:SSH端口本地转发

本地转发机制:
    -L localport:remotehost:remotehostportsshserver
选项:
-f 后台启用
-N 不打开远程shell,处于等待状态
-g 启用网关功能
举例:
ssh–L 9527:telnetsrv:23 -N sshsrv
telnet 127.0.0.1 9527
当访问本机的9527的端口时,被加密后转发到sshsrv的ssh服务,再解密被转发到telnetsrv:23
data < >localhost:9527 < > localhost:XXXXX < > sshsrv:22 < > sshrv:yyyyy < > telnetsrv:23
流程解释:
    数据一旦telnet以后,数据会发送到本机9527端口,再在本机开一个随机端口,充当ssh客户端,再把数据流量发送到22端口的ssh服务端,收到数据以后,解密数据,临时开一个随机端口充当客户端,再把流量发送到23端口telnet服务端
ssh协议里面封装了telnet,一旦A连接了B主机,立即使用telnet连接C主机,此过程可以突破防火墙的限制
 #实验A-x->C   A-B->C
 
选项回顾
-L 本机端口
-f 后台启用,可以在本机直接执行命令,无需另开新终端
-N 不打开远程shell,处于等待状态,不跳到远程主机,还在主机上,只是搭好了隧道,桥搭好,不ssh上去
-g 启用网关功能
-R 服务端口
 
环境:
[root@Centos6 ~]# vim /etc/hosts
A:Centos7  172.18.254.173
B:Centos6  172.18.253.175
C:Centos6-1 172.18.253.192
    注:每个机器都添加hosts
 
条件:
#设置防火墙策略不让Centos7直连Centos6-1
[root@centos6-1 ~]# iptables -A INPUT -s centos7 -j REJECT
 
操作:
[root@centos7 ~]# ssh -L 9527:centos6-1:23 -Nf centos6
#通过本地9527端口访问centos6-1服务器IP地址使用telnet协议23端口,跳板机ip地址
[root@centos7 ~]# ss -tn
State     Recv-Q Send-Q         Local Address:Port                       Peer Address:Port              
ESTAB      0      96            172.18.254.173:22                          172.18.0.100:57618              
ESTAB      0      0             172.18.254.173:35024                     172.18.253.175:22
#隧道已经搭建好了,此时A主机可以通过telnet访问C主机
[root@centos7 ~]# telnet 127.0.0.1 9527    #访问成功
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
CentOS release 6.9 (Final)
Kernel 2.6.32-696.el6.x86_64 on an x86_64
Centos6-1 login: docker  
Password:
Last login: Mon Apr  9 21:33:43 from centos7
 
#删除ssh端口本地装发
[docker@centos6-1 ~]$ exit    #退出telnet但是隧道还是搭着可以使用ps aux查看到
[root@centos7 ~]# ps aux
root       6624  0.0  0.1 180392  1392 ?       Ss   21:59   0:00 ssh -L 9527:centos6-1:23 -Nf centos6
[root@centos7 ~]# killall ssh   #删除搭建的桥
[root@centos7 ~]# telnet centos6-1 #此时可以说明清除成功
Trying 172.18.253.192...
telnet: connect to address 172.18.253.192: Connection refused
 
最后总结:
Centos6上面实现代理科学上网不能用1080端口
有的公司管理的比较严格,指向外面开放80端口,如果想从我外面连接主机可以改ssh接口,在连接指定端口-p
 

四:SSH端口远程装发

远程转发机制:
-R sshserverport:remotehost:remotehostportsshserver
举例:
ssh–R 9527:telnetsrv:23 –N sshsrv
让sshsrv侦听9527端口的访问,如有访问,就加密后通过ssh服务转发请求到本机ssh客户端,再由本机解密后转发到telnetsrv:23
Data < > sshsrv:9527 < > sshsrv:22 < > localhost:XXXXX < > localhost:YYYYY< >telnetsrv:23
 
需求:
在A(Centos7)上开启smtp服务(postfix),B(Centos6)做跳板,C(Centos6-1)客户端给Centos7发送邮件
 
流程解析:
C-x->A    (拒绝访问)
C-B->A      (通过远程代理,接受访问)   
 
查看环境: 
[root@centos7 ~]# ss -ntl   #查看25端口有没有开启
LISTEN      0      100                            127.0.0.1:25                                                 *:*  
[root@centos7 ~]# vim /etc/postfix/main.cf   #对文件配置文件进行修改,Centos7已经不同早期系统使用sendmail作为默认邮件服务器
#目的:修改绑定接口配置
# 不同文件有些localhost意思完全相反,此处localhost再邮件是127,而DNS中是表示的是本地的所有IP,相当于此处的all
#inet_interfaces = all   #此行不是所有主机而是所有ip意思   #此处去掉注释
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
inet_interfaces = localhost    #这一行说明接口是绑定在127.0.0.1上   #加上注释
[root@centos7 ~]# ss -ntl
LISTEN      0      100                                   *:25                                                 *:*    
 
 
环境配置:
[root@centos7 ~]# systemctl start telnet.socket   #开启telnet-server
[root@centos7 ~]# systemctl status telnet.socket   #查看一下是否已经开启
[root@centos7 ~]# iptables -nvL   #查看一下防火墙策略
[root@centos7 ~]# systemctl stop firewalld.service     #关闭防火墙
[root@Centos6 ~]# chkconfig --list iptables   #查看各运行级别防火墙开机运行状态      
iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@Centos6 ~]# chkconfig iptables off   #关闭防火墙
[root@Centos6 ~]# chkconfig --list iptables   #所有运行级别防火墙都关闭
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@Centos6 ~]# chkconfig --del iptables   #移除开机自动启动防火墙
 
[root@Centos6-1 ~]# telnet centos7 25   #测试centos6-1可以通过telnet 25端口连接Centos7
Trying 172.18.254.173...
Connected to centos7.
Escape character is '^]'.
220 centos7.localdomain ESMTP Postfix    #测试成功,25端口可以连接
#关闭跳板机(Centos6)和远程服务器(Centos7)上的SELINUX
[root@centos7 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@centos7 ~]# setenforce 0
 
创建目标条件:
[root@centos7 ~]# iptables -A INPUT -s centos6-1 -j REJECT   #设置防火墙策略,使Centos7不接受Centos6一切请求
[root@centos6-1 ~]# telnet centos7 25
Trying 172.18.254.173...
telnet: connect to address 172.18.254.173: Connection refused
 
操作:Centos6-1使用远程转发,发送邮件给Centos7
 
[root@Centos6 ~]# ssh -R 9527:centos7:25 -fN centos6-1   #建立隧道,此过程确保Centos6可以公钥连接centos6-1,否则需要手动口令验证
[root@Centos6 ~]# ss -ntl    #检查邮件服务,可以知道服务已经开启
LISTEN      0      100                                               ::1:25                                               :::*    
LISTEN      0      100                                         127.0.0.1:25                                                 *:*  
[root@centos6-1 .ssh]# ss -ntl   #此时也可以看到跳板机Centos6-1上9527端口已经在监听
LISTEN      0      128                                         127.0.0.1:9527                                               *:*    
LISTEN      0      128                                               ::1:9527                                             :::*  
[root@centos6-1 ~]# ps -ef |grep ssh   #查看一下ssh运行的进程
root       2288      1  0 09:58 ?        00:00:00 ssh -R 9527:centos7:25 -fN centos6-1
[root@centos6-1 .ssh]# telnet 127.0.0.1 9527   #此时可以通过telnet连接Cetnos7绕过了防火墙
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 centos7.localdomain ESMTP Postfix    #如果想退出进入telnet>   快捷键ctrl + ]
[root@centos7 ~]# ss -nt
State       Recv-Q Send-Q                     Local Address:Port                                   Peer Address:Port              
ESTAB       0      0                         172.18.254.173:25                                    172.18.253.175:32898              
ESTAB       0      96                        172.18.254.173:22                                      172.18.0.100:58652
#此时在Centos7上发现有IP尾号为100的机子在连接实际上是Centos6-1在连接
 
#最后验证是否能成功发送邮件
[root@centos6-1 ~]# telnet localhost 9527   #给Centos7发送邮件
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 centos7.localdomain ESMTP Postfix
mail from:ceo@app.com
250 2.1.0 Ok
rcpt to:root@centos7
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello
.
250 2.0.0 Ok: queued as 86A5411EA866
quit
221 2.0.0 Bye
Connection closed by foreign host.
 
[root@centos7 ~]# mail    #可以收到邮件信息
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 12 messages 12 new
>N  1 root                 Fri Mar 16 10:31  14/496   "Output from your job       4"
N  2 (Cron Daemon)         Sat Mar 17 08:00  25/886   "Cron <root@zangfans> /root/bin/etcback.sh"
N  3 root                 Sat Mar 17 10:20  14/506   "Output from your job       2"
N  4 (Cron Daemon)         Tue Mar 20 08:00  25/886   "Cron <root@zangfans> /root/bin/etcback.sh"
N  5 user@localhost.local Wed Mar 28 08:19 2129/142373 "[abrt] kernel: WARNING: CPU: 1 PID: 0 at net/sched/sch_generic.c:300 dev"
N  6 user@localhost.local Fri Apr  6 19:41 1151/90323 "[abrt] nautilus: nautilus-desktop killed by SIGSEGV"
N  7 HR@centos7.localdoma Tue Apr 10 14:44  16/633   "*** SECURITY information for centos7 ***"
N  8 HR@centos7.localdoma Tue Apr 10 16:38  16/637   "*** SECURITY information for centos7 ***"
N  9 HR@centos7.localdoma Tue Apr 10 16:38  16/641   "*** SECURITY information for centos7 ***"
N 10 HR@centos7.localdoma Tue Apr 10 16:39  16/637   "*** SECURITY information for centos7 ***"
N 11 HR@centos7.localdoma Tue Apr 10 16:39  16/641   "*** SECURITY information for centos7 ***"
N 12 ceo@app.com           Wed Apr 11 10:20  10/314  
& 12
Message 12:
From ceo@app.com Wed Apr 11 10:20:29 2018
Return-Path: <ceo@app.com>
X-Original-To: root@centos7
Delivered-To: root@centos7.localdomain
Status: R
 
hello
 
 

五:动态端口转发

原理:
当用firefox访问internet时,本机的1080端口做为代理服务器,firefox的访问请求被转发到sshserver上,由sshserver替之访问internet
 
动态转发机制:
ssh-D 1080 root@sshserver
在本机firefox设置代理socket proxy:127.0.0.1:1080
 
测试:
curl --socks5 127.0.0.1:1080 http://zangfans.com
 
在企业内部或者网络中,基于安全策略不能随便访问互联网某些站点
需求:
访问站点:Centos7
代理服务:centos6
企业内部主机:Centos6-1
 
环境配置:
[root@centos7 ~]# systemctl status httpd   #查看Cetnos7上httpd服务有没有开启
[root@centos7 ~]# iptables -A INPUT -s centos6-1 -j REJECT   #设置防火墙策略,使Centos7不接受Centos6一切请求
#确保Centos6,Centos6-1开启了防火墙都关闭了
#设置Centos7网页显示内容
[root@centos7 html]# vim index.html
<h1>www.google.com</h1>
 
#虚拟机上此时是否能够访问Centos7站点
#在此处需要多加,主机自己现在init如果从runlevel 3到5可能会出现重启防火墙策略,开启则关闭策略,对Centos7的策略重置
 
操作:
选项:
  -D   本地端口号
 
[root@centos6-1 ~]# ssh -D 1080 centos6   #无秘钥登录,则需要密码验证
[root@Centos6 ~]# ss -nt
State       Recv-Q Send-Q                                 Local Address:Port                                   Peer Address:Port
ESTAB       0      0                                      172.18.253.175:47618                                 172.18.253.192:22    
ESTAB       0      0                                      172.18.253.175:22                                    172.18.253.192:45114
ESTAB       0      0                                      172.18.253.175:22                                      172.18.0.100:58896
[root@centos6-1 ~]# ss -nt
State       Recv-Q Send-Q                                 Local Address:Port                                   Peer Address:Port
ESTAB       0      0                                      172.18.253.192:45172                                 172.18.253.175:22    
ESTAB       0      0                                      172.18.253.192:22                                      172.18.0.100:58868
ESTAB       0      0                                      172.18.253.192:22                                    172.18.253.175:47618
 
#最后通过curl命令页面验证
[root@centos6-1 ~]# curl --socks5 127.0.0.1 centos7   #默认端口就是1080
<h1>www.google.com</h1>
 
  #此方法也只能通过linux机器才进行互联网访问,windows机器暂不支持
 
知识补充
curl里面--socks5选项
  --socks5 <host[:port]>
            Use the specified SOCKS5 proxy  - but resolve the host name locally. If the port number is not specified, it is
            assumed at port 1080.
 
            This option overrides any previous use of -x, --proxy, as they are mutually exclusive.
 
            Since 7.21.7, this option is superfluous since you can specify a socks5 proxy with -x, --proxy using a socks5://
            protocol prefix.
 
            If this option is used several times, the last one will be used. (This option was previously wrongly documented and
            used as --socks without the number appended.)
 
            This option (as well as --socks4) does not work with IPV6, FTPS or LDAP.
基于网页验证
此时虚拟机任不能之间访问Centos7
当本地1080端口受到外部网站请求会转发到外部服务器上面去,代理服务器通过1080端口把请求转发到站点服务器上面去
此时方可正常访问外部站点
 

SSH端口转发(本地转发、远程转发、动态转发)的更多相关文章

  1. 【译】SSH隧道:本地和远程端口转发

    本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...

  2. SSH的本地、远程、动态端口转发实验笔记

    SSH端口转发 SSH 会自动加密和解密所有 SSH 客户端与服务端之间的网络数据.但是,SSH 还能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务.这一 ...

  3. Linux修改SSH端口和禁止Root远程登陆

    Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...

  4. linux修改ssh端口 以及禁止root远程登录 (实验机 CentOs)

    把ssh默认远程连接端口修改为3333 1.编辑防火墙配置: vi /etc/sysconfig/iptables 防火墙新增端口3333,方法如下: -A INPUT -m state --stat ...

  5. 【转】Linux修改SSH端口和禁止Root远程登陆

    Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...

  6. Debian修改ssh端口和禁止root远程登陆设置

    linux修改端口22vi /etc/ssh/sshd_config找到#port 22将前面的#去掉,然后修改端口 port 1234重启服务就OK了service sshd restart或/et ...

  7. Linux 修改SSH端口及禁用ROOT远程SSH登陆

    打开配置文件: vim /etc/ssh/sshd_config 修改Port及PermitRootLogin节点 : //默认为yes 允许 no表示禁止 PermitRootLogin no // ...

  8. centos修改SSH端口并禁用root远程登录

    1.使用 root 用户执行以下步骤:只在 CentOS 6.5 下验证. 2.先查看下服务器端口号范围: # sysctl -a|grep ip_local_port_range 3.修改端口 vi ...

  9. 利用SSH端口转发实现远程访问内网主机远程桌面(一) 建立SSH转发

    近期家里更换了移动的宽带,拨号后拿到的是10开头的内网IP,就不能像之前一样通过路由器的端口映射实现从外网访问主机的远程桌面.这种情况下可以利用一台具有公网IP的服务器充当中转,利用SSH的隧道转发功 ...

  10. 【转】实战 SSH 端口转发

    本文转自:http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/index.html,至于有什么用,懂的懂! 实战 SSH 端口转发 通 ...

随机推荐

  1. 随记PC-win7 64位系统网络连接状态一直转圈、等待状态的异常解决方案

    各位看官好~ 最近电脑也做了下升级,入手个士必得360G的SSD来玩玩,顺便也下个新系统,看看有什么区别,想想顺便升级下系统也是好的,就开始了装机,装系统的路程~~~~~~ 好了不说废话,直接进入主题 ...

  2. 去除input的自动填充色

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #ffffff inset !important; }

  3. java 5并发中的阻塞队列ArrayBlockingQueue的使用以及案例实现

    演示一个阻塞队列的使用 public class BlockingQueueTest { public static void main(String[] args) { //创建一个包含三个元素的阻 ...

  4. Java基础学习笔记二十六 JDBC

    什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库,J ...

  5. 自定义ArrayList

    自定义实现ArrayList很简单,只需要明白下面几点 1.ArrayList 底层是由数组组成的 2.数组有容量限制,但是ArrayList并没有(实际上也有,Integer.MAX_VALUE). ...

  6. RTMP消息详细介绍

    本文继上篇简单分析了RTMP协议如何进行通信进一步详细分析RTMP的消息都有哪些,以及这些消息有什么作用. 一.RMTP消息 由上一篇文章可知RTMP 消息有分成两个部分,一个是头部,一个是有效负载. ...

  7. pip安装selenium报错:Read timed out

    今天打算把selenium降级重新安装,发现安装时总是失败,报如下错误: raise ReadTimeoutError(self._pool, None, 'Read timed out.') pip ...

  8. android 与 服务器通信

    android 与 服务器通信 服务端代码: (1)control 层 /** * 用户登录 * @return */ @RequestMapping(value = "/login&quo ...

  9. 实验二Java面向对象程序设计实验报告(2)

    实验二 Java面向对象程序设计 实验概述: 课程:程序设计与数据结构 班级:1623班 姓名: 邢天岳 学号:2309 指导老师:娄老师 王老师 实验日期:2017.4.16 实验名称: Java面 ...

  10. sqlserver学习_01

    sqlserver的学习成长之路,每一个技术的学习过程都是值得让人回味的,现在百度上关于sqlser的资料很多,但是都太杂,希望能为大家分享一点简单易懂的干货,跟大家一起进步学习. 一.建表 1.创建 ...