Linux basic command - scp to copy files remotely

Linux basic command -seq prints a sequence of numbers

foreword

Sometimes it is unavoidable to copy the file to another server, then you can use the scp command to copy the file remotely. The scp command is based on the SSH protocol. During the copying process, the data is encrypted, which is better than plaintext Transmission is more secure.

1. Command introduction

As usual, first go to the help documentation of scp to view the overview of commands

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.

The scp (secure copy) command is mainly used to copy files remotely. It can copy files or directories between multiple Linux systems. It is somewhat similar to the function of the cp command, but the scope of copying is another host on the network.

2. Command syntax

scp command syntax format: scp parameter file

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. Common parameters

There are so many parameters in the document, and some of the more commonly used ones are summarized;

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

Common parameters:

command options meaning
-1 Use ssh protocol version 1
-2 Use ssh protocol version 2
-4 use ipv4
6 use ipv6
-B run in batch mode
-C use compression
-F Specify ssh configuration file
-l Specify bandwidth limit
-o Specifies the ssh options to use
-P Specify the port number of the remote host
-p Preserve file modification time, access time and permission mode
-q Does not show copy progress
-r copy recursively

4. Reference examples

4.1 Remotely copy from local to another server

4.1.1 Copy files to remote server

Command format: copy file

The first method: Specify the user name, copy the file remotely to the directory of another server, and enter the password after the command is executed.
scp local_file remote_username@remote_ip:remote_folder

The second method: specify the user name, copy the file remotely to another server, the file can be modified to another name, which is equivalent to renaming the remote copy; enter the password after the command is executed.
scp local_file remote_username@remote_ip:remote_file

The third method: without specifying the user name, copy the file remotely to the directory of another server. After the command is executed, you need to enter the user name and password
scp local_file remote_ip:remote_folder

The fourth method: without specifying a user name, copy the file remotely to another server, and the file can be modified to another name, which is equivalent to renaming the remote copy; enter the password after the command is executed.
scp local_file remote_ip:remote_file

Case presentation:

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 Copy directory to remote server

Command format: copy directory

The first method: specify the user name, and remotely copy the directory to the specified directory of another server. You need to use the -r parameter to indicate a recursive operation. After the command is executed, enter the password scp -r local_folder remote_username@remote_ip:
remote_folder

The second method: without specifying the user name, copy the directory remotely to the specified directory of another server. You need to use the -r parameter to indicate recursive operation. After the command is executed, enter the password scp -r
local_folder remote_ip:remote_folder

Case presentation:

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

4.2 Copy from remote to local server

To copy from remote to local, just switch the order of the last two parameters of the command to copy from local to remote. Let’s take a look.

4.2.1 Copy files from remote server to local

The first way: specify the remote user name@specify IP: the file to be copied to ./, where ./ means to copy the file on the remote server to the current path of the local server.
scp remote_username@remote_ip:remote_file ./

The second way: specify the remote username@specified IP: the file to be copied to the local server specify the username, specify the directory, etc.
scp remote_username@remote_ip:remote_file local_username@local_ip:local_folder

Case presentation:

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

Of course, you can also copy the directory, the method is still the same and add the -r parameter in front, so I won’t demonstrate it here.

In addition to selecting the IP to copy files, you can also use the hostname to copy, provided that the hostname needs to be mapped to hosts first.

cat /etc/hosts
192.168.45.166 localhost166
192.168.45.128 localhost128

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

Summarize

The scp command is also often used. You only need to understand how to use it from local to remote, or from remote to local. If you think the above content is okay, you can like it and support it!
insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/130152967