SSH反向隧道 (內網穿透)

SSH反向隧道 (內網穿透)

  • 需要準備兩臺機器
  • A 機器爲公網機器 (11.22.33.44) 相當於橋樑
  • B 機器爲內網機器 (10.0.0.2) 需要穿透

構建隧道

  1. 需要在 A 機器上將sshd服務開啓 GatewayPorts 的配置
    設置 /etc/ssh/sshd_config 配置文件中的 #GatewayPorts noGatewayPorts yes
    sed -i "/GatewayPorts/c GatewayPorts yes" /etc/ssh/sshd_config
    systemctl restart sshd
    
  2. 參數詳解
    • -f 後臺執行ssh指令
    • -C允許壓縮數據
    • -N 不執行遠程指令
    • -R 將遠程主機(服務器)的某個端口轉發到本地端指定機器的指定端口
    • -L 將本地機(客戶機)的某個端口轉發到遠端指定機器的指定端口
    • -o ServerAliveInterval=300 ssh 客戶端每300秒就給server發個心跳,以免鏈路被RST
  3. 建立A機器到B機器的反向代理,即使用ssh隧道功能將 A 機器上指定端口轉發到 B 內網機器上
    ssh -fCNR 8222:10.0.0.2:22 [email protected]
    # OR
    ssh -fCNR 8222:10.0.0.2:22 [email protected] -o ServerAliveInterval=300
    
  4. 建立B機器的正向代理,用來做轉發,B 機器的8282端口爲本地轉發端口,負責和外網進行通信,並將數據轉發到A機器的22端口,實現了可以從其他機器訪問的功能
    ssh -fCNL 0.0.0.0:8282:11.22.33.44:22 [email protected]
    

issues

  1. 需要這個隧道能夠一直保持連接狀態,可以使用 autossh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章