scp is short for secure copy, a command used to copy files remotely under Linux. Similar commands to it are cp, but cp only copies locally and cannot cross servers, and scp transmission is encrypted. It may affect the speed a little. When your server hard disk becomes a read only system, using scp can help you move files out. In addition, scp is also very resource-intensive and will not increase the system load much. At this point, rsync is far behind it. Although rsync is a little faster than scp, when there are many small files, rsync will lead to very high hard disk I/O, and scp basically does not affect the normal use of the system.
1. Command format:
scp [parameter] [original path] [target path]
2. Command function:
scp is the abbreviation of secure copy, scp is a secure remote file copy command based on ssh login under linux system. The linux scp command can copy files and directories between linux servers.
3. Command parameters:
-1 Force the scp command to use the protocol ssh1
-2 Force the scp command to use the protocol ssh2
-4 Force the scp command to use only IPv4 addressing
-6 Force the scp command to use only IPv6 addressing
-B Use batch mode (not during transmission ask for a passphrase or phrase)
-C Enable compression. (Pass the -C flag to ssh to turn on compression)
-p Preserve the modification time, access time and access rights of the original file.
-q Do not display the transfer progress bar.
-r Recursively copies entire directories.
-v Display output in verbose mode. scp and ssh(1) will display debug information for the entire process. This information is used to debug connection, authentication and configuration issues.
-c cipher Encrypt data transmission with cipher, this option will be passed directly to ssh.
-F ssh_config Specifies an alternative ssh configuration file, this parameter is passed directly to ssh.
-i identity_file Read the key file used during transfer from the specified file, this parameter is passed directly to ssh.
-l limit Limit the bandwidth that users can use, in Kbit/s.
-o ssh_option If you are accustomed to using the parameter transmission method in ssh_config(5),
-P port note that the uppercase P is used, and port is the port number used for specifying data transmission.
-S program specifies the program used for encrypted transmission. This program must be able to understand the options of ssh(1).
4. Example of use:
Overview of the practical application of the scp command:
Copying from a local server to a remote server:
(1) Copying a file:
Command format:
scp local_file remote_username@remote_ip:remote_folder
or
scp local_file remote_username@remote_ip:remote_file
or
scp local_file remote_ip:remote_folder
or
scp local_file remote_ip:remote_file
1st and 2nd specify the user name. After the command is executed, you need to enter the user password. The 1st only specifies the remote directory, and the file name remains unchanged. The 2nd specifies the file name
. 3rd and 4th If the user name is not specified, you need to enter the user name and password after the command is executed. The third one only specifies the remote directory, the file name remains unchanged, and the fourth one specifies the file name
(2) Copy directory:
command format:
scp -r local_folder remote_username @remote_ip:remote_folder
or
scp -r local_folder remote_ip:remote_folder
The first one specifies the user name, and the user password is required after the command is executed;
the second one does not specify the user name, and the user name and password are required after the command is executed;
copy from the remote server to Local server:
The scp command copied from the remote to the local is the same as the above command, as long as the two parameters after the command copied from the local to the remote are exchanged in order.
Example 1: Copy files from a distance to a local directory
Command :
scp [email protected]:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
Output:
Copy code
[root@localhost ~]# cd /opt/soft/
[root@localhost soft]# ll
total 80072
drwxr-xr-x 12 root root 4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root 4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root 4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root 4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx 2 root root 4096 09-21 01:16 mysql
drwxr-xr-x 3 root root 4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root 4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root 4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp [email protected]:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
[email protected]'s password:
nginx-0.5.38.tar.gz 100% 479KB 478.7KB/s 00:00
[root@localhost soft]# ll
总计 80556
drwxr-xr-x 12 root root 4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root 4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root 4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root 4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx 2 root root 4096 09-21 01:16 mysql
-rw-r--r-- 1 root root 490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root 4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root 4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root 4096 2012-08-14 tomcat_7.0
[root@localhost soft] #Copy
code
Description :
Download nginx-0.5.38 from the directory of /opt/soft/ on the 192.168.120.204 machine. tar.gz file to the local /opt/soft/ directory
Example 2: Copy from remote to local
Command:
scp -r [email protected]:/opt/soft/mongodb /opt/soft/
Output:
Copy code
[root @localhost soft]# ll
total 80556
drwxr-xr-x 12 root root 4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root 4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root 4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root 4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk -6u16-linux-x64.bin
drwxrwxrwx 2 root root 4096 09-21 01:16 mysql
-rw-r--r-- 1 root root 490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root 4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root 4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root 4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp -r [email protected]:/opt/soft/mongodb /opt/soft/
[email protected]'s password:
mongodb-linux-i686-static-1.8.5.tgz 100% 28MB 28.3MB/s 00:01
README 100% 731 0.7KB/s 00:00
THIRD-PARTY-NOTICES 100% 7866 7.7KB/s 00:00
mongorestore 100% 7753KB 7.6MB/s 00:00
mongod 100% 7760KB 7.6MB/s 00:01
mongoexport 100% 7744KB 7.6MB/s 00:00
bsondump 100% 7737KB 7.6MB/s 00:00
mongofiles 100% 7748KB 7.6MB/s 00:01
mongostat 100% 7808KB 7.6MB/s 00:00
mongos 100% 5262KB 5.1MB/s 00:01
mongo 100% 3707KB 3.6MB/s 00:00
mongoimport 100% 7754KB 7.6MB/s 00:00
mongodump 100% 7773KB 7.6MB/s 00:00
GNU-AGPL-3.0 100% 34KB 33.7KB/s 00:00
[root@localhost soft]# ll
总计 80560
drwxr-xr-x 12 root root 4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root 4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root 4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root 4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxr-xr-x 3 root root 4096 03-15 09:18 mongodb
drwxrwxrwx 2 root root 4096 09-21 01:16 mysql
-rw-r--r-- 1 root root 490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root 4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root 4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root 4096 2012-08-14 tomcat_7.0
[root@localhost soft] #Copy
code
Description :
From /opt/ on the 192.168.120.204 machine Download the mongodb directory from soft/ to the local /opt/soft/ directory.
Example 3: Upload a local file to a specified directory on a remote machine
Command :
scp /opt/soft/nginx-0.5.38.tar.gz [email protected]:/opt/soft/scptest
Output:
Copy code The target of the target machine before
uploading Directory:
[root@localhost soft]# cd scptest/
[root@localhost scptest]# ll
total 0
[root@localhost scptest]# ll
local machine upload:
[root@localhost soft]# scp /opt/soft/nginx-0.5.38.tar.gz [email protected]:/opt/soft/scptest
[email protected]'s password:
nginx-0.5.38.tar .gz 100% 479KB 478.7KB/s 00:00
[root@localhost soft] #The
target directory of the target machine after uploading:
[root@localhost scptest]# ll
total 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#Copy
code
Description :
Copy the file nginx-0.5.38.tar.gz in the local opt/soft/ directory to the remote machine 192.168. 120.204 opt/soft/scptest directory
Example 4: Upload the local directory to the specified directory on the remote machine
Command:
scp -r /opt/soft/mongodb [email protected]:/opt/soft/scptest
output:
Copy code
The target directory of the target machine before uploading:
[root@localhost ~]# cd /opt/soft/scptest/
[root@localhost scptest]# ll
total 484
-rw-r--r-- 1 root root 490220 03-15 09 :25 nginx-0.5.38.tar.gz
[root@localhost scptest]#
local machine upload:
[root@localhost ~]# scp -r /opt/soft/mongodb [email protected]:/opt/soft/scptest
[email protected]'s password:
mongodb-linux-i686-static-1.8.5.tgz 100% 28MB 28.3MB/s 00:01
README 100% 731 0.7KB/s 00:00
THIRD-PARTY-NOTICES 100% 7866 7.7KB/s 00:00
mongorestore 100% 7753KB 7.6MB/s 00:00
mongod 100% 7760KB 7.6MB/s 00:01
mongoexport 100% 7744KB 7.6MB/s 00:00
bsondump 100% 7737KB 7.6MB/s 00:00
mongofiles 100% 7748KB 7.6MB/s 00:00
mongostat 100% 7808KB 7.6MB/s 00:01
mongos 100% 5262KB 5.1MB/s 00:00
mongo 100% 3707KB 3.6MB/s 00:00
mongoimport 100% 7754KB 7.6MB/s 00:01
mongodump 100% 7773KB 7.6MB/s 00:00
GNU-AGPL-3.0 100% 34KB 33.7KB/s 00:00
[root@localhost ~] #The
target directory of the target machine after uploading:
[root@localhost scptest]# ll
total 488
drwxr-xr-x 3 root root 4096 03-15 09:33 mongodb
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest ] #Copy
code
Reprinted in: http://www.itxuexiwang.com/a/liunxjishu/2016/0303/207.html?1457083691
One linux command per day (60): scp command
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326907042&siteId=291194637
Ranking