SSH端口转发

SSH端口转发



关于SSH端口转发

原理

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

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

实验前准备

实验前的准备工作非常重要,关乎实验能否顺利的进行,请务必在每台主机上完成准备工作

关闭防火墙

CentOS 7关闭防火墙的方法

root&Centos7: ~#systemctl disable firewalld.service
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-
org.fedoraproject.FirewallD1.service.
#禁止防火墙自启动
root&Centos7: ~#systemctl stop firewalld.service
#关闭防火墙
root&Centos7: ~#iptables -vnL
Chain INPUT (policy ACCEPT 4044 packets, 482K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 77 packets, 7544 bytes)
 pkts bytes target     prot opt in     out     source               destination
 #确认防火墙已经关闭

CentOS 6关闭防火墙的方法

[root@localhost ~]# chkconfig iptables off
#禁止防火墙自启动
[root@localhost ~]# service iptables stop
#关闭防火墙
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@localhost ~]# iptables -vnL
                ……此处省略部分显示内容……
关闭SElinux策略
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#禁用SElinux策略
[root@localhost ~]# grep SELINUX=disabled /etc/selinux/config
#确定SELINUX=disabled,已经将SElinux策略禁用
SELINUX=disabled
[root@localhost ~]# setenforce 0
#设置SELinux当前状态为Permissive
[root@localhost ~]# getenforce
#获取SELinux当前状态
Permissive
检查是否已安装Telnet服务

Telnet服务有两个软件包,请确认都已经安装。
telnet-server:此软件包为server包,用于接收Telnet client的连接
telnet:此软件包是client包,用于连接Telnet service

[root@localhost ~]# rpm -qa telnet-server
[root@localhost ~]# rpm -ql telnet
package telnet is not installed
#使用rpm -ql 或rpm -qa查看软件包是否已经安装。上面是没有安装的显示。
[root@localhost ~]# rpm -qa telnet-server
telnet-server-0.17-48.el6.x86_64
[root@localhost ~]# rpm -ql telnet
/usr/bin/telnet
/usr/share/man/man1/telnet.1.gz
#使用rpm -qa可以查看到软件包或使用rpm -ql 查看到软件包安装目录,则说明包已经安装。
安装Telnet服务

若没有安装Telnet服务的这两个软件包,则使用yum安装。

[root@localhost ~]# yum list |grep telnet
telnet-server.x86_64                     1:0.17-48.el6                    cdrom
telnet.x86_64                            1:0.17-48.el6                    cdrom
#确认要安装的软件包都在yum源中。
[root@localhost ~]# yum install -y telnet-server telnet
#使用yum同时安装缺少的两个软件包,如果只缺少一个,则只安装对应缺少的那个软件包。
[root@localhost ~]# rpm -ql telnet-server
[root@localhost ~]# rpm -ql telnet
#确认两个软件包都已经安装全。
启动Telnet服务

CentOS 6启动Telnet服务的方法

[root@localhost ~]# chkconfig telnet on
#设置telnet service状态为on
[root@localhost ~]# chkconfig --list telnet
telnet          on
#检查上一步的设置已生效
[root@localhost ~]# service xinetd start
Starting xinetd:                                           [  OK  ]
#启动xinetd服务
[root@localhost ~]# ss -ntl|grep :23
LISTEN     0      64                       :::23                      :::*
#确定TCP的23端口已经打开,Telnet服务已可以接受访问。

CentOS 7启动Telnet服务的方法

root&Centos7: ~# systemctl enable telnet.socket
Created symlink from /etc/systemd/system/sockets.target.wants/telnet.socket to /usr/lib/systemd/system/telnet.socket.
#设置telnet服务为开机自启动
root&Centos7: ~# systemctl start telnet.socket
#启动telnet服务
root&Centos7: ~# systemctl is-active telnet.socket
active
#验证Telnet服务当前是否为活动状态
root&Centos7: ~#ss -ntl|grep :23
LISTEN     0      128         :::23                      :::*

本地端口转发

这里写图片描述

A需要与C建立Telnet连接,但出于安全考虑A不能直接连接C,需要通过B做跳板机来连接C。Telnet是不加密传输的,为了保证A和B之间传输数据的安全,需要在A上建立本地端口转发,建立ssh安全隧道,实现加密传输。A到B走的是ssh协议,通信的数据经过封装,不用担心被截获,在内网可以走Telnet协议,实现安全通信。

建立本地端口转发
root&Centos6: ~#ss -nlt|grep 9527
#检查要使用的9527端口是否被占有,被占有则更换其他端口。
root&Centos6: ~#ssh -L 9527:172.18.22.200:23 -Nf 172.18.0.2
            ……此处省略部分显示内容……
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.18.0.2' (RSA) to the list of known hosts.
root@172.18.0.2's password:     #此处输入B主机的登录密码
#ssh -L 本地转发端口:要访问的目标主机:目标服务的端口 -Nf 跳板机地址
#       -L 建立本地端口转发
#       -f 后台启用
#       -N 不打开远程shell,处于等待状态
确认本地端口转发已经建立
root&Centos6: ~#ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
ESTAB      0      0                                        172.18.22.100:42368                                      172.18.0.2:22
#172.18.22.100与172.18.0.2已经建立连接,说明加密通信的隧道已经搭好。
root&Centos6: ~#ss -nlt|grep 9527
LISTEN     0      128               127.0.0.1:9527                     *:*
LISTEN     0      128                     ::1:9527                    :::*
#本地的9527端口已经打开并处于监听状态
使用创建好的加密隧道进行通信
root&Centos6: ~#telnet 127.0.0.1 9527
        ……此处省略部分显示内容……
