LoginSignup
1
3

More than 1 year has passed since last update.

[SSH] 踏み台を経由して一発でSSHする

Last updated at Posted at 2021-12-12

やりたいこと

リモートサーバへSSHアクセスする際に、
踏み台のサーバに入らずに、一発で目的のサーバに入る

- 例(通常) MyPC > 踏み台サーバ > 目的サーバ
- 例(やりたい事) MyPC > > 目的サーバ

(myPC) $ ssh hostname

通常SSHで目的サーバにアクセスする時

目的のサーバに辿り着く時は、踏み台からのアクセスしか許可していない為、
踏み台のサーバを経由する必要がある。

(myPC) $ ssh -i 踏み台サーバ.pem -p2222 xxx.xxx.xx.xx //踏み台IP
(踏み台) $ ssh ec2-user@xxx.xxx.xx.xx //目的サーバIP
(目的サーバ) $ hostname //目的サーバか確認

環境

  • macOS
  • zsh

構成

$ pwd
/Users/myname/.ssh

$ tree
.
├── conf.d
│   └── hosts
│       ├── ashiba
│       ├── hostnameA
│       └── hostnameB
├── config
├── keys 
│   ├── ashiba.pem
│   ├── hostnameA.pem
│   └── hostnameB.pem



<configファイルを作成して、ローカルから一発でアクセスする>

ローカルにconfigファイルを作成して、これを利用しsshコマンドを簡潔にし、一発で多段階アクセスを可能にする。
設定ファイルは、「.ssh」配下に配置。
./ssh/config ファイルがない場合は、新規で作成。

1.ディレクトリ作成

  • conf.d/hosts | 設定ファイルを記述するディレクトリ
  • keys | 秘密鍵を置くディレクトリを作成
$ cd ~/.ssh 
$ mkdir conf.d/hosts/ && mkdir keys

&& は左の処理が成功したら、右の処理を実行という意味。

2.configファイルを作成

$ vi config

config

「conf.d/hosts/(全て)」の設定ファイルを、読み込むようにする。
※ひとつのファイルに記述すると、長くなるのでドメインごとに分ける。

Include conf.d/hosts/*

3.conf.d/hosts/ 配下に各サーバの設定ファイルを作成

  • (1)踏み台サーバの情報を作成
$ cd conf.d/hosts/
$ vi ashiba

ashiba

# -----------------------------
# ashiba
# -----------------------------
Host ashiba
  Hostname      xxx.xxx.xx.xx
  Port  2222
  User  username
  IdentityFile ~/.ssh/keys/ashiba.pem
  • (2)同じように、目的サーバAの情報を作成
$ vi hostnameA

hostnameA

# -----------------------------
# hostnameA
# -----------------------------
Host hostnameA
  Hostname      xxx.xxx.xx.xx
  Port  22
  User  ec2-user
  ProxyCommand ssh ashiba -W %h:%p
  IdentityFile ~/.ssh/keys/hostnameA.pem
  • (3)目的サーバBの情報を作成
$ vi hostnameB

hostnameB

hostnameBは、hostnameAを経由するものとする。
myPC > ashiba > hostnameA > hostnameB

# -----------------------------
# hostnameB
# -----------------------------
Host hostnameB
  Hostname      xxx.xxx.xx.xx
  Port  22
  User  ec2-user
  ProxyCommand ssh hostnameA -W %h:%p
  IdentityFile ~/.ssh/keys/hostnameB.pem

プロパティ 説明
Host 任意の名前
Hostname ホスト名 or IPアドレス
Port ポート番号
IdentityFile 秘密鍵のパスを指定
ProxyCommand 指定されたHostを経由しredirectするコマンドを指定

4.指定したパスに秘密鍵を設置

IdentityFile ~/.ssh/keys/***.pem」で指定した位置に、秘密鍵を設置。
作成後、パーミッションも変更。

$ cd ~/.ssh/keys

$ vi ashiba.pem
$ vi hostnameA.pem
$ vi hostnameB.pem

$ chmod 400 * //全てのファイルを読み取り専用にパーミッション変更

5.SSHしてみる

Host **で指定した名前でアクセス。

hostnameAへ

$ ssh hostnameA

念の為、踏み台サーバを経由しているか確認

(hostname1) $ last | head
ec2-user pts/1        ec2-18-182-xxx-x Sun Dec 12 22:12   still logged in

last コマンドでログイン履歴を確認すると、踏み台サーバのIPが返ってきたので、
踏み台サーバをしっかり経由している。

hostnameBへ

2段階SSH ashiba > hostnameA > hostnameB

$ ssh hostnameB

ログイン履歴を確認

(hostname1) $ last | head
ec2-user pts/1        ec2-18-xx-xxx-x Sun Dec 12 22:15   still logged in

hostnameAのサーバが返ってきた。これで多段階SSHが可能に。

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