Linux 基本コマンド - ファイルをリモートでコピーする scp

Linux の基本コマンド -seq は一連の数字を出力します

序文

ファイルを別のサーバーにコピーすることが避けられない場合がありますが、その場合は、scp コマンドを使用してファイルをリモートでコピーできます。scp コマンドは SSH プロトコルに基づいています。コピー プロセス中、データは暗号化され、プレーンテキストよりも優れています。送信がより安全になります。

1. コマンドの紹介

いつものように、まず scp のヘルプ ドキュメントにアクセスしてコマンドの概要を確認します。

NAME
     scp — secure copy (remote file copy program)
DESCRIPTION
     scp copies files between hosts on a network.  It uses ssh(1) for data transfer, and uses the same
     authentication and provides the same security as ssh(1).  scp will ask for passwords or passphrases if
     they are needed for authentication.

     File names may contain a user and host specification to indicate that the file is to be copied to/from
     that host.  Local file names can be made explicit using absolute or relative pathnames to avoid scp
     treating file names containing ‘:’ as host specifiers.  Copies between two remote hosts are also per‐
     mitted.

scp (セキュア コピー) コマンドは、主にファイルをリモートでコピーするために使用されます。複数の Linux システム間でファイルまたはディレクトリをコピーできます。機能は cp コマンドに似ていますが、コピーの範囲はネットワーク上の別のホストです。

2. コマンド構文

scp コマンド構文形式: scp パラメータ ファイル

SYNOPSIS
     scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port]
         [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

3. 共通パラメータ

このドキュメントには非常に多くのパラメータがあり、より一般的に使用されるパラメータのいくつかがまとめられています。

     -1      Forces scp to use protocol 1.

     -2      Forces scp to use protocol 2.

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

     -4      Forces scp to use IPv4 addresses only.

     -6      Forces scp to use IPv6 addresses only.

     -B      Selects batch mode (prevents asking for passwords or passphrases).

     -C      Compression enable.  Passes the -C flag to ssh(1) to enable compression.

     -c cipher
             Selects the cipher to use for encrypting the data transfer.  This option is directly passed to
             ssh(1).

     -F ssh_config
             Specifies an alternative per-user configuration file for ssh.  This option is directly passed
             to ssh(1).

     -i identity_file
             Selects the file from which the identity (private key) for public key authentication is read.
             This option is directly passed to ssh(1).

     -l limit
             Limits the used bandwidth, specified in Kbit/s.

     -o ssh_option
             Can be used to pass options to ssh in the format used in ssh_config(5).  This is useful for
             specifying options for which there is no separate scp command-line flag.  For full details of
             the options listed below, and their possible values, see ssh_config(5).

                   AddressFamily
                   BatchMode
                   BindAddress
                   CanonicalDomains
                   CanonicalizeFallbackLocal
                   CanonicalizeHostname
                   CanonicalizeMaxDots
                   CanonicalizePermittedCNAMEs
                   CertificateFile
                   ChallengeResponseAuthentication
                   CheckHostIP
                   Cipher
                   Ciphers
                   Compression
                   CompressionLevel
                   ConnectionAttempts
                   ConnectTimeout
                   ControlMaster
     -2      Forces scp to use protocol 2.

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

     -4      Forces scp to use IPv4 addresses only.

     -6      Forces scp to use IPv6 addresses only.

     -B      Selects batch mode (prevents asking for passwords or passphrases).

     -C      Compression enable.  Passes the -C flag to ssh(1) to enable compression.

     -c cipher
             Selects the cipher to use for encrypting the data transfer.  This option is directly passed to
             ssh(1).

     -F ssh_config
             Specifies an alternative per-user configuration file for ssh.  This option is directly passed
             to ssh(1).

     -i identity_file
             Selects the file from which the identity (private key) for public key authentication is read.
             This option is directly passed to ssh(1).

     -l limit
             Limits the used bandwidth, specified in Kbit/s.

     -o ssh_option
             Can be used to pass options to ssh in the format used in ssh_config(5).  This is useful for
             specifying options for which there is no separate scp command-line flag.  For full details of
             the options listed below, and their possible values, see ssh_config(5).

共通パラメータ:

コマンドオプション 意味
-1 SSHプロトコルバージョン1を使用する
-2 SSHプロトコルバージョン2を使用する
-4 IPv4を使用する
6 IPv6を使用する
-B バッチモードで実行する
-C 圧縮を使用する
-F SSH設定ファイルを指定する
-l 帯域幅制限を指定する
-o 使用する ssh オプションを指定します
-P リモートホストのポート番号を指定します
-p ファイルの変更時刻、アクセス時刻、許可モードを保持する
-q コピーの進行状況が表示されない
-r 再帰的にコピーする

4. 参考例

4.1 ローカルから別のサーバーへのリモートコピー

4.1.1 ファイルをリモートサーバーにコピーする

コマンド形式:ファイルをコピー

1 つ目の方法: ユーザー名を指定し、ファイルを別のサーバーのディレクトリにリモートでコピーし、コマンドの実行後にパスワードを入力します。
scp local_file リモートユーザー名@リモートIP:リモートフォルダー

2 番目の方法: ユーザー名を指定し、ファイルを別のサーバーにリモートでコピーします。ファイルは別の名前に変更できます。これは、リモート コピーの名前を変更するのと同じです。コマンドの実行後にパスワードを入力します。
scp local_file リモートユーザー名@remote_ip:remote_file

3 番目の方法: ユーザー名を指定せずに、ファイルを別のサーバーのディレクトリにリモートでコピーします。コマンドの実行後、ユーザー名とパスワードを入力する必要があります。 scp local_file remote_ip:
remote_folder

4 番目の方法: ユーザー名を指定せずに、ファイルを別のサーバーにリモートでコピーすると、ファイルを別の名前に変更できます (リモート コピーの名前を変更するのと同じです)。コマンドの実行後にパスワードを入力します。
scp local_file リモート IP:remote_file

事例紹介:

scp /usr/local/nginx/conf/nginx.conf [email protected]:/usr/local/nginx/conf

scp /usr/local/nginx/conf/nginx.conf [email protected]:/usr/local/nginx/conf/nginx.conf.backup

scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf

scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf.backup

4.1.2 ディレクトリをリモートサーバーにコピーする

コマンド形式:ディレクトリをコピー

最初の方法: ユーザー名を指定し、別のサーバーの指定されたディレクトリにディレクトリをリモートでコピーします。再帰操作を示すには、-r パラメータを使用する必要があります。コマンドの実行後、パスワード scp -r local_folder remote_username を入力します。 @remote_ip:
リモートフォルダ

2 番目の方法: ユーザー名を指定せずに、別のサーバーの指定されたディレクトリにディレクトリをリモートでコピーします。再帰操作を示すには、-r パラメータを使用する必要があります。コマンドの実行後、パスワード scp -r local_folder remote_ip を入力します
。リモートフォルダー

事例紹介:

scp -r /tmp/ [email protected]:/
scp -r /tmp/ 192.168.45.128:/

4.2 リモートサーバーからローカルサーバーへのコピー

リモートからローカルにコピーするには、コマンドの最後の 2 つのパラメーターの順序を入れ替えて、ローカルからリモートにコピーするだけです。

4.2.1 リモートサーバーからローカルにファイルをコピーする

1 つ目の方法: リモート ユーザー名を指定します@IP を指定します: ./ にコピーされるファイル。ここで、./ は、リモート サーバー上のファイルをローカル サーバーの現在のパスにコピーすることを意味します。
scp リモートユーザー名@リモートIP:リモートファイル ./

2 番目の方法: リモート ユーザー名@指定された IP を指定します。ローカル サーバーにコピーされるファイルはユーザー名を指定し、ディレクトリなどを指定します。
scp リモート ユーザー名@リモート_ip:リモート ファイル ローカル ユーザー名@ローカル_ip:ローカル フォルダー

事例紹介:

scp [email protected]:/usr/local/nginx/conf/nginx.conf 
/usr/local/conf/
scp -r [email protected]:/usr/local/nginx/ [email protected]:/usr/local/

もちろん、ディレクトリをコピーすることもできます。方法は同じで、先頭に -r パラメータを追加するので、ここでは説明しません。

ファイルをコピーする IP を選択するだけでなく、最初にホスト名をホストにマップする必要がある場合は、ホスト名を使用してコピーすることもできます。

cat /etc/hosts
192.168.45.166 localhost166
192.168.45.128 localhost128

scp -r root@localhost128:/usr/local/nginx/ root@localhost166:/usr/local

要約する

scp コマンドもよく使われますが、ローカルからリモート、またはリモートからローカルの使い方を理解するだけで十分です。
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/rhn_111/article/details/130152967