Centos7 login: sun  #输入C主机的非root账户。
Password:   #输入密码
Last login: Tue Sep 12 22:30:37 from ::ffff:172.18.22.100
[sun@Centos7 ~]$
#已经看到了C主机的命令提示符说明远程连接成功。
查看A主机连接状态
root&Centos6: ~#ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
                    ……此处省略部分显示内容……
ESTAB      0      0                                            127.0.0.1:46686                                       127.0.0.1:9527
ESTAB      0      0                                        172.18.22.100:42368                                      172.18.0.2:22
#本机的9527端口连接了本机的46686端口,本机既是Telnet client也是Telnet service。
#本机的42368端口连接了B主机的22端口,建立了ssh通讯。
查看B主机连接状态
[root@localhost ~]# ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
ESTAB      0      0                                           172.18.0.2:39298                                   172.18.22.200:23
ESTAB      0      0                                           172.18.0.2:22                                      172.18.22.100:42368
#B主机同时连接这AC主机,B主机连接的是C主机的23端口,也就是C主机Telnet服务的监听端口。
查看C主机连接状态
root&Centos7: ~#ss -nt
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port
ESTAB      0      0                    ::ffff:172.18.22.200:23                                  ::ffff:172.18.0.2:39298
#已经建立了Telnet连接,不是和A主机建立的而是和B主机建立的。
结束本地端口转发
[sun@Centos7 ~]$ exit
logout
Connection closed by foreign host.
root&Centos6: ~#
#退出登录的远程主机
root&Centos6: ~#ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
                    ……此处省略部分显示内容……
ESTAB      0      0                                        172.18.22.100:42368                                      172.18.0.2:22
#A和B的连接还在,说明安全隧道还在搭着。
root&Centos6: ~#ps aux|grep "ssh -L"
root      18619  0.0  0.0  61932  1284 ?        Ss   22:57   0:00 ssh -L 9527:172.18.22.200:23 -Nf 172.18.0.2
root      18889  0.0  0.0 103332   844 pts/0    S+   23:27   0:00 grep ssh -L
#本地端口转发的进程还在。
root&Centos6: ~#killall ssh
#杀掉本地端口转发的进程
root&Centos6: ~#ps aux|grep "ssh -L"
root      18892  0.0  0.0 103332   848 pts/0    S+   23:28   0:00 grep ssh -L
root&Centos6: ~#ss -nlt|grep 9527
#负责本地端口转发的9527端口已经关闭了,以结束本地端口转发。

远程端口转发

这里写图片描述

A主机需要给C主机发送邮件,但是C主机和B主机都在内网,A主机不能主动连接。B主机可以连接外网,需要B主机去主动访问A主机,并建立远程端口转发,接收A主机发来的邮件交给C主机。通过B主机做跳板机来实现A主机与C主机之间的TCP连接。

配置C主机的邮件服务
root&Centos7: ~#ss -nlt|grep 25
LISTEN     0      100    127.0.0.1:25                       *:*
LISTEN     0      100        ::1:25                      :::*
#正在监听25端口的是本地环回地址,会导致不能接收其它ip地址接收到的邮件。
root&Centos7: /#grep "inet_interfaces = " /etc/postfix/main.cf
    #inet_interfaces = all
    #inet_interfaces = $myhostname
    #inet_interfaces = $myhostname, localhost
    inet_interfaces = localhost
#localhost指的是127.0.0.1地址,all指本机所有地址。需要让inet_interfaces = all生效。
root&Centos7: /#sed -ri.bak 's/#(inet_interfaces = all)/\1/' /etc/postfix/main.cf
#将/etc/postfix/main.cf文件中inet_interfaces = all前的注释去掉。
root&Centos7: /#sed -ri.bak 's/inet_interfaces = localhost/#&/' /etc/postfix/main.cf
#将/etc/postfix/main.cf文件中inet_interfaces = localhost前加上注释。
root&Centos7: /#grep "inet_interfaces = " /etc/postfix/main.cf
    inet_interfaces = all
    #inet_interfaces = $myhostname
    #inet_interfaces = $myhostname, localhost
    #inet_interfaces = localhost
#检验配置文件更改结果
root&Centos7: /#systemctl restart postfix
#重启邮件服务,生效配置文件。
root&Centos7: /#ss -nlt|grep 25
LISTEN     0      100          *:25                       *:*
LISTEN     0      100         :::25                      :::*
#再次检查邮件服务器端口,留意端口监听地址的变化。
建立远程端口转发
[root@localhost ~]# ssh -R 9527:172.18.22.200:25 -fN 172.18.22.100
            ……此处省略部分显示内容……  
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.18.22.100' (RSA) to the list of known hosts.
root@172.18.22.100's password:
#ssh -R 远程转发端口:要访问的目标主机:目标服务的端口 -fN 进行远程转发的主机
#       -R 建立远程端口转发
#       -f 后台启用
#       -N 不打开远程shell,处于等待状态
确认远程端口转发已经建立

