如何用 scp 在本地和远程主机之间复制文件

admin4周前笔记29

使用 scp 命令可以将文件从远程操作系统服务器传输到本地操作系统,或从本地操作系统复制到远程操作系统。此命令使用 SSH 服务进行身份验证,并对传输的数据进行加密,所以它即可以使用用户名密码的方式进

image.png
基本格式
scp source target
将本地文件复制到远端服务器上
[student@servera ~]scp log.tar root@serverb:/logbackup
The authenticity of host 'serverb (172.25.250.11)' can't be established.
ECDSA key fingerprint is SHA256:BCd8VCfEpGbUo3zb1De0hd1Q5nOMEzYNpMFu5o7j4Fg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
root@serverb's password: 
log.tar                                                  100%   11MB  20.8MB/s   00:00    
[student@servera ~] ssh root@serverb "ls /logbackup"
root@serverb's password: 
log.tar
  • root@ 是可选项,如果不指定,则使用你当前的本地用户。你所使用的用户必须要对远端主机上的对应目录有相应的权限。

  • 和 ssh 一样,第一次连接的时候,会有一个安全性的提示,选项 yse 就可以了,因为 scp 就是 OpenSSH 的一部分。

从远端就复制文件到本地
[student@servera ~]scp root@serverb:/logbackup/log.tar backup/
root@serverb's password: 
log.tar                                                  100%   11MB  25.5MB/s   00:00    
[student@servera ~] ls backup/
log.tar

如果是目录,我们可以通过 -r 选项进行递归的方式,复制整个目录内容

[student@servera ~]$ scp -r root@serverb:/var/log/ backup/
root@serverb's password: 
lastlog                                                  100%  285KB   5.9MB/s   00:00    
README                                                   100% 1040   260.8KB/s   00:00    
wtmp                                                     100% 6912     2.6MB/s   00:00    
btmp                                                     100%    0     0.0KB/s   00:00    
audit.log                                                100%  208KB   8.0MB/s   00:00    
sssd.log                                                 100%    0     0.0KB/s   00:00    
sssd_implicit_files.log                                  100%  109    32.9KB/s   00:00    
sssd_nss.log                                             100%   94     8.6KB/s   00:00    
rhsm.log                                                 100% 3642     1.0MB/s   00:00    
tuned.log                                                100% 2434   904.9KB/s   00:00    
messages                                                 100% 1459KB  18.1MB/s   00:00    
secure                                                   100% 8298     3.4MB/s   00:00    
maillog                                                  100%    0     0.0KB/s   00:00    
spooler                                                  100%    0     0.0KB/s   00:00    
dnf.log                                                  100%  107KB   3.4MB/s   00:00    
dnf.librepo.log                                          100%  245KB   8.4MB/s   00:00    
dnf.rpm.log                                              100%   21KB   2.3MB/s   00:00    
hawkey.log                                               100%   18KB   3.8MB/s   00:00    
cron                                                     100% 7166   706.4KB/s   00:00    
boot.log                                                 100%    0     0.0KB/s   00:00

我们还可以使用 scp 把一个文件从远端的一台主机,复制到另外一台主机上

[student@workstation ~]scp student@servera:/home/student/log.tar student@serverb:/home/student/
student@serverb's password: 
log.tar                                                  100%  670KB  19.6MB/s   00:00    
Connection to servera closed.
[student@workstation ~] ssh student@serverb "ls -l"
total 672
-rw-rw-r--. 1 student student 686080 Nov  6 20:54 log.tar


相关文章

如何从 SQL Server 为 MySQL 服务器配置链接服务器

如何从 SQL Server 为 MySQL 服务器配置链接服务器

一、概述链接服务器是 SQL Server 可以通过直接 T-SQL 查询与另一个 ODBC 兼容数据库(例如另一个 SQL Server 实例或 Oracle 数据库)对话的一种方法。SQL Ser...

Linux压缩好帮手bzip2

Linux压缩好帮手bzip2

Linux 中有几个文件压缩和解压缩工具,比如gzip、7-zip、Lrzip、PeaZip 等等。本篇教程中,我们将介绍如何在 Linux 中使用 bzip2 工具压缩及解压缩.bz2文件...

TCP加速 一键安装管理脚本 | BBR/BBR2的原版/Plus/魔改/锐速(LotServer)N合1超级一键加速脚本

此脚本是由 cx9208 在千影脚本的基础上二次开发而成,集合BBR、BBR plus、BBR魔改、锐速(LotServer)4种加速模式的4合1脚本。TCP加速 一键安装管理脚本项目地址:https...

Linux下命令行如何KILL掉一个进程

Linux下命令行如何KILL掉一个进程

Linux 的命令行里面有用来停止正在运行的进程的所有所需工具想像一下:你打开了一个程序(可能来自于你的桌面菜单或者命令行),然后开始使用这个程序,没想到程序会锁死、停止运行、或者意外死机。...

看完这篇文章,还不懂nginx,算我输

看完这篇文章,还不懂nginx,算我输

1. Nginx产生~Nginx 同 Apache 一样都是一种 Web 服务器。基于 REST 架构风格,以统一资源描述符(Uniform Resources Identifier)URI 或者统一...

XML DOM 节点信息概述

XML DOM 节点信息概述

在 XML DOM 中,每个节点都是一个对象。对象拥有方法和属性,并可通过 JavaScript 进行访问和操作。三个重要的节点属性是:nodeNamenodeValuenodeTypenodeNam...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。