0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ProxyJumpで多段接続を試してみた

Posted at

背景・目的

踏み台サーバに秘密鍵を置かずに、プライベートサブネット内のサーバへSSH接続する方法をまとめます。

実践

下記のような構成で試します。

 ローカルPC
     |
     | SSH (鍵A)
     v
 [Bastion Host] (パブリックサブネット)
     |
     | SSH (鍵B)
     v
 [Private EC2] (プライベートサブネット)

準備

VPC

  1. AWSにサインインします
  2. VPCをクリックします
  3. 「VPCを作成」をクリックします
  4. 下記を作成します
    • Public Subnet✕1
    • Private Subnet✕1
    • NAT Gateway
    • Internet Gateway

EC2の作成

  1. EC2に移動します
  2. 下記のインスタンスを起動します。起動時にはそれぞれキーペアを作成します
Bastion Private
目的 踏み台 プライベートサーバ
パブリックIP Yes No
サブネット パブリックサブネット プライベートサブネット
セキュリティグループ 接続元のIP 踏み台サーバのセキュリティグループ

SSH Configの設定

  1. EC2作成時に作成したキーペア(秘密鍵)を.ssh/に配置し、パーミションを600に変更します
  2. ~/.ssh/ssh_configを設定します
    Host bastion
      HostName <踏み台のPublicIP>
      User ec2-user
      IdentityFile ~/.ssh/id_rsa_bastion
    
    Host internal
      HostName <プライベートEC2のPrivateIP>
      User ec2-user
      IdentityFile ~/.ssh/id_rsa_internal
      ProxyJump bastion
    

アクセス

踏み台サーバへSSH

  1. PCからbastionにアクセスします。アクセスできました
    % ssh bastion      
       ,     #_
       ~\_  ####_        Amazon Linux 2023
      ~~  \_#####\
      ~~     \###|
      ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
       ~~       V~' '->
        ~~~         /
          ~~._.   _/
             _/ _/
           _/m/'
    Last login: Thu Apr 10 13:52:25 2025 from XXXXX
    [ec2-user@ip-XXXXX ~]$ 
    

プライベートサーバへSSH

  1. PCからプライベートサーバにアクセスします。できました

    % ssh internal
       ,     #_
       ~\_  ####_        Amazon Linux 2023
      ~~  \_#####\
      ~~     \###|
      ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
       ~~       V~' '->
        ~~~         /
          ~~._.   _/
             _/ _/
           _/m/'
    Last login: Thu Apr 10 13:56:33 2025 from XXXXXX
    [ec2-user@ip-YYYYY ~]$ 
    

考察

以前は、ProxyCommandで接続していましたが、今回はProxyJumpを使用してみました。
設定もシンプルになりまし

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?