scp remote secure copy file

brief introduction

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

scp is mainly used for the following three replication operations.

  • Local replication to remote.
  • Remote replication to local.
  • Replication between two remote systems.

When scp is used to transmit data, the file and password are encrypted and sensitive information will not be disclosed.

Basic grammar

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 to which the file is to be copied. They can both contain user names and host names.

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

The above command will send the remote host( user@host )Foo Txt, which is copied to the bar Txt. As you can see, the host and file should be separated by a colon (:).

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

Both 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 is the current host by default. Note that scp will use the configuration file of SSH client ssh/config. 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 source1 and source2 files to the destination.

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

Usage examples

(1) Copy local files to remote

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

# grammar
$ 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 local documents directory to the remote host,
# The documents directory will be created on the remote host
$ scp -r documents username@server_ip:/path_to_remote_directory

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

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

(2) Remote file copy to local

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

# grammar
$ 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 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 machine sends instructions to copy from remote host A to remote host B. the usage is as follows.

# grammar
$ 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 for passwords for both remote accounts. Data will be transferred directly from one remote host to another.

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 that the encryption algorithm is blowfish.

(2)-C

-The C parameter indicates whether the file is compressed during transfer.

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

(3)-F

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

$ 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

-l parameter is used to limit the bandwidth rate of data transmission, and the unit is Kbit/sec. For the bandwidth shared by many people, this parameter can set aside part of the bandwidth for others 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 information about the original file, 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 need not 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 close the progress bar that displays the copy.

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

(9)-r

-The r parameter indicates whether the directory is copied recursively.

(10)-v

-The v parameter is used to display detailed output.

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

Tags: server network

Posted by bynary on Wed, 01 Jun 2022 02:12:38 +0530