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

linux系统间文件传输命令--scp

2015-12-17 21:56 489 查看
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,
而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,
scp还基本不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,
rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。
1.命令格式:
scp [参数] [原路径] [目标路径]
2.命令功能:
scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录
3. 命令参数(列出几个常用的参数,具体详细的参数可以用man scp 来查看文档!
-p 保留原文件的修改时间,访问时间和访问权限。
-q 不显示传输进度条。
-r 递归复制整个目录。
-v 详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
-P "port"注意是大写的P,port是指定数据传输用到的端口号!
4.使用实例:
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个参数互换顺序就行了。
实例1:
[root@lianxi2 11]# touch scp
[root@lianxi2 11]# ls
scp
[root@lianxi2 11]# scp -v /root/11/scp root@192.168.1.112:/root/22         /把文件scp传输到ip为192.168.1.112的/root/22目录下
Executing: program /usr/bin/ssh host 192.168.1.112, user root, command scp -v -t /root/22
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
```````
root@192.168.1.112's password:                                   /输入密码
```````
Transferred: sent 1688, received 2128 bytes, in 0.0 seconds
Bytes per second: sent 57179.8, received 72084.5
debug1: Exit status 0
[root@lianxi2 11]#
/查看传输的结果/
[root@xinlianxi 22]# ls
scp
实例2:把本地的目录传到远程机器上:
[root@lianxi2 11]# mkdir -p test/test1/test2 创建一个目录
[root@lianxi2 11]# tree test
test
└── test1
└── test2

2 directories, 0 files
[root@lianxi2 11]# scp /root/11/test root@192.168.1.112:/root/22   /不加-r选项传递时,因为没有显示传递信息,故会等一会时间,才会显示下一行
Address 192.168.1.112 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.112's password:               /输入密码后会发现,传输出错
/root/11/test: not a regular file
[root@lianxi2 11]# scp -r /root/11/test root@192.168.1.112:/root/22            /加上-r选项
Address 192.168.1.112 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.112's password:
[root@lianxi2 11]#
查看传输的结果:
[root@xinlianxi 22]# tree test
test
└── test1
└── test2

2 directories, 0 files
[root@xinlianxi 22]#
实例3:把远程机器上的目录拉过来:
[root@lianxi2 11]# scp -r /root/11/test root@192.168.1.112:/root/22
Address 192.168.1.112 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.112's password:
[root@lianxi2 11]# scp -r root@192.168.1.112:/root/sbin /root
Address 192.168.1.112 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.112's password:
!                                                            100%  114     0.1KB/s   00:00
3.1.sh                                                         100%  736     0.7KB/s   00:00
3.sh                                                           100% 3090     3.0KB/s   00:00
1.sh                                                           100% 2743     2.7KB/s   00:00
2.sh                                                           100%  663     0.7KB/s   00:00
[root@lianxi2 11]#
查看结果:
[root@lianxi2 11]# ls /root
anaconda-ks.cfg                 epel-release-6-8_32.noarch.rpm.1  install.log.syslog  sbin
[root@lianxi2 11]#


本文出自 “自定义” 博客,请务必保留此出处http://zidingyi.blog.51cto.com/10735263/1725828
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: