LoginSignup
3
1

More than 3 years have passed since last update.

scpでリモートホスト間でファイル(ディレクトリ)をコピーする

Posted at

目的

異なるリモートホスト間でファイルを転送したい場合、片方のホストに入りもう一方のホストに直接scpコマンドを叩くことができますが、必ずしもリモートホスト間でsshが接続できるとは限りません。
File transferring between two remote hosts

その場合クライアントから以下のようなscpコマンドを叩くことを考えますが
scp hoge@example1.com:~/fuga hoge@example2.com:~/fuga
実際に実行してみるとexample2.comの名前解決に失敗します。

解決策

デフォルトではホスト間でファイル転送することができません。
一旦クライアントにファイルを転送しても良いですが、ファイルサイズが大きい場合はscp -3コマンドを使うことでリモートホスト間のファイル転送ができます

-3オプション

先ほどのコマンドに-3オプションをつけるだけで二つのホスト間でファイルを転送できます
scp -3 hoge@example1.com:~/fuga hoge@example2.com:~/fuga

scpコマンドのmanを開くと
「Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. Note that this option disables the progress meter.」
scp -3 hoge@example1.com:~/fuga hoge@example2.com:~/fuga

手元のホストを通してscpコマンドを実行することができるので大きいファイルでも不必要に認証情報を渡して転送する必要がありません

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