ssh服務隧道鏈接,端口轉發

A,處於外網,B、C同處於一個局域網可以互相訪問。A 發起到B的ssh隧道連接,然後通過B轉發來訪問資源C。用telnet服務來舉例說明

//-------第一種情況,A 端發起隧道鏈接-----------

A 172.18.43.101/16 B 172.18.43.102/16 C 172.18.43.103.在hostC添加路由規則,禁止A訪問C.實現A 通過跳板機B 訪問C

ssh -L :9527 :172.18.43.103:23 -Nfg 172.18.43.102

協議,本地,隨機端口 目的主機,想訪問的服務端口 經過的跳板機

```

[root@node103 ~]#iptables -A INPUT -s 172.18.43.101 -j REJECT

[root@node101 ~]#ssh -L :9527:172.18.43.103:23 -Nfg 172.18.43.102 //在A執行命令。-N,不打開shell,-f後臺運行,-g充當網關

[root@node101 ~]#telnet 127.0.0.1 9527 //根據提示輸入用戶名,密碼可以登錄到C,注意不能用root,要用配普通用戶登錄。

[root@node101 ~]#killall ssh //殺死ssh隧道進程

[root@node101 ~]#telnet 127.0.0.1 9527

Trying 127.0.0.1...

telnet: connect to address 127.0.0.1: Connection refused //已經不能訪問了

```

//上述實驗室外部網絡訪問局域網機器作爲跳板。有侷限性。實際中一般用如下方法,來突破防火牆

//-------第二種情況-B端發起隧道連接---------------------------------

```

[root@node102 ~]#ssh -R :9527:172.18.43.103:23 172.18.43.101 -fN //在B運行

[root@node102 ~]#ps aux|grep ssh //B確定已經後臺運行

root 2924 0.0 0.2 112756 4344 ? Ss 15:09 0:00 /usr/sbin/sshd -D

root 12163 0.0 0.3 157152 5908 ? Ss 15:36 0:00 sshd: root@pts/0

root 12315 0.0 0.0 178544 1136 ? Ss 16:38 0:00 ssh -R :9527:172.18.43.103:23 172.18.43.101 -fN

root 12317 0.0 0.0 112712 968 pts/0 S+ 16:39 0:00 grep --color=auto ssh

[root@node101 ~]#ss -tnl //A主機已經監聽了9527端口

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 *:111 *:*

LISTEN 0 5 192.168.122.1:53 *:*

LISTEN 0 128 *:22 *:*

LISTEN 0 128 127.0.0.1:9527 *:*

[root@node101 ~]#telnet 127.0.0.1 9527 //A主機已經可以訪問telnet服務

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

```

//-------ssh動態端口轉發,在本機執行ssh 與跳板機B建立隧道,實現訪問本地的指定端口,會通過建立的隧道自動通過跳板機B來轉發

```

[root@node102 ~]#curl 172.18.43.103 //B 可以訪問

<h1/>www.google.com</h1>

[root@node101 ~]#curl 172.18.43.103

curl: (7) Failed connect to 172.18.43.103:80; Connection refused //A機訪問失敗

[root@node101 ~]#ssh -D 59527 [email protected] -fNg //建立隧道

[root@node101 ~]#curl --socks5 127.0.0.1:59527 172.18.43.103 //通過代理可以訪問

<h1/>www.google.com</h1>

```

//---------實現跨雲訪問內部數據庫------------------------------------------------------------

//----綁定特定騰訊雲ECS-TX-C,用華爲ECS-HW-C作爲跳板,訪問僅允許局域網訪問的數據庫ECS-HW-C-DB。

三臺ECS修改ssh服務端口爲59527,ECS-HW-C作爲跳板配置key免密登錄騰訊雲ECS-TX-C。數據庫ECS-HW-C-DB分配好測試賬號。

```

[root@longteng ~]# ssh -R 9527:192.168.0.112:3306 xx.xx.xx.xx:59527 -fN //在跳板機ECS-HW-C,運行指令,x.x.x.x 指需要訪問數據的客戶端

//9527客戶端閒置隨機端口,192.168.0.112,mysql局域網地址,mysql3306服務端口,59527被修改過的ssh服務端口

[root@nodetest ~]# mysql -h127.0.0.1 -uroot -P 9527 -p //輸入密碼後可以正常登錄

Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 1358

Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show processlist;

+------+------+---------------------+------+---------+------+-------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+------+------+---------------------+------+---------+------+-------+------------------+

| 1358 | root | 192.168.0.248:36460 | NULL | Query | 0 | init | show processlist |

+------+------+---------------------+------+---------+------+-------+------------------+

1 row in set (0.01 sec)

```


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章