您的位置:首页 > 运维架构 > Linux

linux常用命令(48):scp命令

2015-09-22 15:07 656 查看

scp命令

scp命令是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。

语法

scp(选项)(参数)

选项

-1:使用ssh协议版本1;

-2:使用ssh协议版本2;

-4:使用ipv4;

-6:使用ipv6;

-B:以批处理模式运行;

-C:使用压缩;

-F:指定ssh配置文件;

-l:指定宽带限制;

-o:指定使用的ssh选项;

-P:指定远程主机的端口号;

-p:保留文件的最后修改时间,最后访问时间和权限模式;

-q:不显示复制进度;

-r:以递归方式复制。

参数

源文件:指定要复制的源文件。
目标文件:目标文件。格式为user@host:filename(文件名为目标文件的名称)。


常用范例

scp命令的实际应用概述

从本地服务器复制到远程服务器

(1) 复制文件:

命令格式:

scp local_file remote_username@remote_ip:remote_folder

或者

scp local_file remote_username@remote_ip:remote_file

或者

scp local_file remote_ip:remote_folder

或者

scp local_file remote_ip:remote_file

第1,2个指定了用户名,命令执行后需要输入用户密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名

第3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名

(2) 复制目录:

命令格式:

scp -r local_folder remote_username@remote_ip:remote_folder

或者

scp -r local_folder remote_ip:remote_folder

第1个指定了用户名,命令执行后需要输入用户密码;

第2个没有指定用户名,命令执行后需要输入用户名和密码;

从远程服务器复制到本地服务器

从远程复制到本地的scp命令与上面的命令雷同,只要将从本地复制到远程的命令后面2个参数互换顺序就行了。

实例一:从远处复制文件到本地目录

命令:

scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/

输出:

[root@localhost ~]# cd /opt/soft/

[root@localhost soft]# ll

总计 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 root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/

root@192.168.120.204'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]#

说明:

从192.168.120.204机器上的/opt/soft/的目录中下载nginx-0.5.38.tar.gz 文件到本地/opt/soft/目录中

实例二:从远处复制到本地

命令:

scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/

输出:

[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]# scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/

root@192.168.120.204'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]#

说明:

从192.168.120.204机器上的/opt/soft/中下载mongodb 目录到本地的/opt/soft/目录来。

实例三:上传本地文件到远程机器指定目录

命令:

scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest

输出:

上传前目标机器的目标目录:

[root@localhost soft]# cd scptest/

[root@localhost scptest]# ll

总计 0

[root@localhost scptest]# ll

本地机器上传:

[root@localhost soft]# scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest

root@192.168.120.204's password:

nginx-0.5.38.tar.gz 100% 479KB 478.7KB/s 00:00

[root@localhost soft]#

上传后目标机器的目标目录:

[root@localhost scptest]# ll

总计 484

-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz

[root@localhost scptest]#

说明:

复制本地opt/soft/目录下的文件nginx-0.5.38.tar.gz 到远程机器192.168.120.204的opt/soft/scptest目录

实例四:上传本地目录到远程机器指定目录

命令:

scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest

输出:

上传前目标机器的目标目录:

[root@localhost ~]# cd /opt/soft/scptest/

[root@localhost scptest]# ll

总计 484

-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz

[root@localhost scptest]#

本地机器上传:

[root@localhost ~]# scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest

root@192.168.120.204'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 ~]#

上传后目标机器的目标目录:

[root@localhost scptest]# ll

总计 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]#

说明:

上传本地目录 /opt/soft/mongodb到远程机器192.168.120.204上/opt/soft/scptest的目录中去
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: