SSH 远程文件/目录传输命令SCP示例详解

需求

  1. 同网段机器之间,如何使用SSH连接快速传输文件(或目录)。
  2. 数据最好以加密方式传输。

解决

SCP命令,采用同ssh的加密方式传输数据,在同个网络内能很方便的跨机器传输数据。

SCP - secure copy (remote file copy program)
scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication and provides the same security as ssh(1). Unlike rcp(1), scp will ask for passwords or passphrases if they are needed for authentication.

示例

tips
1. 注意确保文件和目录都存在。
2. @连接用户名和IP,连接IP和路径名。
3. 上传的命令参数顺序:把什么(本地)上传到哪
4. 下载的命令参数顺序:把什么(远程)下载到哪

1. 上传文件

scp 要上传什么文件 以什么用户@上传到什么服务器:的什么位置

[root@CRXJ-COLL-1 src_dir]# scp ./install.log [email protected]:/root/des_dir/install.log.bak
[email protected]'s password:
install.log                                                                            100%   62KB  62.4KB/s   00:00 
[root@CRXJ-COLL-1 src_dir]#

 

2. 上传整个目录

scp -r 要上传什么目录 以什么用户@上传到什么服务器:的什么位置

-r 递归复制

[root@CRXJ-COLL-1 ~]# scp -r  ./src_dir/ [email protected]:/root/
[email protected]'s password:
install.log                                                                            100%   62KB  62.4KB/s   00:00   
[root@CRXJ-COLL-1 ~]#

 

3. 下载文件

scp 从什么用户@什么服务器:的什么文件 下载到什么位置

[root@CRXJ-COLL-1 src_dir]# scp [email protected]:/root/des_dir/install.log.bak /root/src_dir/
[email protected]'s password:
install.log.bak                                                                        100%   62KB  62.4KB/s   00:00   
[root@CRXJ-COLL-1 src_dir]#

 

4. 下载整个目录

scp -r 从什么用户@什么服务器:的什么目录 下载到什么位置

扫描二维码关注公众号,回复: 1146617 查看本文章

-r 递归复制

 

[root@CRXJ-COLL-1 ~]# scp -r [email protected]:/root/des_dir/ /root/
[email protected]'s password:
install.log.bak                                                                        100%   62KB  62.4KB/s   00:00   
[root@CRXJ-COLL-1 ~]#

 

5. 可能的错误

  1. ./src_dir: not a regular file 目标文件夹不存在

6. 其他可能用到的参数

  1. -p : 指定端口
  2. -v : 显示进度
  3. -C : 压缩选项
  4. -4 : 强行使用 IPV4 地址 .
  5. -6 : 强行使用 IPV6 地址 .

猜你喜欢

转载自futeng.iteye.com/blog/2065847