在Linux CLI上创建SSH隧道的简便方法

时间:2020-02-23 14:38:04  来源:igfitidea点击:

在这篇博客文章中,将介绍如何轻松地创建到远程服务器的SSH隧道。

Mole简介

该工具很好地用于在Linux isMole上创建SSH隧道。 Mole是用于创建ssh隧道的cli应用程序,可通过ssh服务器将本地端口转发到远程端点。

只要用户可以通过ssh访问具有目标计算机或者服务访问权限的计算机,Mole便可以访问被防火墙阻止的计算机和服务。

+----------+          +----------+          +----------+
|          |          |          |          |          |
|          |          | Firewall |          |          |
|          |          |          |          |          |
|  Local   |  tunnel  +----------+  tunnel  |          |
| Computer |--------------------------------|  Server  |
|          |          +----------+          |          |
|          |          |          |          |          |
|          |          | Firewall |          |          |
|          |          |          |          |          |
+----------+          +----------+          +----------+
                                                 |
                                                 |
                                                 | tunnel
                                                 |
                                                 |
                                            +----------+
                                            |          |
                                            |          |
                                            |          |
                                            |          |
                                            |  Remote  |
                                            | Computer |
                                            |          |
                                            |          |
                                            |          |
                                            +----------+

使用Mole,我们还可以在端口转发的帮助下访问仅在本地地址上侦听的服务。了解如何在带有Firewalld的CentOS 7上创建端口转发。

另外,请注意,"服务器和远程"计算机可能是同一台计算机。

+-------------------+             +--------------------+
| Local Computer    |             | Remote/Server    |
|                   |             |                    |
|                   |             |                    |
| (172.17.0.10:     |    tunnel   |                    |
|        50001)     |-------------| (172.17.0.100:22)  |
|  tunnel client    |             |  tunnel server     |
|       |           |             |         |          |
|       | port      |             |         | port     |
|       | forward   |             |         | forward  |
|       |           |             |         |          |
| (127.0.0.1:3306)  |             | (127.0.0.1:50000)  |
|  local address    |             |         |          |
|                   |             |         | local    |
|                   |             |         | conn.    |
|                   |             |         |          |
|                   |             | (127.0.0.1:3306)   |
|                   |             |  remote address    |
|                   |             |      +----+        |
|                   |             |      | DB |        |
|                   |             |      +----+        |
+-------------------+             +--------------------+

Mole的突出特点

自动本地地址选择:找到可用的端口并开始监听它,因此不需要在每次运行该应用程序时都给出本地标志别名:将隧道设置保存为别名,以便以后可以重用利用SSH配置文件:尽可能使用$HOME/.ssh/config中指定的某些选项(例如用户名,身份密钥和端口),因此无需在多个位置进行相同的SSH服务器配置。

如何在Linux/macOS上安装Mole

在Linux上通过安装脚本安装Mole,可以通过curl下载。

curl -O https://raw.githubusercontent.com/davrodpin/mole/master/tools/install.sh

脚本下载后,使其可执行,然后安装

chmod +x install.sh
./install.sh

该脚本将在/usr/local/bin目录下安装mole,但是它需要管理员权限才能部署文件。可能需要我们输入sudo密码。

我们可以使用以下命令确认文件位置和版本:

$which mole
/usr/local/bin/mole

$mole --version
mole 0.2.0

要查看帮助页面,请使用--help选项。

$ mole help
usage:
  mole [-v] [-local [<host>]:<port>] remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
  mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
  mole -alias <alias_name> -delete
  mole start <alias_name>
  mole -help
  mole version

  -alias string
        Create a tunnel alias
  -delete
        delete a tunnel alias (must be used with -alias)
  -help
        list all options available
  -key string
        (optional) Set server authentication key file path
  -local value
        (optional) Set local endpoint address: [<host>]:<port>
  -remote value
        set remote endpoing address: [<host>]:<port>
  -server value
        set server address: [<user>@]<host>[:<port>]
  -start string
        Start a tunnel using a give alias
  -v    (optional) Increase log verbosity
  -version
        display the mole version

如何使用Mole创建SSH隧道

