Linux基础命令-scp远程复制文件

news2024/9/23 15:19:42

Linux基础命令-seq打印数字序列

前言

有时候不可避免的需要将文件复制到另外一台服务器上,那么这时就可以使用scp命令远程拷贝文件,scp命令是基于SSH协议,在复制的过程中数据都是加密过的,会比明文传输更为安全。

一.命令介绍

依旧惯例,先到scp的帮助文档中查看命令的概述

NAME
     scp — secure copy (remote file copy program)
DESCRIPTION
     scp copies files between hosts on a network.  It uses ssh(1) for data transfer, and uses the same
     authentication and provides the same security as ssh(1).  scp will ask for passwords or passphrases if
     they are needed for authentication.

     File names may contain a user and host specification to indicate that the file is to be copied to/from
     that host.  Local file names can be made explicit using absolute or relative pathnames to avoid scp
     treating file names containing ‘:’ as host specifiers.  Copies between two remote hosts are also per‐
     mitted.

scp(secure copy)命令,主要功能是用来远程拷贝文件,可以在多台Linux系统之间复制文件或目录,有些类似于cp命令的功能,但复制的范围是网络上的另一台主机。

二. 命令语法

scp命令语法格式:scp 参数 文件

SYNOPSIS
     scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port]
         [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

三. 常用参数

文档中一共有这么多的参数,总结一些比较常用的;

     -1      Forces scp to use protocol 1.

     -2      Forces scp to use protocol 2.

     -3      Copies between two remote hosts are transferred through the local host.  Without this option
             the data is copied directly between the two remote hosts.  Note that this option disables the
             progress meter.

     -4      Forces scp to use IPv4 addresses only.

     -6      Forces scp to use IPv6 addresses only.

     -B      Selects batch mode (prevents asking for passwords or passphrases).

     -C      Compression enable.  Passes the -C flag to ssh(1) to enable compression.

     -c cipher
             Selects the cipher to use for encrypting the data transfer.  This option is directly passed to
             ssh(1).

     -F ssh_config
             Specifies an alternative per-user configuration file for ssh.  This option is directly passed
             to ssh(1).

     -i identity_file
             Selects the file from which the identity (private key) for public key authentication is read.
             This option is directly passed to ssh(1).

     -l limit
             Limits the used bandwidth, specified in Kbit/s.

     -o ssh_option
             Can be used to pass options to ssh in the format used in ssh_config(5).  This is useful for
             specifying options for which there is no separate scp command-line flag.  For full details of
             the options listed below, and their possible values, see ssh_config(5).

                   AddressFamily
                   BatchMode
                   BindAddress
                   CanonicalDomains
                   CanonicalizeFallbackLocal
                   CanonicalizeHostname
                   CanonicalizeMaxDots
                   CanonicalizePermittedCNAMEs
                   CertificateFile
                   ChallengeResponseAuthentication
                   CheckHostIP
                   Cipher
                   Ciphers
                   Compression
                   CompressionLevel
                   ConnectionAttempts
                   ConnectTimeout
                   ControlMaster
     -2      Forces scp to use protocol 2.

     -3      Copies between two remote hosts are transferred through the local host.  Without this option
             the data is copied directly between the two remote hosts.  Note that this option disables the
             progress meter.

     -4      Forces scp to use IPv4 addresses only.

     -6      Forces scp to use IPv6 addresses only.

     -B      Selects batch mode (prevents asking for passwords or passphrases).

     -C      Compression enable.  Passes the -C flag to ssh(1) to enable compression.

     -c cipher
             Selects the cipher to use for encrypting the data transfer.  This option is directly passed to
             ssh(1).

     -F ssh_config
             Specifies an alternative per-user configuration file for ssh.  This option is directly passed
             to ssh(1).

     -i identity_file
             Selects the file from which the identity (private key) for public key authentication is read.
             This option is directly passed to ssh(1).

     -l limit
             Limits the used bandwidth, specified in Kbit/s.

     -o ssh_option
             Can be used to pass options to ssh in the format used in ssh_config(5).  This is useful for
             specifying options for which there is no separate scp command-line flag.  For full details of
             the options listed below, and their possible values, see ssh_config(5).

常用参数:

命令选项含义
-1使用ssh协议版本1
-2使用ssh协议版本2
-4使用ipv4
6使用ipv6
-B以批处理模式运行
-C使用压缩
-F指定ssh配置文件
-l指定带宽限制
-o指定使用的ssh选项
-P指定远程主机的端口号
-p保留文件的修改时间,访问时间和权限模式
-q不显示复制进度
-r以递归的方式进行复制

四. 参考实例

4.1 从本地远程复制到另一台服务器

4.1.1 拷贝文件到远程服务器

命令格式: 拷贝文件

第一种方式:指定用户名,将文件远程拷贝到另外一台服务器的目录下,命令执行后再输入密码。
scp local_file remote_username@remote_ip:remote_folder

第二种方式:指定用户名,将文件远程拷贝到另外一台服务器下,文件可以修改成其他名字,相当于远程拷贝重命名;命令执行后在输入密码。
scp local_file remote_username@remote_ip:remote_file

第三种方式:不指定用户名,将文件远程拷贝到另外一台服务器的目录下,命令执行后需要输入用户名和密码
scp local_file remote_ip:remote_folder

第四种方式:不指定用户名,将文件远程拷贝到另外一台服务器下,文件可以修改成其他名字,相当于远程拷贝重命名;命令执行后在输入密码。
scp local_file remote_ip:remote_file

案例演示:

scp /usr/local/nginx/conf/nginx.conf root@192.168.45.128:/usr/local/nginx/conf

scp /usr/local/nginx/conf/nginx.conf root@192.168.45.128:/usr/local/nginx/conf/nginx.conf.backup

scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf

scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf.backup

4.1.2 拷贝目录到远程服务器

命令格式: 拷贝目录

第一种方式:指定用户名,将目录远程拷贝到另外一台服务器的指定目录下,需要使用-r参数表示递归操作,命令执行后再输入密码
scp -r local_folder remote_username@remote_ip:remote_folder

第二种方式:不指定用户名,将目录远程拷贝到另外一台服务器的指定目录下,需要使用-r参数表示递归操作,命令执行后再输入密码
scp -r local_folder remote_ip:remote_folder

案例演示:

scp -r /tmp/ root@192.168.45.128:/
scp -r /tmp/ 192.168.45.128:/

4.2 从远程复制到本地服务器

从 远程复制到本地,只要将从本地复制到远程的命令的后2个参数 调换顺序即可,一起来看下。

4.2.1 远程服务器的文件拷贝到本地

第一种方式:指定远程用户名@指定IP:要复制的文件到./,这里./表示将远程服务器上的文件拷贝到本地服务器的当前路径。
scp remote_username@remote_ip:remote_file ./

第二种方式:指定远程用户名@指定IP:要复制的文件到本地服务器指定用户名,指定目录等
scp remote_username@remote_ip:remote_file local_username@local_ip:local_folder

案例演示:

scp root@192.168.45.128:/usr/local/nginx/conf/nginx.conf 
/usr/local/conf/
scp -r root@192.168.45.128:/usr/local/nginx/ root@192.168.45.166:/usr/local/

当然也可以拷贝目录,方法还是一样在前面加-r参数,这里就不再演示。

除了可以选择IP来拷贝文件,还可以使用主机名进行拷贝,前提需要先将hostname做hosts映射。

cat /etc/hosts
192.168.45.166 localhost166
192.168.45.128 localhost128

scp -r root@localhost128:/usr/local/nginx/ root@localhost166:/usr/local

总结

scp命令也是经常会需要用到的,只需要了解从本地到远程,或从远程到本地的是如何使用即可,若觉得以上内容还行,可以点赞支持一下!
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/440082.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vue ElementUI Axios 前后端案例(day02) 之 ElementUI

ElementUI Element,一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库 组件 1.Layout 布局 通过基础的 24 分栏,迅速简便地创建布局。 就是这样分了24个格子 基础布局 使用单一分栏创建基础的栅格布局。 通过 row 和 col 组件&…

keepalived+nginx安装

欢迎使用ShowDoc! 1、安装基础包: yum -y install libnl libnl-devel 2、上传包: tar -zxvf keepalived-2.0.20.tar.gz -C /data/imas/base_soft mkdir -p /data/imas/base_soft/keepalived cd /data/imas/base_soft/keepalived-2.0.20 .…

基于Netty开发IM即时通讯之群聊功能

本篇涉及的群聊核心功能,大致如下所示: 1)登录:每个客户端连接服务端的时候,都需要输入自己的账号信息,以便和连接通道进行绑定;2)创建群组:输入群组 ID 和群组名称进行…

