Linux command: scp

Table of contents

Introduction

1. Grammar

Two, examples

2.1 Copy local files to remote host directory

2.2 Copy the local directory to the remote host directory

2.3 Copy the files from the remote host to this machine

2.4 Copy the remote host directory to this machine


Introduction

Today we will introduce a Linux command: scp

scp — secure copy (remote file copy program), meaning safe copy, means safe copy, is a remote file copy program. To be precise, it is a command for securely copying files (including directories) between different servers. scp is based on the rcp program in the BSD source code from Regents of the University of California.

How safe?

 Commands to securely copy files to and from remote systems to the local system via  the SSH protocol. Using SSH means it enjoys the same level of data encryption as SSH and is therefore considered a secure way to transfer files across two remote hosts.

1. Grammar

Grammar introduction:

scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target

With so many parameters? Yes, it is. If you don't believe me, you can use the following command to check:

man scp

Most of these parameters are not used by us, so we can introduce a few commonly used ones.

-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 preserving the times and modes of the file.

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

The translation is:

-P: Specifies the port to connect to on the remote host. Note that this option is written with a capital "P" because -P is reserved for saving the time and mode of the file.

-p: Preserve the modification time, access time and mode of the original file.

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

 The translation is:

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

 -C Compression enable. Passes the -C flag to ssh(1) to enable compression.

 The translation is:

 -C: Compression enabled. Pass the -C flag to ssh(1) to enable compression.

Summarized as follows:

-P: Specifies the port. The default is 22, no need to specify; if it is not 22, use capital P to specify the port.

-p: Preserve the modification time, access time and mode of the original file.

-r: Recursively copy entire directories. Copying the directory is essential.

-C: Compress the file.

Two, examples

2.1 Copy local files to remote host directory

scp redis.conf root@k8s-node02:/usr/local/redis-cluster/slave-6381/conf

Copy the redis.conf file in the current directory of the machine to the /usr/local/redis-cluster/slave-6381/conf directory on port 22 of the k8s-node02 host through the user root of the remote host.

Among them, k8s-node02 can be replaced with the ip of the remote host, port 22 is the default and does not need to be filled in, and root is the user name of the remote host.

After executing this command, there will be a prompt:

 meaning is:

Unable to determine authenticity of host 'k8s-master01 (192.168.222.100)'.

The ECDSA key fingerprint is SHA256:nbOQMg0kDl5DW/jNRip8WsjbGKqzS/CIUuir7EWIB1Q.

Are you sure you want to continue connecting (yes/no/[fingerprint])?

 We enter yes at this time, and then enter the password of the remote host. If successful, the successfully copied file will be displayed: redis.conf.

2.2 Copy the local directory to the remote host directory

We create a directory, then create a file under the directory, and finally copy the directory along with the file to the remote host.

scp -r test root@k8s-master01:/usr/local/redis-cluster/slave-6381/

Now go to the /usr/local/redis-cluster/slave-6381/ directory of the k8s-master01 host to check.

2.3 Copy the files from the remote host to this machine

 We first delete the test-scp.txt file in the local directory, and then copy the file from the k8s-master01 host.

scp root@k8s-master01:/usr/local/redis-cluster/slave-6381/test/test-scp.txt ./

The ./ above can be modified to the directory you want, and the ./ here refers to the current directory of the current host.

Next, we will delete the entire test directory, and then copy the directory of the remote host.

2.4 Copy the remote host directory to this machine

scp -r root@k8s-master01:/usr/local/redis-cluster/slave-6381/test ./

 

Guess you like

Origin blog.csdn.net/qq_42971035/article/details/128171117