
别再乱传文件Windows↔Linux、Linux间文件复制与同步全攻略系统间复制文档一、windows 和 Linux 之间传输1.xftp 工具2.xshell 工具二、Linux 之间传输1.scp 命令2.rsync 命令shell 模式daemon 模式系统间复制文档摘要本文详细介绍了在不同操作系统之间复制文档的多种方法。内容涵盖 Windows 与 Linux 之间的文件传输使用 xftp 和 xshell 工具以及 Linux 系统之间的文件同步使用 scp 和 rsync 命令。文章通过丰富的命令行示例对比了 scp 的全量复制与 rsync 的增量同步特性并提供了 shell 模式和 daemon 模式两种 rsync 使用方式的说明帮助读者根据实际场景选择最合适的文件传输方案。一、windows 和 Linux 之间传输1.xftp 工具在两个窗口之间拖动文件进行上传和下载。2.xshell 工具Linux服务器安装 lrzsz 软件包。该方式传输速度比ftp慢。rootclient ~12:19:27]# yum install -y lrzsz已加载插件fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com...上传直接将文件拖拽到命令行窗口即可。拉取使用 sz 工具并指定保存位置。二、Linux 之间传输1.scp 命令scp全名secure copy也就是安全复制是基于ssh协议也就是说Linux服务器要开启ssh服务。为了方便上传和下载提前配置好秘钥登录服务器。#配置秘钥登录服务器[rootclient ~12:30:37]# ssh-keygenGenerating public/private rsa key pair. Enterfileinwhichto save the key(/root/.ssh/id_rsa): Created directory/root/.ssh.Enter passphrase(emptyforno passphrase): Enter same passphrase again: Your identification has been savedin/root/.ssh/id_rsa. Your public key has been savedin/root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:ZV23gP/Kpe8MTcuLes4TF88DgSyRnasH6rbwiaxHBjg rootclient.lz.cloud The keys randomart image is: ---[RSA 2048]---- | . .. .| | o B o...| | . .. | | E . . o . | | . . S o ooo| | o . . . .| | o.. . ..*.| | ..o. .* .| | .oo.. . | ----[SHA256]----- [rootclient ~ 12:31:00]# ssh-copy-id rootserver /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: /root/.ssh/id_rsa.pub /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys rootservers password: Number of key(s)added:1Now try logging into the machine, with:ssh rootserverand check tomakesure that only the key(s)you wanted were added.[rootclient ~12:31:21]# scp rootserver:/root/etc-20260515.tar .etc-20260515.tar100% 29MB34.7MB/s 00:00[rootclient ~12:35:20]# ls etc*etc-20260515.tar# 复制多个文件[rootclient ~12:42:39]# scp rootserver:/root/{etc-20260515.tar,etc.tar} .etc-20260515.tar100% 29MB42.7MB/s 00:00 etc.tar100% 29MB46.2MB/s 00:00[rootclient ~12:42:57]# ls etc*etc-20260515.tar etc.tar# 复制目录使用 -r 选项[rootclient ~12:43:00]# scp rootserver:/etc/selinux/ .scp: /etc/selinux: not a regularfile[rootclient ~12:43:59]# scp -r rootserver:/etc/selinux/ .semanage.conf100%2321769.0KB/s 00:00 config100%542835.8KB/s 00:00 cil100%77923.1MB/s 00:00 hll100% 17KB9.0MB/s 00:00...# 上传如果路径没有写表示相对用户家目录。这里是上传到root用户家目录/root[rootclient ~12:45:12]# scp etc.tar rootserver:etc.tar100% 29MB41.1MB/s 00:00[rootclient ~12:48:02]# scp etc.tar etc-20260515.tar rootserver:etc.tar100% 29MB50.0MB/s 00:00 etc-20260515.tar100% 29MB58.6MB/s 00:00[rootclient ~12:48:26]# scp -r selinux/ rootserver:semanage.conf100%2321525.0KB/s 00:00 config100%542412.5KB/s 00:00 cil100%77928.5MB/s 00:00 hll100% 17KB10.1MB/s 00:00...scp 命令缺点不管目的位置是否有文件总是再复制一次可以理解为全量备份。缺少比对功能。2.rsync 命令RsyncRemote Synchronize是一款开源的、快速的、多功能的、远程数据同步备份工具并且支持多种操作系统平台运行。Rsync具有本地与远程两台主机之间的数据快速复制同步镜像、远程备份等功能该功能类似scp但是优于scp功能还具有本地不同分区目录之间全量及增量复制数据。Rsync同步数据镜像时通过“quick check”算法仅同步大小或最后修改时间发生变化的文件或目录当然也可以根据权限属主等属性变化的同步所以可以实现快速同步。前提客户端和服务端都要提前安装好 rsync 软件包。[rootclient ~12:51:49]# yum install -y rsync已加载插件fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com...shell 模式shell 模式是基于 sshd 服务传送。语法rsync[OPTION]… SRC DESTrsync[OPTION]… SRC[USER]HOST:DESTrsync[OPTION]…[USER]HOST:SRC DEST常用选项-n 参数执行空运行与真实执行显示结果一致但是没有做任何改变。-v 显示执行过程中详细输出。-a 代表“archive mode”同时启用参数-r -l -p -t -g -o -D-r 递归同步整个文件夹-l 同步软连接-p 保留权限-t 保留时间戳-g 保留所属组-o 保留所有者-D 同步设备文件说明-A同步时保留ACLs内容。-X 同步时保留selinux内容。-a不同步acl selinux内容。# 准备文件[rootclient ~13:26:01]# mkdir Pictures[rootclient ~13:26:18]# touch Pictures/snap{1..5}.jpg# 首次同步[rootclient ~13:26:40]# rsync -av Pictures rootserver:sending incrementalfilelist Pictures/ Pictures/snap1.jpg Pictures/snap2.jpg Pictures/snap3.jpg Pictures/snap4.jpg Pictures/snap5.jpg sent351bytes received115bytes932.00bytes/sec total size is0speedup is0.00# 再次同步[rootclient ~13:27:04]# rsync -av Pictures rootserver:sending incrementalfilelist sent153bytes received17bytes113.33bytes/sec total size is0speedup is0.00# 更新部分文件时间戳再同步[rootclient ~13:27:09]# touch Pictures/snap{1,2}*[rootclient ~13:27:28]# rsync -av Pictures rootserver:sending incrementalfilelist Pictures/snap1.jpg Pictures/snap2.jpg sent239bytes received55bytes588.00bytes/sec total size is0speedup is0.00# 删除 SRC 中文件默认不会同步删除DEST中文件[rootclient ~13:27:31]# rm -f Pictures/snap5.jpg[rootclient ~13:27:48]# rsync -av Pictures rootserver:sending incrementalfilelist Pictures/ sent143bytes received20bytes326.00bytes/sec total size is0speedup is0.00# 使用选项 --delete 同步删除 DEST 中文件[rootclient ~13:27:51]# rsync -av Pictures rootserver: --deletesending incrementalfilelist deleting Pictures/snap5.jpg sent144bytes received35bytes358.00bytes/sec total size is0speedup is0.00将目标文件拉取到本地操作过程一致。同步注意事项• 文件访问时间等属性、读写等权限、文件内容等有任何变动都会被认为修改。• 目标目录下如果文件比源目录还新则不会同步。• 源路径的最后是否有斜杠有不同的含义• 有斜杠只是复制目录中的文件。• 没有斜杠的话不但要复制目录中的文件还要复制目录本身。daemon 模式daemon 模式不依赖 ssh 服务。daemon 模式要求服务端部署 rsynrc 服务。• 语法1源路径或目的路径的主机名后面包含两个冒号。• 语法2rsync://URL格式。rsync[OPTION]… SRC[USER]HOST::DESTrsync[OPTION]…[USER]HOST::SRC DESTrsync[OPTION]… rsync://[USER]HOST[:PORT]/SRC[DEST]