[关闭]
@mrz1 2018-01-09T07:14:28.000000Z 字数 2324 阅读 926

ssh端口转发三种场景

服务


参考样例

1.本地转发:A通过B去连接C;在A机器上操作(telnet服务)

1A通过ssh(telnet)访问B,B【解析ssh(telnet)】通过telnet访问C

角色:C充当的telnet服务器;B充当的是SSH服务器和telnet客户端

1.确定是否安装着两个包;并且启用

  1. [root@centos7 ~]#rpm -qa telnet*
  2. telnet-0.17-64.el7.x86_64 //客户端
  3. telnet-server-0.17-64.el7.x86_64 //服务器端

2.在C上把A(ip)加入防火墙策略

  1. [root@centos7 ~]#iptables -A INPUT -s A(ip) -j REJECT

3.可以先查看B和C当前的有谁在连接自己

  1. [root@centos7 ~]#ss -nt //是为了一会查看

4.在A机器上

  1. [root@centos7 ~]#ssh -L 9527:C(ip):23 B(ip) -Nf //成功
  2. root@172.18.101.93's password:
  3. 解读:
  4. 9527(随便设置未使用的端口号)
  5. C(ip):23(C机器的ip:telnet端口23)
  6. B(ip)(B机器的ip)
  7. -Nf 不打开远程shell,处于等待状态并且后台启用
  8. 这里C机器认为是B连接而不是A;但是真正的发起者是A

5.在看过程3查看B和C当前的是谁在连接自己

  1. 这里看的只是主要连接ABC;别的没写;
  2. [root@centos6 ~]#ss -nt //B上
  3. State Recv-Q Send-Q Local Address:Port PeerAddress:Port
  4. ESTAB 0 0 B(ip):22 A(ip):41084
  5. [root@centos7 ~]#ss -nt //C上
  6. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  7. ESTAB 0 0 ::ffff:C(ip):23 ::ffff:B(ip):60124

远程转发:在B机器上让A去连接C

1在B机器上让A去连接C;(C禁止A(ip)去连接)


  1. [root@centos7 ~]#ss -ntl // ::1:25 这里监听的是127.0.0.1地址 修改
  2. State Recv-Q Send-Q Local Address:Port LISTEN 0 100 ::1:25 :::*
  3. [root@centos7 ~]#vim /etc/postfix/main.cf
  4. inet_interfaces = localhost //找到这一行localhost改为all
  5. [root@centos7 ~]#systemctl restart postfix //重新启动服务
  6. [root@centos7 ~]#systemctl status postfix //查看服务状态
  7. [root@centos7 ~]#ss -ntl //比刚才多了一行*所有ip
  8. State Recv-Q Send-Q Local Address:Port
  9. LISTEN 0 100 *:25 *:*
  10. LISTEN 0 100 :::25 :::*

2.在B机器上操作

  1. 因为C已经禁止A去连接;所以需要通过B去连接C
  2. [root@centos6 ~]#ssh -R 9999:172.18.101.118:25 172.18.102.28 -fN

3.这时在A机器上去连接C,并发邮件

  1. [root@centos7 ~]#telnet 127.0.0.1 9999
  2. Trying 127.0.0.1...
  3. Connected to 127.0.0.1.
  4. Escape character is '^]'.
  5. 220 centos7.qifei.com ESMTP Postfix
  6. mail from:zqf@alibaba.com //邮箱
  7. 250 2.1.0 Ok
  8. rcpt to:root //发给谁
  9. 250 2.1.5 Ok
  10. data //数据
  11. 354 End data with <CR><LF>.<CR><LF>
  12. subject:hello
  13. welcome to alibaba!
  14. .
  15. 250 2.0.0 Ok: queued as 67A17195746
  16. quit
  17. 221 2.0.0 Bye
  18. Connection closed by foreign host.
  19. 这里C机器认为是B连接而不是A;但是真正的发起者是A

4.C机器上查看邮件

  1. [root@centos7 mail]#mail
  2. Heirloom Mail version 12.5 7/5/10. Type ? for help.
  3. "/var/spool/mail/root": 2 messages 1 new
  4. >N 2 zqf@alibaba.com Tue Jan 9 14:28 11/328 "hello"
  5. & 2
  6. Message 2:
  7. From zqf@alibaba.com Tue Jan 9 14:28:50 2018
  8. Return-Path: <zqf@alibaba.com>
  9. X-Original-To: root
  10. Delivered-To: root@centos7.qifei.com
  11. subject:hello
  12. Status: R
  13. welcome to alibaba!

动态端口转发:

16(A)通过5(B)去连接7(C);

  1. 6(A)机器上执行
  2. [root@centos7 ~]#ssh -D 1080 root@172.18.101.93 -N
  3. root@172.18.101.93's password: //浏览器就可以访问7(C)l
  4. 这个是命令查看网页
  5. [root@centos7 ~]#ssh -D 1080 root@172.18.101.93 -Nf
  6. root@172.18.101.93's password:
  7. [root@centos7 ~]#curl --socks5 127.0.0.1:1080 http://172.18.101.118/
  8. <h1>下班了....</h1>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注