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之间传文件和目录

相关推荐
ICscholar6 小时前
ExaDigiT/RAPS
linux·服务器·ubuntu·系统架构·运维开发
sim20206 小时前
systemctl isolate graphical.target命令不能随便敲
linux·mysql
薛定谔的猫19826 小时前
RAG(二)基于 LangChain+FAISS + 通义千问搭建轻量级 RAG 检索增强生成系统
运维·服务器·langchain
米高梅狮子7 小时前
4. Linux 进程调度管理
linux·运维·服务器
再创世纪8 小时前
让USB打印机变网络打印机,秀才USB打印服务器
linux·运维·网络
fengyehongWorld8 小时前
Linux ssh端口转发
linux·ssh
昨夜见军贴06169 小时前
IACheck AI审核如何实现自动化来料证书报告审核,全面提升生产效率与合规水平
运维·人工智能·自动化
知识分享小能手10 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的Shell编程详细知识点(含案例代码)(17)
linux·学习·ubuntu
KKKlucifer10 小时前
国内API接口安全市场发展观察:技术演进与厂商生态格局
安全
浩子智控10 小时前
电子产品设计企业知识管理
运维·服务器·eclipse·系统安全·硬件工程