使用SSH实现内网穿透

来源:互联网 发布:php垃圾回收机制概述 编辑:程序博客网 时间:2024/05/17 22:25

    最近因为工作原因需要SSH到内网服务器,网上搜了一下,才发现SSH的功能真是强大的多,不仅能解决我的问题,还有其他更NB的功能呢.

    下面是IBM的一篇文章,详细介绍了这些功能,人家写的这么好,我就直接贴出来吧.

    https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/

    其中远程转发功能可以解决内网穿透的问题,本地转发可以突破内网访问的限制,动态转发可以突破内网限制上网.

    想起来几年前接腾讯的支付API,要求只能在腾讯云平台上调用,如果我能早知道本地转发这个功能,就不用费心部署nginx反向代理了.偷笑

    

    实际使用中还有几个参数需要介绍.

    -C 数据压缩,这个不多说了.

    -f Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background.后台执行命令.使用起来会是这个样子:

    localusername@localserver:~$ ssh -f -L 1521:localhost:1521 remoteusername@192.168.20.250
    Cannot fork into background without a command to execute.
    localusername@localserver:~$ ssh -f -L 1521:localhost:1521 remoteusername@192.168.20.250 "echo hello;sleep 5;echo word"
    remoteusername@192.168.20.250's password: [enter password here]
    localusername@localserver:~$ hello word


    这个参数一般配合-N参数一起使用,实现后台运行.

    -N      Do not execute a remote command.  This is useful for just forwarding ports.


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

    本地端口转发时,默认转发端口只允许本地程序访问,使用-g参数才可以接受远程主机连接.这个参数和-L一起使用时才起作用,和-R一起使用时无用.如果公司使用了远程转发,又需要在家里远程登录的,则需要再在VPS上执行本地转发一次,并且使用-g参数.

    实际使用时,经常出现连接断开或无响应的情况,需要设置连接心跳.

    修改~/.ssh/config文件,增加如下内容:

    Host *

    ServerAliveInterval 30

    如果依然有问题,增加如下内容再重试:

    TCPKeepAlive yes

0 0
原创粉丝点击