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

多段ssh先をコンフィグに記述して簡単にログインし、scpでファイルコピーする

0
Posted at

目的

linuxサーバにsshする時、外部からアクセスできない環境の時は踏み台サーバを経由してログインする例が多いと思います。

毎回、以下のようなコマンドを入力して踏み台経由でログインすることは面倒だし、どのサーバで作業しているか間違えるトラブルの元にもなるので、ログイン先のサーバをsshのコンフィグに記述しておくと便利です。
ssh -i "xxxxxxx.pem" ubuntu@ec2-12-345-678-90.ap-northeast-1.compute.amazonaws.com

手順

ローカル端末 → bastion → bastion-2 → internal-serverのように3段でsshする場合です。
.ssh/configに以下のように記述します。

Host bastion
  HostName bastion.com
  User user
  IdentityFile ~/.ssh/id_rsa

Host bastion-2
  HostName 10.1.2.3
  User user
  IdentityFile ~/.ssh/id_rsa
  ProxyCommand ssh -CW %h:%p bastion 2> /dev/null

Host internal-server
  HostName 172.30.1.2
  User internal-user
  IdentityFile ~/.ssh/internal-server.pem
  ProxyCommand ssh -CW %h:%p bastion-2 2> /dev/null

この例ではIdentityFileを指定していますが、IdentityFileは全てローカル端末に保管しておく必要がありますのでパーミッションに気をつけてください。
ProxyCommandの指定は、本来の接続先であるホスト%h(internal-server)とそのポート%pへはbastion-2を経由してsshし、標準エラー出力をファイル「/dev/null」に書き出すという動作になります。

結果

上記の場合、ssh internal-serverの記述のみでローカル端末から直接internal-serverへのログインが可能になります。

また、scp internal-server:./Copy source/* /Users/Copy destinationと記述することで一度にinternal-serverにファイルコピー(この場合はinternal-serverからローカル端末へのコピー)をすることができます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?