linux怎么从另一个服务器拷贝文件
linux怎么从另一个服务器拷贝文件详细介绍
在 Linux 系统里,你可以借助多种工具从另一个服务器拷贝文件,下面为你介绍几种常见的方法。
1. 使用
scp(安全拷贝)
scp是最为常用的工具,它基于 SSH 协议,能安全地在不同服务器间拷贝文件。
从远程服务器拷贝文件到本地
bash
username@remote_host:/path/to/remote/file /path/to/local/directory
username:远程服务器的用户名。remote_host:远程服务器的 IP 地址或者域名。/path/to/remote/file:远程服务器上文件的路径。/path/to/local/directory:本地接收文件的目录。
从本地拷贝文件到远程服务器
bash
/path/to/local/file username@remote_host:/path/to/remote/directory
拷贝整个目录
若要拷贝整个目录,可使用-r选项(递归拷贝):
bash
username@remote_host:/path/to/remote/directory /path/to/local/directory
2. 使用
rsync
rsync也是一个强大的文件同步工具,它能够高效地拷贝和同步文件,而且可以只传输文件的差异部分,从而节省带宽。
从远程服务器拷贝文件到本地
bash
username@remote_host:/path/to/remote/file /path/to/local/directory
-a:归档模式,保留文件的所有属性,如权限、时间戳等。-v:详细模式,显示拷贝过程。-z:压缩传输,减少带宽使用。
从本地拷贝文件到远程服务器
bash
/path/to/local/file username@remote_host:/path/to/remote/directory
拷贝整个目录
bash
username@remote_host:/path/to/remote/directory/ /path/to/local/directory
注意,远程目录后面的斜杠/表示只拷贝目录内的内容,而不包括目录本身。
3. 使用
sftp(安全文件传输协议)
sftp是一个交互式的文件传输工具,同样基于 SSH 协议。
连接到远程服务器
bash
username@remote_host
下载文件
bash
get /path/to/remote/file /path/to/local/directory
上传文件
bash
put /path/to/local/file /path/to/remote/directory
912put /path/to/local/file /path/to/remote/directory
退出
sftp会话
bash
912
以上这些方法都需要你拥有远程服务器的访问权限,并且确保远程服务器的 SSH 服务处于开启状态。