背景

ServerA(Linux系统):
内网:192.168.111.201
公网:10.121.8.88(虚构的ip方便理解)

ServerB:
内网:192.168.111.202

本机:
安装有SecureCRT软件

注意上图中的箭头。箭头指向可以粗略地认为可以ping通就可以了。

这里为了方便测试,准备了2台虚拟机,ServerA(192.168.111.201)、ServerB(192.168.111.202)
并在ServerB(192.168.111.202)做如下配置,表示ServerB只允许ServerA连接。

# 允许登陆ssh的ip
vi /etc/hosts.allow
sshd:192.168.111.201:allow

# 除了上面允许的,其他的ip都拒绝登陆ssh
vi /etc/hosts.deny
sshd:ALL

ssh安全隧道(一):本地端口转发。
目标:本机直接访问ServerB

ssh安全隧道(二):远程端口转发。内网穿透。
目标:ServerB直接访问本机

一、本地(windows)SecureCRT隧道远程ServerB

远程端口转发
SecureCRT菜单 》 File 》Quick Connect...

使用ssh隧道连接ES securecrt ssh隧道_内网穿透

使用ssh隧道连接ES securecrt ssh隧道_使用ssh隧道连接ES_02

 需要注意的是,这个登录ServerA后,需要一直保持登录状态。 窗口不能关闭。

使用ssh隧道连接ES securecrt ssh隧道_内网穿透_03

这一步配置好后,关闭连接后重新打开。

本地直连ssh登录ServerB。
SecureCRT菜单 》 File 》Quick Connect...

使用ssh隧道连接ES securecrt ssh隧道_端口转发_04

 

使用ssh隧道连接ES securecrt ssh隧道_内网_05

 这是测试的ssh的22端口,其他mysql的3306等端口是一样的

二、本地(windows)SecureCRT隧道方式连接mysql

SecureCRT菜单 》 File 》Quick Connect...

使用ssh隧道连接ES securecrt ssh隧道_内网穿透

使用ssh隧道连接ES securecrt ssh隧道_使用ssh隧道连接ES_02

 需要注意的是,这个登录ServerA后,需要一直保持登录状态。 窗口不能关闭。

使用ssh隧道连接ES securecrt ssh隧道_端口转发_08

 这一步配置好后,关闭连接后重新打开。

本地直连登录ServerB的mysql。

使用ssh隧道连接ES securecrt ssh隧道_内网穿透_09

三、本地(windows)SecureCRT远程端口转发,内网穿透

远程端口转发

原理是把服务器的端口转发到本机的端口上,
比如把服务器的 8081 端口转发给本机的 8080 端口,服务器使用nginx反向代理到 8081 端口绑定域名就可以正常访问本地电脑中web应用了。

本机启动一个web服务如下: 

// http://127.0.0.1:8080/test
    @GetMapping("/test")
    public String test() {
        return "test ok";
    }

本机配置如下:

SecureCRT菜单 》 File 》Quick Connect...

使用ssh隧道连接ES securecrt ssh隧道_内网穿透

这里做本地测试用的内网ip,这里的ip可改为ServerA的公网ip

使用ssh隧道连接ES securecrt ssh隧道_使用ssh隧道连接ES_02

 需要注意的是,这个登录ServerA后,需要一直保持登录状态。 窗口不能关闭。

使用ssh隧道连接ES securecrt ssh隧道_端口转发_12

 这里做本地测试用的内网ip,这里的ip可改为ServerA的公网ip

 这一步配置好后,关闭连接后重新打开。
到这一步,已经可以使用 ServerA的127.0.0.1:8081 访问,本机的 127.0.0.1:8080端口了。
但是只能用127.0.0.1:8081 转发访问,并不能使用ServerA的内网(192.168.111.201:8081)或者外网(10.121.8.88:8081)转发访问,
需要使用nginx将内网(192.168.111.201:8081)或者外网(10.121.8.88:8081)转发到127.0.0.1:8081。127.0.0.1:8081再转发到本机。

 ServerA配置

在ServerA(192.168.111.201)安装Nginx。Linux下安装nginx详细步骤 配置如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8082;
        server_name  localhost;

        location / {
            proxy_pass http://127.0.0.1:8081;
        }
    }
}

本机浏览器访问:http://127.0.0.1:8080/test

ServerA(内网192.168.111.201)访问本机:curl http://127.0.0.1:8081/test
ServerA(公网10.121.8.88)浏览器访问本机:http://10.121.8.88:8082/test

ServerB(192.168.111.202)访问本机:curl http://192.168.111.201:8082/test

真实案例

我有台公网Linux服务器:124.223.x.x:8080

使用ssh隧道连接ES securecrt ssh隧道_内网_13

nginx配置如下 

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8080;
        server_name  localhost;
        location / {
            proxy_pass http://127.0.0.1:8081;
        }
    }
}

本机web服务

// http://127.0.0.1:8080/test
    @GetMapping("/test")
    public String test() {
        return "test ok";
    }

使用ssh隧道连接ES securecrt ssh隧道_使用ssh隧道连接ES_14