scp command

scp is a client program provided by SSH, which is used to encrypt files (ie copy files) between two hosts.

Introduction

scp is the abbreviation of secure copy, which is equivalent to cp command + SSH. Its bottom layer is the SSH protocol, and the default port is 22, which is equivalent to using the ssh command to log in to the remote host first, and then perform the copy operation.

scp is mainly used for the following three copy operations.

-Copy locally to remote. -Remote copy to local. -Replication between two remote systems.

When using scp to transfer data, files and passwords are encrypted, and sensitive information will not be leaked.

Basic syntax

The syntax of scp is similar to that of cp.

$ scp source destination

In the above command, source is the current location of the file, and destination is the location where the file is to be copied. They can all contain username and hostname.

$ scp user@host:foo.txt bar.txt

The above command copies foo.txt in the user's home directory of the remote host (user@host) to bar.txt in the current directory of the machine. As you can see, a colon (:) is used to separate the host and the file.

scp will first log in to the remote host with SSH, and then copy files in the encrypted connection. After the client initiates a connection, it will prompt the user to enter a password, which is consistent with the usage of SSH.

Both the user name and host name can be omitted. The default value of the user name is the current user name of the machine, and the host name defaults to the current host. Note that scp will use the configuration file .ssh/config of the SSH client. If the alias of the host is defined in the configuration file, the alias connection can also be used here.

scp supports copying multiple files at once.

$ scp source1 source2 destination

The above command will copy the two files source1 and source2 to destination.

Note that if the file to be copied already has a file with the same name in the target location, scp will overwrite the file with the same name without warning.

Usage example

(1) Copy local files to remote

The usage of copying local files to remote system is as follows.

# Syntax
$ scp SourceFile user@host:directory/TargetFile

# Example
$ scp file.txt remote_username@10.10.0.2:/remote/directory

The following is an example of copying an entire directory.

# Copy the documents directory of this machine to the remote host,
# Will create the documents directory on the remote host
$ scp -r documents username@server_ip:/path_to_remote_directory

# Copy the entire directory of the machine to the remote directory
$ scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/

# Copy all the contents of the local directory to the remote directory
$ scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/

(2) Copy remote files to local

The usage of copying files from remote host to local is as follows.

# Syntax
$ scp user@host:directory/SourceFile TargetFile

# Example
$ scp remote_username@10.10.0.2:/remote/file.txt /local/directory

The following is an example of copying an entire directory.

# Copy a remote directory to the local directory
$ scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/

# Copy all the contents in the remote directory to the local directory
$ scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/
$ scp -r user@host:directory/SourceFolder TargetFolder

(3) Replication between two remote systems

The local computer issues instructions to copy from remote host A to remote host B. The usage is as follows.

# Syntax
$ scp user@host1:directory/SourceFile user@host2:directory/SourceFile

# Example
$ scp user1@host1.com:/files/file.txt user2@host2.com:/files

You will be prompted to enter the passwords for the two remote accounts. Data will be transferred directly from one remote host to another remote host.

Configuration item

(1)-c

The -c parameter is used to specify the encryption algorithm for file copy data transmission.

$ scp -c blowfish some_file your_username@remotehost.edu:~

The above code specifies the encryption algorithm as blowfish.

(2)-C

The -C parameter indicates whether to compress files during transmission.

$ scp -c blowfish -C local_file your_username@remotehost.edu:~

(3)-F

The -F parameter is used to specify the ssh_config file for ssh use.

$ scp -F /home/pungki/proxy_ssh_config Label.pdf root@172.20.10.8:/root

(4)-i

The -i parameter is used to specify the key.

$ scp -vCq -i private_key.pem ~/test.txt root@192.168.1.3:/some/path/test.txt

(5)-l

The -l parameter is used to limit the bandwidth rate of the transmitted data, the unit is Kbit/sec. For bandwidth shared by multiple people, this parameter can set aside a portion of the bandwidth for other people to use.

$ scp -l 80 yourusername@yourserver:/home/yourusername/*.

In the above code, the bandwidth occupied by the scp command is limited to 80K bits per second, that is, 10K bytes per second.

(6)-p

The -p parameter is used to retain the original file information such as modification time, access time, and file status (mode).

$ scp -p ~/test.txt root@192.168.1.3:/some/path/test.txt

(7)-P

The -P parameter is used to specify the SSH port of the remote host. If the remote host uses the default port 22, you don't need to specify it, otherwise you need to specify it in the command with the -P parameter.

$ scp -P 2222 user@host:directory/SourceFile TargetFile

(8)-q

The -q parameter is used to turn off the progress bar showing the copy.

$ scp -q Label.pdf mrarianto@202.xxx:.

(9)-r

The -r parameter indicates whether to copy the directory recursively.

(10)-v

The -v parameter is used to display detailed output.

$ scp -v ~/test.txt root@192.168.1.3:/root/help2356.txt