local port forwarding

  • Machine and Network Condition

     qbit 的开发机为 Q(192.168.1.Q),
    想要访问主机 D(192.168.2.D) 上的 MongoDB(27017)
    Q 不能直连 MongoDB 192.168.2.D:27017
    Q 可以通过 ssh 访问主机 J(192.168.2.J)
    J 可以直连 MongoDB 192.168.2.D:27017
  • Diagram of machine and network status
    image.png
  • Execute the following command on the development machine Q to create a tunnel

     # 参数 N: 只做端口转发,不执行远程命令
    # 参数 L: Local 本地转发
    ssh -NL 6666:192.168.2.D:27017 qbit@192.168.2.J
  • Execute the following command on the development machine Q to connect to MongoDB

     mongosh mongodb://localhost:6666

Remote port forwarding

  • Machine and Network Condition

     qbit 的开发机为 Q(192.168.1.Q),
    想要将本机 6666 端口的服务暴露给主机 D(192.168.2.D) 访问
    D 不能直连 192.168.1.Q:6666
    D 可以与主机 J(192.168.2.J)建立 TCP 连接
    J 可以与 D 建立 TCP 连接
    J 不可以主动向 Q 发起连接
    Q 可以通过 ssh 访问 主机 J
  • Diagram of machine and network status
    image.png
  • Open on the host J /etc/ssh/sshd_config configure GatewayPorts , otherwise only monitor localhost
  • Execute the following command on the development machine Q to create a tunnel

     # -q Quiet mode.
    # -f 输入密码之后,后台运行
    # -N 只做端口转发,不执行远程命令
    # -R Remote 远程转发
    ssh -NR 7777:192.168.1.Q:6666 qbit@192.168.2.J
  • Execute the following command on the host D to access the service on port 6666 of the machine Q

     curl http://192.168.2.J:7777
  • ssh automatically disconnects the problem, you can modify the server /etc/ssh/sshd_config the following configuration items

     ClientAliveInterval 60
    ClientAliveCountMax 3
  • If the access report is similar to the following error, it means that the ssh server cannot access the corresponding port

     channel 1: open failed: connect failed: Connection refused

Dynamic port forwarding

  • Dynamic port forwarding is mainly used as a socks proxy
  • Machine and Network Condition

     qbit 的开发机为 Q(192.168.1.Q),
    想要访问互联网主机 ip.sb 的 80 端口
    J 可以与 ip.sb 建立 TCP 连接
    Q 不能直连 ip.sb:80
    Q 可以通过 ssh 访问 主机 J
  • Diagram of machine and network status
    image.png
  • Execute the following command on the development machine Q to create a tunnel

     # 参数 N: 只做端口转发,不执行远程命令
    # 参数 D: dynamic 动态转发
    ssh -ND localhost:6666 qbit@192.168.2.J
  • Execute the following command on the host D to access the service on port 6666 of the machine Q

     curl -x socks5h://localhost:6666 http://ip.sb
This article is from qbit snap

qbit
268 声望279 粉丝

引用和评论

0 条评论