scp, rsync コマンドとは?オプションやサーバー間のコピーを解説

scp (Secure Copy)scp とは、リモートファイルコピープログラムです。
rsyncrsync とは、リモートファイルコピープログラムです。

つまり scp と rsync は同じことができます。

rsync -av user@192.0.2.2:~/directory ./

Permission denied (publickey,gssapi-keyex,gssapi-with-mic)が発生する場合

ssh で利用する秘密鍵を正しく指定出来ていない可能性があります。

そのため、OpenSSH authentication agent に秘密鍵のパスを追加しましょう。

ssh-add /path_to_privatekey.pem

※ /path_to_privatekey.pem は秘密鍵のパスです

本記事では、rsync や scp のオプションの使い方を解説していきます。

なお scp の開発チームは、scp が時代遅れなので、rsync を使うことを推奨しています。

The scp protocol is outdated, inflexible and not readily fixed. We recommend the use of more modern protocols like sftp and rsync for file transfer instead.

https://www.openssh.com/txt/release-8.0
関連記事:ネットワークのコマンド
スポンサーリンク

rsync の使い方

ローカルからリモートにコピー

ローカルの local.txt を、ユーザー (user) でリモート (192.0.2.2) の ~/ にコピーします。

rsync local.txt user@192.0.2.2:~/

Permission denied (publickey,gssapi-keyex,gssapi-with-mic)が発生する場合

ssh で利用する秘密鍵を正しく指定出来ていない可能性があります。

そのため、OpenSSH authentication agent に秘密鍵のパスを追加しましょう。

ssh-add /path_to_privatekey.pem

※ /path_to_privatekey.pem は秘密鍵のパスです

リモートからローカルにコピー

リモート (192.0.2.2) の ~/remote.txtローカルの ./ にコピーします。

rsync user@192.0.2.2:~/remote.txt ./

-r ディレクトリをコピー

リモート (192.0.2.2) の ~/directoryローカルの ./ にコピーします。

rsync -r user@192.0.2.2:~/directory ./

-v ログの情報量を増やす

rsync -v user@192.0.2.2:~/remote.txt ./
remote.txt

sent 38 bytes  received 95 bytes  266.00 bytes/sec
total size is 0  speedup is 0.00

-z 送信時にファイルを圧縮

rsync -z user@192.0.2.2:~/remote.txt ./

-a ほぼ全てをコピー

以下の機能を持つオプション -rlptgoD を同じ意味。

rsync -a user@192.0.2.2:~/directory ./

-n (--dry-run) ドライラン

ドライランは実際にはコピーしません。(コマンドの構文確認などで利用します。)

rsync -n user@192.0.2.2:~/remote.txt ./

--exclude 除外

rsync -r --exclude={test2.txt,test3.txt} user@192.0.2.2:~/directory ./
スポンサーリンク

scp の使い方

非推奨とはいえ、scp の使い方も一応載せておきます。

ローカルからリモートにコピー

ローカルの local.txt を、ユーザー (user) でリモート (192.0.2.2) の ~/ にコピーします。

scp local.txt user@192.0.2.2:~/

Permission denied (publickey,gssapi-keyex,gssapi-with-mic)が発生する場合

ssh で利用する秘密鍵を正しく指定出来ていない可能性があります。

そのため、OpenSSH authentication agent に秘密鍵のパスを追加しましょう。

ssh-add /path_to_privatekey.pem

※ /path_to_privatekey.pem は秘密鍵のパスです

リモートからローカルにコピー

リモート (192.0.2.2) の ~/remote.txtローカルの ./ にコピーします。

scp user@192.0.2.2:~/remote.txt ./
スポンサーリンク

関連記事

関連記事:ネットワークのコマンド