linux scp command usage details

Posted by lucifer27 on Sat, 13 Nov 2021 02:36:40 +0100

1: Foreword

The Linux scp command is used to copy files and directories between Linux.
scp is the abbreviation of secure copy. scp is a secure remote file copy command based on ssh login in linux system.
scp is encrypted, rcp is not encrypted, and scp is an enhanced version of rcp.
scp command can also be used under Windows system, so we can use scp to transfer files to each other on linux and windows systems.

2: Grammar

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 [...] [[user@]host2:]file2
Easy to write:

scp [Optional parameters] file_source file_target 
Parameter Description:
-1:  force scp Command usage protocol ssh1
-2:  force scp Command usage protocol ssh2
-4:  force scp Command use only IPv4 addressing
-6:  force scp Command use only IPv6 addressing
-B:  Use batch mode (transmission password or phrase is not asked during transmission)
-C:  Allow compression. (will)-C Flag passed to ssh,To turn on the compression function)
-p: Retain the modification time, access time and access rights of the original file.
-q:  The transfer progress bar is not displayed.
-r:  Copy the entire directory recursively.
-v: Displays the output in detail. scp and ssh(1)The debugging information of the whole process is displayed. This information is used to debug connections, verify and configure problems.
-c cipher:  with cipher Encrypt the data transmission, and this option will be passed directly to ssh. 
-F ssh_config:  Specify an alternate 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 users can use to Kbit/s In.
-o ssh_option:  If used to ssh_config(5)Parameter transfer method in,
-P port: Note that it is capitalized P, port Is the port number used to specify data transmission
-S program:  Specifies the program to use when encrypting the transmission. This procedure must be understandable ssh(1)Options for.

3: Instance

1. Copy from local to remote

(1) Command format

scp local_file remote_username@remote_ip:remote_folder 
perhaps 
scp local_file remote_username@remote_ip:remote_file 
perhaps 
scp local_file remote_ip:remote_folder 
perhaps 
scp local_file remote_ip:remote_file 

The first and second specify the user name, and the password needs to be entered after the command is executed. The first only specifies the remote directory, and the file name remains unchanged. The second specifies the file name;
The 3rd and 4th do not specify a user name. After the command is executed, you need to enter a user name and password. The 3rd only specifies a remote directory, and the file name remains unchanged. The 4th specifies a file name;

(2) Application examples

scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music 
scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music/001.mp3 
scp /home/space/music/1.mp3 www.runoob.com:/home/root/others/music 
scp /home/space/music/1.mp3 www.runoob.com:/home/root/others/music/001.mp3 

(3) Copy directory command format

scp -r local_folder remote_username@remote_ip:remote_folder 
perhaps 
scp -r local_folder remote_ip:remote_folder 

The first specifies the user name, and the password needs to be entered after the command is executed;
The second user name is not specified. After the command is executed, you need to enter the user name and password;
Application example:

scp -r /home/space/music/ root@www.runoob.com:/home/root/others/ 
scp -r /home/space/music/ www.runoob.com:/home/root/others/ 

The above command copies the local music directory to the remote others directory.

2. Copy from remote to local

To copy from remote to local, just exchange the last two parameters of the command copied from local to remote, as shown in the following example
Application example:

scp root@www.runoob.com:/home/root/others/music /home/space/music/1.mp3 
scp -r www.runoob.com:/home/root/others/ /home/space/music/

explain
1. If the remote server firewall has a specified port set for the scp command, we need to use the - P parameter to set the port number of the command. The command format is as follows:
#The scp command uses port number 4588

scp -P 4588 remote@www.runoob.com:/usr/local/sin.sh /home/administrator

2. When using the scp command, ensure that the user has the permission to read the corresponding files of the remote server, otherwise the scp command will not work.

Topics: Linux ssh server