【云原生进阶之容器】第六章容器网络6.5.2--Calico网络架构详述

《云原生进阶之容器》专题索引: 第一章Docker核心技术1.1节——Docker综述第一章Docker核心技术1.2节——Linux容器LXC第一章Docker核心技术1.3节——命名空间Namespace第一章Docker核心技术1.4节——chroot技术第一章Docker核心技术1.5.1节——cgroup综述

从 Dev 和 Ops 视角出发,聊聊 DevSecOps 的 What / Why / How

近日,极小狐和 TA 的朋友们相聚上海,开展了一场技术 Meetup,从 DevSecOps 的 What、Why、How 出发,通过分享真实应用案例,与参会者交流 DevSecOps 的实践过程和落地经验。 本文整理自极狐(GitLab) 资深云原生架构师郭旭…

爬虫日常-selenium登录12306,绕过验证

文章目录 前言代码设计 前言 hello兄弟们,这里是无聊的网友。愉快的周末过去了,欢迎回到学习频道。书接上文,我们说到了再用selenium登录12306时遇到了滑块验证的问题。当前的网站几乎每家都会在登录模块添加一个认证,来规避各种…

js 同步与异步

一、js 执行机制 JavaScript语言的一大特点就是单线程,即(同一时间只能做一件事情)。因为JavaScript是为了处理页面中用户的交互,以及操作DOM而诞生的。比如对某个DOM元素进行添加和删除操作。不能同时进行,应该先进行…

