SSH 远程连接端口转发

18 篇文章 1 订阅
需求分析:

如何才能访问内网中的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 登录远程主机才能转发特权端口.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值