SSH 远程连接端口转发

    xiaoxiao2021-03-25  366

    需求分析:

    如何才能访问内网中的linux主机呢?

     由几个靠谱的方法:

    1 路由端口转发,但是需要有路由器配置修改权限

    2 花生壳 向日葵相关产品,但是平台有局限性

    3 靠谱简单的办法 : ssh 端口转发

    ssh 端口转发需要的条件:

     有一台有公网IP地址的主机,买个vps就ok,当然也可以使用家用路由器。

    参考一篇经典文章:实战SSH端口转发

    基本操作流程:希望台内网一太服务器A,有一台公网linux B, 希望在家里用C随时连接A。

    在A上执行命令:

    ssh -gfnNTR x.x.x.x:8899:localhost:22 root@x.x.x.x -o ServerAliveInterval=300

    其中x.x.x.x 为公网linux  B,localhost 为A

    然后在C上执行:

    ssh root@x.x.x.x -p 8899

    即可连接到A上。

    参数解释:

    -g :Allows remote hosts to connect to local forwarded ports. 

            允许远程主机连接到x.x.x.x 的所转发的端口(8899),这个是最重要的一个选项,如果希望本配置项在B中生效,需要编辑中

            /etc/ssh/sshd_config   打开GatewayPorts 并把no 改为yes,并重起ssh server生效

    - f:Requests ssh to go to background just before command execution. 后台执行

    -n:Redirects stdin from /dev/null .  This must be used when ssh is run in the background.

    -N:Do not execute a remote command.

    -T:Disable pseudo-tty allocation.  不占用tty shell

    -R:[bind_address:]port:host:hostport 

           Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.  This works by allocating a socket to listen to              port on the remote side, and whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port            hostport from the local machine.

          将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口.  远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过 安全通道转向出去, 同时本地主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口.

    转载请注明原文地址: https://ju.6miu.com/read-19003.html

    最新回复(0)