一文带你通俗理解23种软件设计模式(推荐收藏,适合小白学习,附带C++例程完整源码)

作者:翟天保Steven 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处 一、设计模式是什么? 设计模式是为了解决在软件开发过程中遇到的某些问题而形成的思想。同一场景有多种设计模式可以应…

第18章_JDK8-17新特性(上)

第18章_JDK8-17新特性(上) 讲师:尚硅谷-宋红康(江湖人称:康师傅) 官网:http://www.atguigu.com 本章专题与脉络 1. Java版本迭代概述 1.1 发布特点(小步快跑,快速迭代…

安装TortoiseGit后桌面文件夹和用户文件夹中显示红色叹号

✨ TortoiseGit作为一个很好用的git图形化工具,能够很方便的进行版本控制。但在安装这个软件之后就遇到了一个问题。 👀问题描述:我们知道,安装TortoiseGit之后,当自己版本库中文件发生更改,但还没有提交到受控库时&am…

信息系统管理工程师好考吗?如何去备考呢?

信息系统管理工程师有点难度,侧重于IT技术的中级资格考试,主要适合系统管理员等专业技术人员去考 一、信息系统管理工程师考试介绍: 考试科目有两科,且成绩不延续,考试都合格后的证书是永久有效的。 报考条件&#x…

文件改名,如何将文件复制到指定文件夹里,并设置自动编号同名文件

在工作中,我们经常会遇到要将文件进行备份的时候,那么文件名称相同的情况下要如何批量备份呢?又如何自动编号同名文件?今天小编就给大家分享一下我的操作办法。 首先,第一步我们要进入“文件批量改名高手”的主页面并…

pot lib:optimal transport python库

文章目录 transport1. [计算最优传输(Computational Optimal Transport)](https://zhuanlan.zhihu.com/p/94978686)2. 离散测度 (Discrete measures), 蒙日(Monge)问题, Kantorovich Relaxation (松弛的蒙日问题)3. scipy.stats.wasserstein_distance 距…

CVPR2023活体检测Instance-Aware Domain Generalization for Face Anti-Spoofing学习笔记

论文链接:https://arxiv.org/pdf/2304.05640.pdf 代码链接:GitHub - qianyuzqy/IADG: (CVPR 2023) Instance-Aware Domain Generalization for Face Anti-Spoofing(尚未公布) 研究动机 此前的基于域泛化(domain gen…

信号完整性分析:关于传输线的三十个问题解答(二)

11.对于 50 欧姆带状线的纵横比,什么是好的经验法则?(What is a good rule of thumb for the aspect ratio of a 50-Ohm stripline?) 在带状线几何形状和 FR4 基板中,线宽和平面之间的电介质间距的纵横比为 。由于有两个平面,带…

如何快速建立一个podman环境

本文介绍如何安装podman,并创建podman容器 环境 Centos8 安装podman Podman 是一个容器环境,首先在主机上安装 Podman。执行下面命令来安装podman: [rootlocalhost ~]# yum -y install podman然后修改一下用户命名空间的大小&#xff1a…

202305读书笔记|《因思念而沉着》——任何赞美都是身外之物唯自由可随身携带

《因思念而沉着》作者巴哑哑,忘了是什么时候加入的书架,昨天下班地铁上读完的书。是美的! 部分节选如下: 羽叶茑萝举着熄灭的花青色的小枣落了一地所以哭泣沾染上了你的脸 在没落下 当我们开始生活 就是开始患上了眼疾你独自悲伤…

互联网求职指南2023版(内含腾讯、阿里、字节真实面经)

0. 缘起 五年前,小编曾经写过一篇文章,文章主要带大家了解当时的互联网巨头公司、独角兽公司。并给出了互联网的求职建议。从简历、知识储备、暑期实习、面试上给出了中肯的建议。现在看来也是很受用的,感兴趣的可以点击链接《互联网求职指南…

【人工智能概论】 构建神经网络——以用InceptionNet解决MNIST任务为例

【人工智能概论】 构建神经网络——以用InceptionNet解决MNIST任务为例 文章目录 【人工智能概论】 构建神经网络——以用InceptionNet解决MNIST任务为例一. 整体思路1.1 两条原则1.2 四个步骤 二. 举例——用InceptionNet解决MNIST任务2.1 模型简介2.2 MNIST任务2.3 完整的程序…

【ChatGPT 】ChatGPT Sidebar 实战:自定义 ChatGPT 搜索页面回复模板(示例开发和文员专用模板)

目录 一、前言 二、ChatGPT Sidebar 通用配置 (1)通用配置入口 (2)设置 ① 如何访问 ChatGPT ② 语言 ③ 主题 三、ChatGPT Sidebar 搜索页面 (1)搜索页面入口 (2)设置 …