2

我正在尝试在运行服务器端口的数据库和另一台服务器之间创建一个 SSH 隧道,如下所示。

MySQL:3306 <=====> Server-A:3306

我想使用Server-A:3306作为数据库 URI 来连接到数据库。

我在 ServerA 上运行以下命令

ssh -f -N -i ~/keys/test.pem foo@foo.storesandbox.com -L5001:127.0.0.1:2001

我可以看到隧道已启动并正在运行。但是当我使用 Server-A 的公共 IP 并尝试连接到数据库时,它不起作用。

如果我在 Server-A 和我运行 MySQL 客户端的位置之间创建另一个隧道。然后它工作。但我不想那样做。

这个问题的原因可能是什么。我对脚本相当陌生

4

1 回答 1

2

默认情况下,当您使用这样的命令时,本地端(ssh 客户端)会在地址为 127.0.0.1 的环回接口上创建侦听端口

ssh me@server -L3306:localhost:3306

如果您检查主机上的 netstat,您会看到类似这样的内容

sudo netstat -ntlp | grep 3306
tcp  0 0 127.0.0.1:3306  0.0.0.0:*   LISTEN 12354/ssh

因此,本地节点上的应用程序可以连接到此类映射服务,因为环回接口对主机本身可见,但外部节点无权访问此虚拟接口,因此无法与正在侦听此单个接口的任何服务(端口)建立任何连接.

要指示本地 ssh 客户端与世界共享此类映射端口,您需要指示它绑定到所有接口(包括环回)或仅绑定到特定接口

# here you explicitly tell ssh client to accept connection to your tunnel
# from any client(i.e. bind listenning port to all interfaces)
ssh me@server -L0.0.0.0:3306:localhost:3306 
sudo netstat -ntlp | grep 3306 
tcp  0 0 0.0.0.0:3306   0.0.0.0:*   LISTEN 12354/ssh

#here you do the same thing by using -g option
ssh me@server -g -L3306:localhost:3306
sudo netstat -ntlp | grep 3306 
tcp  0 0 0.0.0.0:3306   0.0.0.0:*   LISTEN 12354/ssh

#and here is an example of how to bind to specific interfaces only
# 10.0.0.12 is an IP of one of interfaces on your node
# 10.1.0.156 is also IP address of one interfaces of your node
ssh me@server -L10.0.0.12:3306:localhost:3306 -L10.1.0.156:3306:localhost:3306

sudo netstat -ntlp | grep 3306
tcp  0 0 10.0.0.12:3306   0.0.0.0:*   LISTEN 12354/ssh
tcp  0 0 10.1.0.156:3306  0.0.0.0:*   LISTEN 12354/ssh

于 2020-04-20T06:34:44.797 回答