Detailed explanation of Linux scp command and realization of mutual transfer of local files and remote server files

  During Linux operation and maintenance, sometimes it is necessary to transfer local files to other servers, or obtain files from other servers to the local server. The scp command can realize the mutual transfer of files between the local server and the remote server.
  In order to improve the security of data when copying across machines, scp uses ssh connection and encryption. If ssh password-free login is configured between machines, there is no need to enter a password when using scp.
  scp is the abbreviation of secure copy, which is a secure remote file copy command based on ssh login under Linux system, which can copy files or directories between Linux servers. Commands similar to it have cp, but cp is only copied locally, not across servers, and scp transmission is encrypted, which may slightly affect the speed. When the server hard disk becomes read-only read only system, you can use scp to move the file out. In addition, scp does not take up resources very much and does not increase the system load much. At this point, rsync is far behind it. Although rsync is a bit faster than scp, when there are many small files, rsync will cause very high hard disk I/O, and scp basically does not affect the normal use of the system.

  1. Command format
  Enter scp help to view the command format.

$ 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. Option meaning
  Enter man scp to view the meaning of specific options.

$ 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

  Three, the Chinese interpretation of command parameters

  • -1 Force scp command to use protocol ssh1.
  • -2 Force scp command to use protocol ssh2.
  • -3 Transfer data between two remote hosts through the local host. Without this option, the data will be copied directly between the remote hosts. Note: This option does not support progress bar display.
  • -4 Force the scp command to use only IPv4 addressing.
  • -6 Force the scp command to only use IPv6 addressing.
  • -B Use batch mode (don’t ask for the transmission password or phrase during transmission).
  • -C allows compression. (Pass the -C flag to ssh to turn on compression).
  • -p Keep the modification time, access time and access permissions of the original file.
  • -q Does not display the transfer progress bar.
  • -r Copy the entire directory recursively.
  • -v Display output in verbose mode. scp and ssh(1) will display the debugging information of the whole process. This information is used to debug connection, verification and configuration issues.
  • -c cipher Use cipher to encrypt data transmission. This option will be passed to ssh directly.
  • -F ssh_config specifies an alternative ssh configuration file, this parameter is passed directly to ssh.
  • -i identity_file Read the key file used during transmission from the specified file. This parameter is passed directly to ssh.
  • -l limit Limit the bandwidth that the user can use, in Kbit/s.
  • -P port specifies the port number used for data transmission
  • -S program Specifies the program used for encrypted transmission. The program must be able to understand the options of ssh(1).

  Four, use examples

  Environmental information:

Remote server IP 47.100.247.240
Remote server login user testuser
The path of the remote server file /home/testuser/
Remote server file name test.sql
Local server file path /home/testuser1
Local server file name test1.sql
  1. Copy remote server files to the current path of
    the local server . In the current path of the local server, enter the following command, press Enter and enter the login password of the remote server to copy the remote files to the current path of the local server.
$ scp testuser@47.100.247.240:/home/testuser/test.sql .
  1. Push local server files to remote server
    In the current path of the local server, enter the following command, press Enter and enter the login password of the remote server to copy the local files to the specified path of the remote server.
$ scp test1.sql testuser@47.100.247.240:/home/testuser/
  1. Copy all the remote server folders to the local server
$ scp -r testuser@47.100.247.240:/home/testuser/ .
  1. Copy all the local server folders to the remote server
$ scp -r /home/testuser1/ testuser@47.100.247.240:/home/testuser/

  Article reference:
  Detailed explanation of scp command

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/109504074
Recommended