B主机的连接状态

[root@localhost ~]# ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
ESTAB      0      0                                           172.18.0.2:48008                                   172.18.22.100:22
#B主机作为ssh client已与A主机建立了ssh连接。

A主机的连接状态

root&Centos6: ~#ss -nlt|grep 9527
LISTEN     0      128               127.0.0.1:9527                     *:*
LISTEN     0      128                     ::1:9527                    :::*
#确定A主机的9527端口已处于监听状态
root&Centos6: ~#ss -nt
State      Recv-Q Send-Q                                   Local Address:Port                                     Peer Address:Port
ESTAB      0      0                                        172.18.22.100:22                                         172.18.0.2:48008
#已与B主机建立了ssh连接
使用创建好的加密隧道发送邮件
root&Centos6: ~#telnet 127.0.0.1 9527
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 Centos7.xiaoyu ESMTP Postfix
helo siwei,link
250 Centos7.xiaoyu
mail from:sun@siwei.link
250 2.1.0 Ok
rcpt to:root
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject:hello,ssh
ssh remote link
.   #.表示邮件已经完成
250 2.0.0 Ok: queued as 4822D412B44F
quit    #退出
221 2.0.0 Bye
Connection closed by foreign host.
查看C主机的连接状态
root&Centos7: /#ss -nt
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port
ESTAB      0      0                           172.18.22.200:25                                         172.18.0.2:59490
#本机的25端口接收到B主机的访问,并建立SMTP连接。
在C主机查看A主机发来的邮件
root&Centos7: /#mail
                    ……此处省略部分显示内容……
 N  8 sun@siwei.link        Wed Sep 13 10:48  11/321   "hello,s"
& 8    #输入数字8,查看第8封邮件
Message  8:
From sun@siwei.link  Wed Sep 13 10:48:13 2017
Return-Path: <sun@siwei.link>
X-Original-To: root
Delivered-To: root@Centos7.xiaoyu
subject:hello,ssh
Status: R

ssh remote link

& q     #退出邮箱
#接收到A主机发送来的邮件
在B主机关闭远程端口转发
[root@localhost ~]#killall ssh 

动态端口转发

这里写图片描述

C是一台web服务器,A主机被防火墙隔离,不能直接访问C主机的web服务 ,通过在A主机上建立动态端口转发,利用代理服务器B来访问A的web服务。

在C主机安装并配置httpd服务
root&Centos7: /#yum install -y httpd
root&Centos7: /#systemctl start httpd
root&Centos7: /#ss -ntl|grep 80
LISTEN     0      128         :::80                      :::*
root&Centos7: /#echo "<h1>www.siwei.link</h1>" >/var/www/html/index.html
#设置web网站首页内容
root&Centos7: /#cat /var/www/html/index.html
<h1>www.siwei.link</h1>
#验证首页内容写入成功
root&Centos7: /#iptables -A INPUT -s 172.18.22.100 -j REJECT
#在C主机设置防火墙策略禁止A主机访问。
在A主机访问C主机的web服务
root&Centos6: ~#curl http://172.18.22.200
curl: (7) couldn't connect to host
#访问被拒绝
在A主机建立动态端口转发
root&Centos6: ~#ssh -D 1080 172.18.0.2 -Nf
root&Centos6: ~#curl --socks5 127.0.0.1 http://172.18.22.200
<h1>www.siwei.link</h1>
#使用本地环回地址做代理,使用curl命令访问C主机的web网站
#看到C主机设置的首页内容,说明访问成功。
root&Centos6: ~#killall ssh
#关闭动态端口转发

附:误删除配置文件解决办法

无论在工作还是实验中,对待配置文件一定要慎重,改动前先做好备份,删除前先做好确认。不然就呵呵了o( ̄︶ ̄)o
下面是以postfix服务的/etc/postfix/main.cf配置文件为例,实现文件误删除后的恢复。

root&Centos7: ~#rpm -ql postfix|grep main.cf
#找出此文件来自于那个数据包
root&Centos7: ~#cd /
root&Centos7: /#rpm2cpio /mnt/cdrom/Packages/postfix-2.10.1-6.el7.x86_64.rpm |cpio -itv|grep /etc/postfix/main.cf
-rw-r--r--   1 root     root        27176 Jun 10  2014 ./etc/postfix/main.cf
25016 blocks
root&Centos7: /#rpm2cpio /mnt/cdrom/Packages/postfix-2.10.1-6.el7.x86_64.rpm |cpio -idv  ./etc/postfix/main.cf
./etc/postfix/main.cf
#这一步需要在根目录下进行,将光盘里软件包内的文件恢复出来。
root&Centos7: /#ls /etc/postfix/main.cf
/etc/postfix/main.cf

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值