Linuxの管理者はコマンドライン環境に精通しているべきです。なぜなら、GUI(グラフィカルユーザーインターフェース)モードはLinuxサーバーに一般的にインストールされていません。
SSHは、Linux管理者がリモートで安全にサーバーを管理できるようにするための最も人気のあるプロトコルかもしれません。SSHコマンドと共に、SCPコマンドがあり、これはセキュアな方法でサーバー間でファイル(複数可)をコピーするために使用されます。
SCPコマンドの基本構文
以下のコマンドは、「ソースファイル名」を「宛先フォルダ」に「宛先ホスト」で「ユーザー名」アカウントを使用してコピーすることを意味します。
scp source_file_name username@destination_host:destination_folder
SCPコマンドには多くのパラメータがありますが、日常的に使用する可能性があるパラメータを以下に示します。
目次
Linuxでの安全なファイル転送
基本のSCPコマンドはパラメータなしでバックグラウンドでファイルをコピーします。プロセスが完了するか、エラーが表示されるまで、ユーザーは何も見られません。
画面にデバッグ情報を出力する“-v
”パラメータを使用できます。これは、接続、認証、および設定の問題をデバッグするのに役立ちます。
ローカルホストからリモートサーバーへのファイルコピー
次のコマンドは、ファイル“scp-cheatsheet.pdf”をローカルからリモートLinuxシステムの/home/tecmintディレクトリにコピーします。
$ scp -v scp-cheatsheet.pdf [email protected]:/home/tecmint/.
サンプル出力:
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -t /home/tecmint/. OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22. debug1: Connection established. debug1: identity file /home/tecmint/.ssh/id_rsa type -1 debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_dsa type -1 debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_xmss type -1 ...
リモートホストからローカルホストへのファイルコピー
次のコマンドは、ファイル“ssh-cheatsheet.pdf”をリモートホストからローカルシステムの/home/tecmintディレクトリにコピーします。
$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.
サンプル出力:
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -f /home/ravi/ssh-cheatsheet.pdf OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22. debug1: Connection established. debug1: identity file /home/tecmint/.ssh/id_rsa type -1 debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_dsa type -1 debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1 ...
リモートホストから別のホストへのファイルコピー
次のコマンドは、ファイル“ssh-cheatsheet.pdf”をリモートホストから別のリモートホストシステムの/home/tecmintディレクトリにコピーします。
$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf [email protected]:/home/anusha/.
ファイルのオリジナルの作成日時と時間でのファイルコピー
“-p
”パラメータは、ファイルのコピーを行う際にファイルのオリジナルの変更時間とアクセス時間を保持し、推定時間と接続速度が画面に表示されます。
$ scp -p scp-cheatsheet.pdf [email protected]:/home/tecmint/.
サンプル出力:
[email protected]'s password: scp-cheatsheet.pdf 100% 531 721.4KB/s 00:00
ファイルコピー時のScp圧縮
ファイル転送を高速化できるパラメータの1つが“-C
”パラメータであり、これはファイルの圧縮を実行中に行うために使用されます。圧縮はネットワーク内でのみ発生する点が特徴的です。ファイルが宛先サーバーに到着すると、圧縮が行われる前の元のサイズに戻ります。
以下のコマンドを見てください。93 Mbの単一ファイルを使用しています。
$ scp -pv messages.log [email protected]:.
サンプル出力:
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t. OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: Connecting to 202.x.x.x [202.x.x.x] port 22. debug1: Connection established. debug1: identity file /home/pungki/.ssh/id_rsa type -1 debug1: Found key in /home/pungki/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: Trying private key: /home/pungki/.ssh/id_rsa debug1: Next authentication method: password [email protected]'s password: debug1: Authentication succeeded (password). Authenticated to 202.x.x.x ([202.x.x.x]:22). debug1: Sending command: scp -v -p -t. File mtime 1323853868 atime 1380425711 Sending file timestamps: T1323853868 0 1380425711 0 messages.log 100% 93MB 58.6KB/s 27:05 Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds Bytes per second: sent 58758.4, received 15.6 debug1: Exit status 0
“-C
”パラメータなしでファイルをコピーすると、1661.3秒になります。以下のコマンドで“-C
“パラメータを使用した結果と比較してみてください。
$ scp -Cpv messages.log [email protected]:.
サンプル出力:
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t. OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: Connecting to 202.x.x.x [202.x.x.x] port 22. debug1: Connection established. debug1: identity file /home/pungki/.ssh/id_rsa type -1 debug1: Host '202.x.x.x' is known and matches the RSA host key. debug1: Found key in /home/pungki/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: Next authentication method: publickey debug1: Trying private key: /home/pungki/.ssh/id_rsa debug1: Next authentication method: password [email protected]'s password: debug1: Enabling compression at level 6. debug1: Authentication succeeded (password). Authenticated to 202.x.x.x ([202.x.x.x]:22). debug1: channel 0: new [client-session] debug1: Sending command: scp -v -p -t . File mtime 1323853868 atime 1380428748 Sending file timestamps: T1323853868 0 1380428748 0 Sink: T1323853868 0 1380428748 0 Sending file modes: C0600 97517300 messages.log messages.log 100% 93MB 602.7KB/s 02:38 Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds Bytes per second: sent 54813.9, received 97.0 debug1: Exit status 0 debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09 debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48
圧縮を使用すると、転送プロセスは162.5秒で完了します。これは、“-C
”パラメータを使用しない場合と比べて10倍高速です。ネットワーク上で多くのファイルをコピーする場合、“-C
”パラメータは必要な総時間を短縮するのに役立ちます。
注意すべき点は、圧縮方法がすべてのファイルで機能するわけではないことです。元のファイルが既に圧縮されている場合、改善は見られません。.zip、.rar、画像、.isoファイルなどは、“-C
”パラメータの影響を受けません。
SCP暗号化を変更してファイルを暗号化する
デフォルトでは、SCPは「AES-128」を使用してファイルを暗号化します。別の暗号化を使用したい場合は、「-c
」パラメータを使用できます。
このコマンドを見てください。
$ scp -c 3des Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13
上記のコマンドは、SCPに3desアルゴリズムを使用してファイルを暗号化するよう指示しています。注意してほしいのは、このパラメータは「-c
」ではなく「-C
」を使用していません。
SCPコマンドでの帯域幅使用の制限
もう一つ便利なパラメータは「-l
」パラメータです。「-l」パラメータは使用する帯域幅を制限します。自動化スクリプトでたくさんのファイルをコピーするが、SCPプロセスで帯域幅を使い果たしたくない場合に役立ちます。
$ scp -l 400 Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 50.3KB/s 01:13
「-l
」パラメータの後ろの400という値は、SCPプロセスの帯域幅を50 KB/secに制限することを意味します。
覚えておくべきことは、帯域幅はキロビット/秒(kbps)で指定されており、8ビットが1バイトに相当するということです。
一方でSCPはキロバイト/秒(KB/s)でカウントされます。したがって、バンド幅を最大でもSCPのみ50 KB/sに制限したい場合、50 x 8 = 400に設定する必要があります。
別のポートでのSCP
通常、SCPはデフォルトでポート22を使用しますが、セキュリティ上の理由から、ポートを別のポートに変更することができます。たとえば、ポート2249を使用しています。
その後、コマンドは次のようになります。
$ scp -P 2249 Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 262.3KB/s 00:14
大文字の「P
」を使用することを確認してください。「p
」は既に保持された時間とモードに使用されています。
SCP – ファイルおよびディレクトリを再帰的にコピー
時には、ディレクトリとその中にあるすべてのファイル/ディレクトリをコピーする必要があります。「-r
」パラメータを使用して、単一のコマンドでこれを行うことができると便利です。これにより、すべてのディレクトリが再帰的にコピーされます。
$ scp -r documents [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13 scp.txt 100% 10KB 9.8KB/s 00:00
コピー処理が完了すると、宛先サーバーでは、すべてのファイルが含まれた「documents」という名前のディレクトリが見つかります。フォルダ「documents」は自動的に作成されます。
SCP – 進捗メッセージの無効化
SCPからの進捗メーターと警告/診断メッセージを表示しないようにするには、「-q
」パラメータを使用して無効にできます。例を示します。
$ scp -q Label.pdf [email protected]:. [email protected]'s password: pungki@mint ~/Documents $
ご覧のように、パスワードを入力した後、SCPプロセスに関する情報はありません。プロセスが完了すると、もう一度プロンプトが表示されます。
SCP – プロキシを使用したファイルのコピー
プロキシサーバーは通常、オフィス環境で使用されます。ネイティブでは、SCPはプロキシを設定されていません。プロキシを使用している環境では、SCPにプロキシと通信するように「伝える」必要があります。
ここでのシナリオは、プロキシのアドレスが10.0.96.6で、プロキシのポートが8080です。プロキシにはユーザー認証も実装されています。まず、「~/.ssh/config」ファイルを作成する必要があります。次に、その内部にこのコマンドを入れます。
ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth
その後、「~/.ssh/proxyauth」ファイルを作成し、これを含めます。
myusername:mypassword
その後、通常どおりSCPを透過的に実行できます。
注意してくださいcorkscrewは、システムにまだインストールされていない可能性があります。私のLinux Mintでは、最初に標準のLinux Mintインストール手順を使用してインストールする必要があります。
$ apt-get install corkscrew
他のyumベースのシステムのユーザーは、以下のyumコマンドを使用してcorkscrewをインストールできます。
# yum install corkscrew
もう一つの注意点は、“~/.ssh/proxyauth”ファイルが“ユーザー名”と“パスワード”を明文で含んでいるため、ファイルがあなただけがアクセスできるようにしてください。
別のssh_configファイルを選択
会社のネットワークと公共のネットワークの間を頻繁に切り替えるモバイルユーザーにとって、SCPの設定を常に変更するのは苦痛です。必要に応じて異なるssh_configファイルを置く方が良いでしょう。
プロキシは会社のネットワークで使用されますが、公共のネットワークでは使用されず、定期的にネットワークを切り替えます。
$ scp -F /home/pungki/proxy_ssh_config Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13
デフォルトでは、“ssh_config”ファイルは“~/.ssh/config”にユーザーごとに配置されます。プロキシ互換性を持つ特定の“ssh_config”ファイルを作成することで、ネットワーク間を切り替えるのが簡単になります。
会社のネットワークにいるときは、“-F
”パラメータを使用できます。公共のネットワークにいるときは、“-F
”パラメータをスキップできます。
それでSCPについては以上です。詳細についてはSCPのmanページを参照してください。コメントや提案があれば自由にお寄せください。