让我们看一些有关如何使用Mole创建SSH隧道的示例

示例1:提供所有受支持的选项

创建具有以下详细信息的隧道:在localhost端口8080上,别名为tunnel1到服务器IP192.168.18.50:80远程ssh用户isvagrant,使用的SSH服务器是192.168.18.51:22身份验证方法是Public/Private SSH keysSSh Public key location~/.ssh/test_rsa.pubSSH私钥~/.ssh/test_rsa

我们需要将Public ssh密钥复制到用于隧道的远程SSH服务器

$ssh-copy-id -i ~/.ssh/test_rsa.pub theitroad@localhost

通过对服务器执行SSH来导入远程SSH服务器指纹

$ssh -i ~/.ssh/test_rsa.pub theitroad@localhost

现在创建一个隧道:

$mole -alias tunnel1 -v -local 127.0.0.1:8080 \
-remote 192.168.18.50:80 \
-server theitroad@localhost \
-key ~/.ssh/test_rsa

要启动隧道,请使用

$mole -start tunnel1

输出示例:

$  mole -start tunnel1

DEBU[0000] cli options                                   options="[local=127.0.0.1:8080, remote=192.168.18.50:80, [email protected], key=/home/jmutai/.ssh/test_rsa, verbose=true, help=false, version=false]"
DEBU[0000] server: [name=192.168.18.51, address=192.168.18.51:22, user=vagrant, key=/home/jmutai/.ssh/test_rsa]
DEBU[0000] tunnel: [local:127.0.0.1:8080, server:192.168.18.51:22, remote:192.168.18.50:80]
INFO[0000] listening on local address                    local_address="127.0.0.1:8080"
DEBU[0017] new connection                                address="127.0.0.1:36908"
DEBU[0017] known_hosts file used: /root/.ssh/known_hosts
DEBU[0019] new connection established to server          server="[name=192.168.18.51, address=192.168.18.51:22, user=vagrant, key=/home/jmutai/.ssh/test_rsa]"
DEBU[0024] new connection established to remote          remote="192.168.18.50:80" server="[name=192.168.18.51, address=192.168.18.51:22, user=vagrant, key=/home/jmutai/.ssh/test_rsa]"
DEBU[0024] new connection                                address="127.0.0.1:36910"
DEBU[0024] known_hosts file used: /root/.ssh/known_hosts
DEBU[0026] new connection established to server          server="[name=192.168.18.51, address=192.168.18.51:22, user=vagrant, key=/home/jmutai/.ssh/test_rsa]"
DEBU[0029] new connection established to remote          remote="192.168.18.50:80" server="[name=192.168.18.51, address=192.168.18.51:22, user=vagrant, key=/home/jmutai/.ssh/test_rsa]

示例2:使用ssh配置文件查找给定的服务器主机

我们可以将远程SSH服务器的ssh配置添加到~/.ssh/config,然后在运行mole时调用其名称。

$cat .ssh/config
Host rserver1
  Hostname 192.168.18.51
  User vagrant
  Port 22
  IdentityFile ~/.ssh/test_rsa

然后创建一个隧道

$mole -alias tunnel1 -v -local 127.0.0.1:8080 \
-remote  192.168.18.50:80 \
-server rserver1

我们也可以使用:8080而不是127.0.0.1:8080

$mole -alias tunnel1 -v -local :8080 \
-remote 192.168.18.50:80 \
-server rserver1

$mole -start tunnel1

远程服务器端口相同

$mole -alias tunnel1 -v -local 127.0.0.1:8080 \
-remote :80 \
-server rserver1

示例3:让mole随机选择局部终点

我们应该已经注意到,我们使用选项"本地127.0.0.1:8080"指定了本地端口" 8080"用于隧道传输流量。 Mole可以为我们随机选择未使用的TCP端口。

为此,example2将更改为

$mole -alias tunnel1 -remote 192.168.18.50:80 -server rserver1
$mole -start tunnel1

将打印出要访问的本地地址

INFO listening on local address local_address="127.0.0.1:36683"

确认我们可以使用隧道访问远程服务器上的Web服务器。

要删除SSH隧道,请运行:

$mole -delete -alias tunnel1