LoginSignup
1
1

More than 1 year has passed since last update.

踏み台サーバの公開鍵を使用せずに踏み台サーバ経由の SSH 接続をする(VS Code でも使用可能)

Posted at

はじめに

今回は、踏み台サーバ経由で別の LAN に接続されたマシンに SSH 接続をする設定方法をご紹介します。
前回の Ubuntu 20.04 LTS で NIC(ネットワークインターフェース)を2つ同時に使用する方法 の記事の構成の話の延長です。
今までは 10.11.2.0/24 上のみに存在するマシンだと 10.11.2.0/24 のネットワークに VPN などで接続する必要がありましたが、前回のマシンを踏み台にすることによって直接外部から触ろうという試みです。

構成

nic-wlan3.png

前回 LAN ケーブルを2本差ししたマシンの名称は Bastion(要塞)、10.11.2.0/24 上にある接続したいマシンを Client とします。
あまり関係ないですが、Bastion の OS は Ubuntu 20.04.3 LTS、Client の OS は Rocky Linux 8.4 です。

準備

接続したいマシンの公開鍵を事前に Bastion と Client に登録しておきます。踏み台サーバを使いたいなと思う方なら SSH キーの発行や登録方法はわかると思いますので省略します。

方法

/Users/[user]/.ssh/config(Windows なら C:\Users\[user]\.ssh\config) のファイルを以下のように編集します([user] はご自身のユーザ名です)。

/Users/[user]/.ssh/config
Host bastion
  HostName 192.168.0.3
  User root
  Port 22
  IdentityFile /Users/[user]/.ssh/id_rsa
  ForwardAgent yes

Host Client # Windows の場合。Mac / Linux の場合は下記
  Hostname 10.11.2.8
  Port 22
  User root
  ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -q -W %h:%p bastion # bastion は上で設定した Host の名称を使用
  IdentityFile /Users/[user]/.ssh/id_rsa

Host Client # Mac / Linux の場合
  Hostname 10.11.2.8
  Port 22
  User root
  ProxyCommand ssh -q -W %h:%p bastion
  IdentityFile /Users/[user]/.ssh/id_rsa

VS Code のエクスプローラーも使用可能

Remote-SSH のプラグインが入っていれば、例の左下の緑の SSH のボタンで Client に接続するとエクスプローラーも使用可能です。

終わりに

私がハマったのは、Windows の場合は ProxyCommand のところで ssh.exe のフルパスを書かないといけないことでした。
特に Windows 使いの方で、ハマっている人が居て救われれば幸いです。

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