linux-scp用法

发布时间 2023-10-03 20:18:36作者: ⭐⭐-fighting⭐⭐

scp(Secure Copy Protocol)是一个用于在本地系统和远程系统之间安全地传输文件的命令行工具。它基于 SSH 协议,能够加密传输数据,提供了对文件传输的安全支持。下面详细介绍 scp 的用法。

1. 将本地文件复制到远程主机

scp [options] <local_file> <username@remote_host>:<remote_path>
  • [options]: 可选参数,如 -r(递归复制)等。
  • <local_file>: 要复制的本地文件或目录。
  • <username@remote_host>: 远程主机的用户名和主机名/IP地址。
  • <remote_path>: 远程主机上保存文件的路径。

2. 将远程文件复制到本地

scp [options] <username@remote_host>:<remote_file> <local_path>
  • [options]: 可选参数,如 -r(递归复制)等。
  • <username@remote_host>: 远程主机的用户名和主机名/IP地址。
  • <remote_file>: 要复制的远程文件路径。
  • <local_path>: 本地保存文件的路径。

3. 复制文件夹及其内容

如果要复制整个目录及其内容,需要加上 -r 参数来进行递归复制。

scp -r <local_directory> <username@remote_host>:<remote_path>

或者从远程主机复制到本地:

scp -r <username@remote_host>:<remote_directory> <local_path>

4. 使用非默认 SSH 端口

如果远程 SSH 服务器不是默认的 22 端口,可以使用 -P 参数指定端口。

scp -P <port> <local_file> <username@remote_host>:<remote_path>

或者从远程主机复制到本地:

scp -P <port> <username@remote_host>:<remote_file> <local_path>

5. 其他常用选项

  • -v: 显示详细的输出,用于调试。
  • -q: 静默模式,不显示输出。
  • -C: 使用压缩传输数据。
  • -i <identity_file>: 指定身份验证文件(私钥)。
  • --limit=<speed>: 限制传输速度。

示例

  1. 将本地文件复制到远程主机:
scp /path/to/local/file.txt username@remote_host:/path/to/remote/
  1. 将远程文件复制到本地:
scp username@remote_host:/path/to/remote/file.txt /path/to/local/
  1. 复制目录及其内容到远程主机:
scp -r /path/to/local/directory username@remote_host:/path/to/remote/
  1. 复制目录及其内容到本地:
scp -r username@remote_host:/path/to/remote/directory /path/to/local/