Linux scp命令教程:如何安全地在Linux机器之间复制文件(附案例详解和注意事项)

Linux scp命令介绍

scp命令是Secure Copy的缩写,它是一个基于SSH的命令行工具,用于在两个位置之间安全地复制文件和目录。使用scp,你可以从本地系统复制文件或目录到远程系统,从远程系统复制文件或目录到本地系统,或者从本地系统复制文件或目录到两个远程系统。

Linux scp命令适用的Linux版本

scp命令在大多数Linux发行版中都可以使用,包括但不限于Ubuntu, Debian, Fedora, CentOS等。如果你的系统中没有预装scp命令,你可以通过包管理器来安装它。例如,在基于Debian的系统中,你可以使用apt-get命令来安装:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo apt-get install openssh-client

在基于RHEL的系统中,你可以使用yumdnf命令来安装:

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo yum install openssh-clients

或者

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo dnf install openssh-clients

Linux scp命令的基本语法

scp命令的基本语法如下:

bash 复制代码
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

其中,OPTIONscp命令的选项,如密码,SSH配置,SSH端口,限制,递归复制等¹。[user@]SRC_HOST:]file1是源文件,[user@]DEST_HOST:]file2是目标文件¹。

Linux scp命令的常用选项或参数说明

scp命令提供了许多选项来控制其行为。以下是一些最常用的选项:

  • -P:指定远程主机的SSH端口。
  • -p:保留文件的修改和访问时间。
  • -q:如果你想要禁止进度条和非错误消息,可以使用此选项。
  • -C:此选项强制scp在将数据发送到目标机器时进行压缩。
  • -r:此选项告诉scp递归地复制目录。

Linux scp命令的实例

实例1:从本地复制文件到远程系统

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp file.txt remote_username@10.10.0.2:/remote/directory

file.txt是我们要复制的文件名,remote_username是远程服务器上的用户,10.10.0.2是服务器的IP地址。/remote/directory是你要复制文件的路径¹。

实例2:从远程系统复制文件到本地

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp remote_username@10.10.0.2:/remote/directory/file.txt /local/directory

从远程系统复制文件到本地系统。

实例3:使用-P选项指定SSH端口

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp -P 2222 file.txt remote_username@10.10.0.2:/remote/directory

使用-P选项来指定SSH端口为2222。

实例4:使用-p选项保留文件的修改和访问时间

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp -p file.txt remote_username@10.10.0.2:/remote/directory

使用-p选项来保留文件的修改和访问时间。

实例5:使用-q选项禁止进度条和非错误消息

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp -q file.txt remote_username@10.10.0.2:/remote/directory

使用-q选项来禁止进度条和非错误消息。

实例6:使用-C选项进行压缩

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp -C file.txt remote_username@10.10.0.2:/remote/directory

使用-C选项来在将数据发送到目标机器时进行压缩。

实例7:使用-r选项递归地复制目录

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ scp -r /local/directory remote_username@10.10.0.2:/remote/directory

使用-r选项来递归地复制目录。

Linux scp命令的注意事项

  • 当使用scp传输数据时,文件和密码都会被加密,因此任何在网络上窥探的人都无法获取任何敏感信息。
  • 在复制大文件时,建议在screen或tmux会话中运行scp命令。
  • 如果你在使用scp命令时遇到了bash: scp: command not found的错误,那么你可能需要安装openssh-client包。

更多详细内容可以参考:
linux入门学习教程 - Linux入门自学网
Linux scp命令详解:安全高效地在服务器及Windows之间传文件和目录

相关推荐
Johny_Zhao7 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
用户9623779544812 小时前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机15 小时前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机15 小时前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户9623779544816 小时前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star16 小时前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户9623779544820 小时前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
chlk1232 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux