Linuxscpコマンドの詳細な説明とローカルファイルとリモートサーバーファイルの相互転送の実現

  Linuxの運用・保守業務では、ローカルファイルを他のサーバーに転送したり、他のサーバーからローカルサーバーにファイルを取得したりする必要がある場合があります。scpコマンドは、ローカルサーバーとリモートサーバー間のファイルの相互転送を実現します。
  マシン間でコピーする際のデータのセキュリティを向上させるために、scpはssh接続と暗号化を使用します。マシン間でsshパスワードなしのログインが設定されている場合、scpを使用するときにパスワードを入力する必要はありません。
  scpは、secure copyの略で、Linuxシステムでのsshログインに基づくセキュアなリモートファイルコピーコマンドであり、Linuxサーバー間でファイルまたはディレクトリをコピーできます。これに似たコマンドにはcpがありますが、cpはサーバー間ではなくローカルにのみコピーされ、scp送信は暗号化されるため、速度にわずかに影響する可能性があります。サーバーのハードディスクが読み取り専用の読み取り専用システムになると、scpを使用してファイルを移動できます。さらに、scpはリソースをあまり消費せず、システムの負荷をあまり増加させません。この時点で、rsyncははるかに遅れています。rsyncはscpよりも少し高速ですが、小さなファイルが多数ある場合、rsyncは非常に高いハードディスクI / Oを引き起こし、scpは基本的にシステムの通常の使用に影響を与えません。

  1.コマンド形式コマンド形式
  を表示するには、scphelpと入力します。

$ scp help
usage: 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

  2.オプションの意味
  特定のオプションの意味を表示するには、manscpと入力します。

$ man scp
SCP(1)                                                              BSD General Commands Manual                                                             SCP(1)

NAME
     scp — secure copy (remote file copy program)

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

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

     The options are as follows:

     -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
                   ControlPath
                   ControlPersist
                   GlobalKnownHostsFile
                   GSSAPIAuthentication
                   GSSAPIDelegateCredentials
                   HashKnownHosts
                   Host
                   HostbasedAuthentication
                   HostbasedKeyTypes
                   HostKeyAlgorithms
                   HostKeyAlias
                   HostName
                   IdentitiesOnly
                   IdentityAgent
                   IdentityFile
                   IPQoS
                   KbdInteractiveAuthentication
                   KbdInteractiveDevices
                   KexAlgorithms
                   LogLevel
                   MACs
                   NoHostAuthenticationForLocalhost
                   NumberOfPasswordPrompts
                   PasswordAuthentication
                   PKCS11Provider
                   Port
                   PreferredAuthentications
                   Protocol
                   ProxyCommand
                   ProxyJump
                   PubkeyAcceptedKeyTypes
                   PubkeyAuthentication
                   RekeyLimit
                   RhostsRSAAuthentication
                   RSAAuthentication
                   SendEnv
                   ServerAliveInterval
                   ServerAliveCountMax
                   StrictHostKeyChecking
                   TCPKeepAlive
                   UpdateHostKeys
                   UsePrivilegedPort
                   User
                   UserKnownHostsFile
                   VerifyHostKeyDNS

     -P port
             Specifies the port to connect to on the remote host.  Note that this option is written with a capital ‘P’, because -p is already reserved for pre‐
             serving the times and modes of the file.

     -p      Preserves modification times, access times, and modes from the original file.

     -q      Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh(1).

     -r      Recursively copy entire directories.  Note that scp follows symbolic links encountered in the tree traversal.

     -S program
             Name of program to use for the encrypted connection.  The program must understand ssh(1) options.

     -v      Verbose mode.  Causes scp and ssh(1) to print debugging messages about their progress.  This is helpful in debugging connection, authentication, and
             configuration problems.

EXIT STATUS
     The scp utility exits 0 on success, and >0 if an error occurs.

SEE ALSO
     sftp(1), ssh(1), ssh-add(1), ssh-agent(1), ssh-keygen(1), ssh_config(5), sshd(8)

HISTORY
     scp is based on the rcp program in BSD source code from the Regents of the University of California.

AUTHORS
     Timo Rinne <tri@iki.fi>
     Tatu Ylonen <ylo@cs.hut.fi>

BSD                                                                      November 6, 2020                                                                      BSD

  三、コマンドパラメータの中国語の解釈

  • -1scpコマンドにプロトコルssh1の使用を強制します。
  • -2プロトコルssh2を使用するようにscpコマンドを強制します。
  • -3ローカルホストを介して2つのリモートホスト間でデータを転送します。このオプションがないと、データはリモートホスト間で直接コピーされます。注:このオプションは、プログレスバーの表示をサポートしていません。
  • -4scpコマンドでIPv4アドレス指定のみを使用するように強制します。
  • -6scpコマンドでIPv6アドレス指定のみを使用するように強制します。
  • -Bバッチモードを使用します(送信中に送信パスワードやフレーズを要求しないでください)。
  • -Cは圧縮を許可します。(圧縮をオンにするには、-Cフラグをsshに渡します)。
  • -p元のファイルの変更時間、アクセス時間、およびアクセス許可を保持します。
  • -q転送プログレスバーを表示しません。
  • -rディレクトリ全体を再帰的にコピーします。
  • -v詳細モードで出力を表示します。scpおよびssh(1)は、プロセス全体のデバッグ情報を表示します。この情報は、接続、検証、および構成の問題をデバッグするために使用されます。
  • -c暗号暗号を使用してデータ転送を暗号化します。このオプションはsshに直接渡されます。
  • -F ssh_configは、代替のssh構成ファイルを指定します。このパラメーターはsshに直接渡されます。
  • -iidentity_file送信中に使用されたキーファイルを指定されたファイルから読み取ります。このパラメーターはsshに直接渡されます。
  • -llimitユーザーが使用できる帯域幅をキロビット/秒で制限します。
  • -Pポートは、データ送信に使用されるポート番号を指定します
  • -Sprogram暗号化された送信に使用されるプログラムを指定します。プログラムは、ssh(1)のオプションを理解できる必要があります。

  4、使用例

  環境情報:

リモートサーバーIP 47.100.247.240
リモートサーバーログインユーザー testuser
リモートサーバーファイルのパス / home / testuser /
リモートサーバーのファイル名 test.sql
ローカルサーバーのファイルパス / home / testuser1
ローカルサーバーのファイル名 test1.sql
  1. リモートサーバーファイルを
    ローカルサーバーの現在のパスにコピーしますローカルサーバーの現在のパスで、次のコマンドを入力し、Enterキーを押してリモートサーバーのログインパスワードを入力し、リモートファイルをローカルの現在のパスにコピーします。サーバ。
$ scp testuser@47.100.247.240:/home/testuser/test.sql .
  1. ローカルサーバーファイルをリモートサーバーにプッシュするローカルサーバー
    の現在のパスで、次のコマンドを入力し、Enterキーを押してリモートサーバーのログインパスワードを入力し、ローカルファイルをリモートサーバーの指定されたパスにコピーします。
$ scp test1.sql testuser@47.100.247.240:/home/testuser/
  1. すべてのリモートサーバーフォルダーをローカルサーバーにコピーします
$ scp -r testuser@47.100.247.240:/home/testuser/ .
  1. すべてのローカルサーバーフォルダーをリモートサーバーにコピーします
$ scp -r /home/testuser1/ testuser@47.100.247.240:/home/testuser/

  記事の参照:
  scpコマンドの詳細な説明

おすすめ

転載: blog.csdn.net/piaoranyuji/article/details/109504074