scp是secure copy的简写,用于在linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你的服务器变为只读read only system时,用scp可以帮助你把文件移动出来。另外,scp不会额外占用资源,不会提高多少系统负荷,在这一点上rsync就远远不及它了。虽然rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统使用。
【命令格式】
scp [参数] [原路径] [目标路径]
【命令功能】
scp是linux系统下基于ssh登录进行安全的远程文件拷贝命令;可理解为scp命令可以在linux服务器之间复制文件或目录。
【命令参数】
-1 强制scp命令使用协议ssh1
-2 强制scp命令使用协议ssh2
-4 强制scp命令只使用IPv4寻址
-6 强制scp命令只使用IPv6寻址
-B 使用批处理模式(传输过程中不询问传输口令或短语)
-C 允许压缩(将-C标志传递给ssh,从而打开压缩功能)
-p 保留源文件的修改时间,访问时间和权限。
-q 不显示传输进度条
-r 递归复制整个目录
-v 详细方式显示传输(scp和ssh(1)会显示整个过程的调试信息,这些信息用于调试连接,验证和配置问题。
-c cipher 以cipher将数据传输进行加密,这个选项将直接传递给ssh
-F ssh_config 指定一个替换的ssh配置文件,此参数直接传递给ssh
-i identity_file 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l limit 限定用户所能使用的带宽,以kb/s为单位。
-P port 注意是大写的P,port是指定数据传输用到的端口号
-S program 指定加密传输时所使用的程序,此程序必须能够理解ssh(1)的选项
【使用案例】
从本地主机到远程主机:
[root@node ~]# scp puppet-server-2.7.23-1.el6.noarch.rpm 172.16.18.7:/root root@172.16.18.7's password: puppet-server-2.7.23-1.el6.noarch.rpm 100% 25KB 25.0KB/s 00:00 [root@node1 ~]# ls anaconda-ks.cfg install.log.syslog install.log puppet-server-2.7.23-1.el6.noarch.rpm
[root@node ~]# scp -r puppets 172.16.18.7:/root root@172.16.18.7's password: user.pp 100% 146 0.1KB/s 00:00 notify.pp 100% 50 0.1KB/s 00:00 exec.pp 100% 145 0.1KB/s 00:00 group.pp 100% 288 0.3KB/s 00:00 class2.pp 100% 492 0.5KB/s 00:00 class.pp 100% 424 0.4KB/s 00:00 package.pp 100% 118 0.1KB/s 00:00 file.pp 100% 301 0.3KB/s 00:00 case.pp 100% 186 0.2KB/s 00:00 class3.pp 100% 799 0.8KB/s 00:00 if.pp 100% 279 0.3KB/s 00:00 [root@node1 ~]# ls anaconda-ks.cfg puppets install.log puppet-server-2.7.23-1.el6.noarch.rpm install.log.syslog
从远程主机到本地主机:
[root@node ~]# scp 172.16.18.7:/etc/my.cnf /root root@172.16.18.7's password: my.cnf 100% 251 0.3KB/s 00:00 [root@node ~]# ls anaconda-ks.cfg my.cnf
复制过程不使用密码:
#######主机一########### [root@node ~]# ssh-keygen -t rsa -P '' Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: d7:7b:32:0a:7c:d9:42:2b:fd:ea:3f:8f:66:77:44:d7 root@node.magedu.com The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | | | | | .| | . E| | S o . ..| | . + + . .| | + * = .. | | + ++=. .| | .+=++.. | +-----------------+ [root@node ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.18.7 root@172.16.18.7's password: Now try logging into the machine, with "ssh 'root@172.16.18.7'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. #######主机二####### [root@node1 ~]# ssh-keygen -t rsa -P '' Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 83:09:ad:d0:cc:39:f4:88:6c:89:87:fe:04:6f:e6:20 root@node1.magedu.com The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | . | | + B = | |o.B O o | |.oo. + o | |Eo =. o S | |. B . | | o | | | | | +-----------------+ [root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.18.6 The authenticity of host '172.16.18.6 (172.16.18.6)' can't be established. RSA key fingerprint is 34:31:7a:4d:20:c9:c7:40:4b:80:cd:d2:9c:27:12:27. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.18.6' (RSA) to the list of known hosts. root@172.16.18.6's password: Now try logging into the machine, with "ssh 'root@172.16.18.6'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. #######复制######## [root@node ~]# scp my.cnf 172.16.18.7:/root my.cnf 100% 251 0.3KB/s 00:00
不同用户之间的远程复制:
[root@node ~]# scp my.cnf zhao@172.16.18.7:/home/zhao/ #指定用户zhao zhao@172.16.18.7's password: my.cnf 100% 251 0.3KB/s 00:00 [zhao@node1 ~]$ ls #登陆zhao用户上 my.cnf
更多关于scp的知识接触到时将后续更新!!
转载于:https://blog.51cto.com/pangge/1332138