1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gatewayサーバを経由してDBにアクセスする方法(ポートフォワーディング)

Last updated at Posted at 2015-08-21

概要

  • とあるDBサーバに踏み台を経由して接続したい
  • こんなかんじ
× : local → DB
○ : local → gateway → DB
  • 全体像は分かったけど、実際どうすればいいんだ...?
  • ってことで、やっとこさ出来たのでメモ代わりに

事前準備

  • local → gatewayへsshできるように設定
  • gateway → DBへ接続できるアカウントの作成
  • localhostの開いているportと目的地のportを調べておきます
  • 以下のコマンドを打って、何も出てこなければ未使用です
 lsof -i :port番号

コマンド

  • gateway = 192.168.3.10
  • DB = 192.168.3.11
ssh gateway-user@192.168.3.10 -f -N -L 55439:192.168.3.11:5439
psql -h localhost -p 55439 -U test_user -d Test

 解説

  • gateway-user@192.168.3.10

  • gatewayサーバのユーザ名@gatewayのIP or ドメイン

  • -f

  • バックグラウンドで実行

  • -N

  • トンネルとして接続(ssh先で何もしない)

  • 55439:192.168.3.11:5439

  • 55439 → 自分の使いたいport

  • 192.168.3.11 → DBサーバのIP(目的地)

  • 5439 → 目的地のport

まとめ

  • ポートフォワーディングの設定をして、gatewayサーバを経由し目的地に設定できるようにしました。
localhost:55439 → 192.168.3.10 → 192.168.3.11:5439
  • localの55439が192.168.3.11にひも付けられるってイメージかな?

とりあえず調べてTry&Errorで出来た!って感じなのでもっといい方法とか、簡単な方法とかを見つけたら修正していきます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?