LoginSignup
0
1

More than 1 year has passed since last update.

プライベートなEC2にローカルPCからアクセスする

Posted at

はじめに

Privateサブネット内で起動されるEC2インスタンスにクライアントPCからログインする機会があったため、備忘録として残しておこうと思います。

環境情報

  • ローカルPC:Windows 10 Pro
  • EC2:Amazon Linux2

構成図

Publicサブネット内のEC2インスタンスを経由してPrivateサブネット内のEC2インスタンスにアクセスします。
image.png

手順

~/.ssh/configにPublicインスタンスとPrivateインスタンスの接続情報を記述します。

~/.ssh/config
Host instance1
    # 踏み台サーバのパブリックIPを設定
    HostName xxx.xxx.xxx.xxx
    User ec2-user
    # 踏み台サーバの秘密鍵のPath
    IdentityFile =~/path/instance1.pem
    Port 22

Host instance2
    # プライベートサーバのプライベートIPを設定
    HostName xxx.xxx.xxx.xxx
    User ec2-user
    # 踏み台サーバを経由
    ProxyCommand ssh instance1 -W %h:%p
    IdentityFile =~/.ssh/path/instance2.pem
    Port 22

・コマンドプロンプトから以下のコマンドを実行し、プライベートサーバへアクセスする。

ssh instance1

最後に

今回は.ssh/configを利用してローカルPCからプライベートインスタンスへアクセスする方法を記載しました。
MySQL Workbenchでprivateサブネット内のDBインスタンスに接続する際も.ssh/configが利用できそうなので、別の機会でまとめようかと思います。

参考サイト

https://s10i.me/whitenote/post/42
https://dev.classmethod.jp/articles/bastion-multi-stage-ssh-only-local-pem/

0
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
0
1