簡體   English   中英

創建從 Jump 主機到數據庫的 SSH 隧道,並在 Jump Host 端口上訪問數據庫

[英]Create a SSH tunnel from a Jump host to a Database and access the database on Jump Host Port

我正在嘗試在數據庫運行服務器端口和另一台服務器之間創建 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並嘗試連接數據庫時,它不起作用。

如果我在服務器 A 和運行 MySQL 客戶端的位置之間創建另一個隧道。 然后它工作。 但我不想那樣做。

這個問題的原因可能是什么。 我對腳本相當陌生

默認情況下,當您使用這樣的命令時,本地端(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM