如何使用 Mole 在 Linux 上创建 SSH 隧道
在这篇博文中,我将向您展示如何使用名为 Mole 的好工具轻松创建到远程 Linux 服务器的 SSH 隧道。我了解到,SSH 隧道的一大障碍是创建隧道的复杂性、涉及的命令以及有关该主题的文档较少。对于最终用户来说,掌握 SSH 命令可能很困难,但“我的 SSH 命令备忘单”可以帮助您快速掌握 SSH。
对开源的看法
开源社区对于改变人们对 Linux 及其众多工具的看法非常有帮助。公平地说,Linux 不再被视为极客的“那个东西”,如今,技术经验很少的用户可以使用它并在稍微破坏一些东西的同时学习,哈哈!
鼹鼠简介
我们将用来在 Linux 上创建 SSH 隧道的工具是 Mole。 Mole 是一个用于创建 ssh 隧道的 cli 应用程序,通过 ssh 服务器将本地端口转发到远程端点。
只要用户可以通过 ssh 访问能够访问目标计算机或服务的计算机,Mole 就可以帮助您访问被防火墙阻止的计算机和服务。
+----------+ +----------+ +----------+
| | | | | |
| | | Firewall | | |
| | | | | |
| Local | tunnel +----------+ tunnel | Jump |
| 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 | |
| | | +----+ |
+-------------------+ +--------------------+
鼹鼠的突出特征
- 自动本地地址选择:找到可用的端口并开始监听它,因此不需要在每次运行应用时都提供
-local
标志。 - 别名:将隧道设置保存在别名下,以便以后重复使用。
- 利用 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
sudo ./install.sh
该脚本将在/usr/local/bin
下安装mole,但它需要管理员权限才能部署该文件。它可能需要您输入 sudo 密码。
您可以使用以下命令确认文件位置和版本:
$ which mole
/usr/local/bin/mole
$ mole version
2.0.0
要查看帮助页面,请使用--help
选项
$ mole --help
ool to create ssh tunnels focused on resiliency and user experience.
Usage:
mole [command]
Available Commands:
add Adds an alias for a ssh tunneling configuration
delete Deletes an alias for a ssh tunneling configuration
help Help about any command
misc A set of miscelaneous commands
show Shows configuration details about ssh tunnel aliases
start Starts a ssh tunnel
stop Stops an instance of mole
version Prints the version for mole
Flags:
-h, --help help for mole
Use "mole [command] --help" for more information about a command.
如何使用 Mole 创建 SSH 隧道
让我们看几个例子来了解如何使用 Mole 创建 SSH 隧道
#1:提供所有支持的选项
使用以下详细信息创建隧道:
- 在本地主机端口8080上
- 使用别名
tunnel1
- 到网络服务器 IP 192.168.130.11:80
- 远程 ssh 用户是jkmutai,使用的 SSH 服务器是192.168.207.2:22
- 身份验证方法是公共/私有 SSH 密钥
- SSH 公钥位置
~/.ssh/id_rsa.pub
- SSH 私钥
~/.ssh/id_rsa
您需要将公共 ssh 密钥复制到用于隧道的远程 SSH 服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub [email
通过 SSH 服务器 192.168.207.2:22 ssh 到服务器 192.168.130.11:80 导入远程 SSH 服务器指纹,我们将执行以下命令:
ssh -i ~/.ssh/id_rsa [email
创建到我们的 Web 服务器的隧道
mole start local \
--verbose \
--source 127.0.0.1:8080 \
--destination 192.168.130.11:80 \
--server [email :22 \
--key ~/.ssh/id_rsa
对于可用的 start
命令标志视图:
mole start local --help
输出命令:
INFO[0000] instance identifier is cbd5bb88
DEBU[0000] generating an empty config struct
DEBU[0000] server: [name=192.168.207.2, address=192.168.207.2:22, user=jkmutai]
DEBU[0000] tunnel: [channels:[[source=127.0.0.1:8080, destination=192.168.130.11:80]], server:192.168.207.2:22]
DEBU[0000] known_hosts file used: /home/jkmutai/.ssh/known_hosts
DEBU[0000] connection to the ssh server is established server="[name=192.168.207.2, address=192.168.207.2:22, user=jkmutai]"
INFO[0000] tunnel channel is waiting for connection destination="192.168.130.11:80" source="127.0.0.1:8080"
DEBU[0000] start sending keep alive packets
另一个例子:
mole start local \
--verbose \
--source 192.168.200.8:8080 \
--destination 192.168.200.5:9090 \
--server [email :22 \
--key ~/.ssh/id_rsa
创建别名
让我们考虑使用别名来保存配置的示例。
mole add alias local tunnel1 \
--verbose \
--source 192.168.200.8:8080 \
--destination 192.168.200.5:9090 \
--server [email :22 \
--key ~/.ssh/id_rsa
列出创建的别名:
$ mole show alias
[aliases]
[aliases.tunnel1]
name = "tunnel1"
type = "local"
verbose = true
insecure = false
detach = false
source = ["192.168.200.8:8080"]
destination = ["192.168.200.5:9090"]
server = "[email :22"
key = "/home/jkmutai/.ssh/id_rsa"
keep-alive-interval = "10s"
connection-retries = 3
wait-and-retry = "3s"
ssh-agent = ""
timeout = "3s"
config = "$HOME/.ssh/config"
rpc = false
rpc-address = "127.0.0.1:0"
要启动隧道,请使用
$ mole start alias tunnel1
INFO[0000] instance identifier is tunnel1
DEBU[0000] generating an empty config struct
DEBU[0000] server: [name=192.168.200.5, address=192.168.200.5:22, user=jkmutai]
DEBU[0000] tunnel: [channels:[[source=192.168.200.8:8080, destination=192.168.200.5:9090]], server:192.168.200.5:22]
DEBU[0000] known_hosts file used: /home/jkmutai/.ssh/known_hosts
DEBU[0000] connection to the ssh server is established server="[name=192.168.200.5, address=192.168.200.5:22, user=jkmutai]"
INFO[0000] tunnel channel is waiting for connection destination="192.168.200.5:9090" source="192.168.200.8:8080"
DEBU[0000] start sending keep alive packets
#2:使用 ssh 配置文件查找给定的服务器主机
您可以将远程SSH服务器的ssh配置添加到~/.ssh/config
,然后在运行mole时调用其名称。
$ vim ~/.ssh/config
Host myserver
User mole
Hostname 127.0.0.1
Port 2022
IdentityFile /path-to-ssh-priv-key
然后创建隧道
mole start local \
--verbose \
--source 127.0.0.1:8080 \
--destination 172.17.0.100:80 \
--server myserver
#3:让mole随机选择本地端点
您应该已经注意到,我们通过选项 -local 127.0.0.1:8080
指定了本地端口 8080
用于隧道流量。 Mole 可以为您随机选择未使用的 TCP 端口。
为此,示例2将更改为
mole start local \
--destination 172.17.0.100:80 \
--server myserver
将打印出要访问的本地地址
INFO[0000] tunnel channel is waiting for connection destination="172.17.0.100:80" source="127.0.0.1:40525"
确认您可以使用隧道访问远程服务器上的 Web 服务器。
#4:通过仅指定源端口将本地地址绑定到127.0.0.1
考虑下面的例子:
mole start local \
--source :8080 \
--destination 172.17.0.100:80 \
--server myserver
#5:通过仅指定目标端口连接到在 127.0.0.1 上运行的远程服务
请参阅下面共享的命令:
mole start local \
--source 127.0.0.1:8080 \
--destination :80 \
--server myserver
#6:创建别名
使用别名后无需记住隧道设置。见下文:
$ mole add alias local myalias \
--source :8080 \
--destination 172.17.0.100:80 \
--server user@example.com:22
$ mole start alias myalias
#7:在后台启动鼹鼠
请参阅下面的示例:
$ mole add alias local example \
--source :8080 \
--destination 172.17.0.100:80 \
--server [email :22
$ mole start alias example --detach
#8:利用 SSH 配置文件中的 LocalForward
参考下面分享的配置:
$ vim ~/.ssh/config
Host example
User mole
Hostname 127.0.0.1
Port 22122
LocalForward 21112 192.168.33.11:80
IdentityFile test-env/ssh-server/keys/key
$ mole start local --server example
INFO[0000] tunnel channel is waiting for connection destination="192.168.33.11:80" source="127.0.0.1:21112"
#9:使用单个 ssh 连接创建多个隧道
考虑这个例子:
$ mole start local \
--source :9090 \
--source :9091 \
--destination 192.168.33.11:80 \
--destination 192.168.33.11:8080 \
--server example
INFO[0000] tunnel channel is waiting for connection destination="192.168.33.11:8080" source="127.0.0.1:9091"
INFO[0000] tunnel channel is waiting for connection destination="192.168.33.11:80" source="127.0.0.1:9090"
#10:显示任何分离的鼹鼠实例的日志
让我们考虑一个例子:
$ mole start local \
--detach \
--source :9090 \
--source :9091 \
--destination 192.168.33.11:80 \
--destination 192.168.33.11:8080 \
--server example
INFO[0000] instance identifier is afb046da
INFO[0000] execute "mole stop afb046da" if you like to stop it at any time
$ mole show logs --follow afb046da
time="2021-09-17T13:57:10-07:00" level=info msg="instance identifier is 1879de9f"
time="2021-09-17T13:57:10-07:00" level=debug msg="generating an empty config struct"
time="2021-09-17T13:57:10-07:00" level=debug msg="server: [name=127.0.0.01, address=127.0.0.01:22122, user=mole]"
time="2021-09-17T13:57:10-07:00" level=debug msg="tunnel: [channels:[[source=127.0.0.1:9888, destination=192.168.33.11:80]], server:127.0.0.01:22122]"
time="2021-09-17T13:57:10-07:00" level=debug msg="connection to the ssh server is established" server="[name=127.0.0.01, address=127.0.0.01:22122, user=mole]"
time="2021-09-17T13:57:10-07:00" level=info msg="tunnel channel is waiting for connection" destination="192.168.33.11:80" source="127.0.0.1:9888"
time="2021-09-17T13:57:10-07:00" level=debug msg="start sending keep alive packets"
#11: 显示所有/任意mole实例的运行配置
使用命令:
mole show instances
mole show instances <ID>
我希望这几个示例能够帮助您创建通往远程服务器的 SSH 隧道,该服务器可能位于防火墙后面。您可以在 Github 上为该项目做出贡献。