SCP Linux コマンド – Linux でファイルを安全に転送する
Linux 管理者はコマンドライン環境に精通している必要があります。 Linux サーバーではGUI (グラフィカル ユーザー インターフェイス) モードがインストールされるのは一般的ではないためです。
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 :/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 :/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 :/home/ravi/ssh-cheatsheet.pdf [email :/home/anusha/.
元の作成日時を含むファイルをコピーする
「-p
」パラメータは、ファイルのコピー中にファイルの元の変更時間とアクセス時間を保持し、推定時間と接続速度が画面に表示されます。
scp -p scp-cheatsheet.pdf [email :/home/tecmint/.
サンプル出力:
[email 's password:
scp-cheatsheet.pdf 100% 531 721.4KB/s 00:00
ファイルコピー中のScp圧縮
ファイル転送を高速化できるパラメータの 1 つは「-C
」パラメータです。これは、外出先でファイルを圧縮するために使用されます。ユニークなのは、圧縮がネットワーク内でのみ行われることです。ファイルが宛先サーバーに到着すると、圧縮が行われる前の元のサイズに戻ります。
これらのコマンドを見てください。 93 MB の単一ファイルを使用しています。
scp -pv messages.log [email :.
サンプル出力:
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 '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 :.
サンプル出力:
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 '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 :.
[email 's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
上記のコマンドは、SCP に3des アルゴリズムを使用してファイルを暗号化するように指示します。このパラメータには「-C
」ではなく「-c
」を使用していることに注意してください。
SCP コマンドによる帯域幅使用の制限
便利なもう 1 つのパラメータは、「-l
」パラメータです。 「-l」パラメータは、使用する帯域幅を制限します。これは、自動スクリプトを実行して大量のファイルをコピーする場合に便利ですが、SCP プロセスによって帯域幅が消費されることは望ましくありません。
scp -l 400 Label.pdf [email :.
[email 's password:
Label.pdf 100% 3672KB 50.3KB/s 01:13
「-l
」パラメータの背後にある 400 値は、SCP プロセスの帯域幅を 50 KB/ のみに制限することを意味します。秒です。
覚えておいていただきたいのは、帯域幅はキロビット/秒 (kbps) で指定されるということです。つまり8 ビットが1 バイトに相当します。
一方、SCP はキロバイト/秒 (KB/秒) でカウントされます。したがって、帯域幅を SCP の最大値である 50 KB/s に制限したい場合は、それを 50 に設定する必要があります。 × 8=400。
異なるポートを持つ SCP
通常、SCP はデフォルト ポートとしてポート 22 を使用しますが、セキュリティ上の理由から、ポートを別のポートに変更することができます。たとえば、ポート2249を使用しています。
次に、コマンドは次のようになります。
scp -P 2249 Label.pdf [email :.
[email 's password:
Label.pdf 100% 3672KB 262.3KB/s 00:14
「p
」は保存された時間とモードにすでに使用されているため、「p
」ではなく大文字の「P
」を使用していることを確認してください。
SCP – ファイルとディレクトリを再帰的にコピーする
場合によっては、ディレクトリとその中のすべてのファイル/ディレクトリをコピーする必要があります。ディレクトリ全体を再帰的にコピーする「-r
」パラメータを使用して、これを 1 つのコマンドで実行できればさらに良いでしょう。
scp -r documents [email :.
[email 's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00
コピープロセスが完了すると、宛先サーバーに「ドキュメント」という名前のディレクトリとそのすべてのファイルが表示されます。 「ドキュメント」フォルダが自動的に作成されます。
SCP – 進行状況メッセージを無効にする
SCP からの進行状況メーターと警告/診断メッセージを表示しないことを選択した場合は、「-q
」パラメータを使用して無効にすることができます。以下に例を示します。
scp -q Label.pdf [email :.
[email '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 を実行できます。
コルク抜きがシステムにまだインストールされていない可能性があることに注意してください。私の Linux Mint では、標準の Linux Mint インストール手順を使用して、最初にそれをインストールする必要があります。
apt-get install corkscrew
他の yum ベースのシステムの場合、ユーザーは次の yum コマンドを使用して Corkscrew をインストールできます。
yum install corkscrew
もう 1 つは、「~/.ssh/proxyauth」ファイルには「ユーザー名」と「パスワード」がクリアテキスト形式で含まれていることです。 、ファイルに自分だけがアクセスできるようにしてください。
別の ssh_config ファイルを選択してください
社内ネットワークと公衆ネットワークを頻繁に切り替えるモバイルユーザーにとって、SCP の設定を常に変更するのは苦痛です。ニーズに合わせて別のssh_config ファイルを配置できればより良いでしょう。
プロキシは社内ネットワークでは使用されますが、パブリック ネットワークでは使用されず、定期的にネットワークを切り替えます。
scp -F /home/pungki/proxy_ssh_config Label.pdf
[email :.
[email 's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
デフォルトでは、ユーザーごとの「ssh_config」ファイルは「~/.ssh/config」に配置されます。プロキシ互換性のある特定の「ssh_config」ファイルを作成すると、ネットワーク間の切り替えが簡単になります。
企業ネットワークに接続している場合は、「-F
」パラメータを使用できます。パブリック ネットワークを使用している場合は、「-F
」パラメータをスキップできます。
以上がSCPについてです。詳細については、SCP のマニュアル ページを参照してください。コメントや提案をお気軽に残してください。