ssh 远程复制文件

SSH is a lifesaver when you need to remotely manage a computer, but did you know you can also upload and download files, too? Using SSH keys, you can skip having to enter passwords and use this for scripts!

当您需要远程管理计算机时,SSH是救生员,但是您知道您也可以上传和下载文件吗? 使用SSH密钥,您可以不必输入密码并将其用于脚本!

This process works on Linux and Mac OS, provided that they’re properly configured for SSH access. If you’re using Windows, you can use Cygwin to get Linux-like functionality, and with a little tweaking, SSH will run as well.

只要正确配置了SSH访问权限,此过程即可在Linux和Mac OS上运行。 如果您使用的是Windows,则可以使用Cygwin获得类似Linux的功能 ,并且稍作调整, SSH也将运行 。

通过SSH复制文件 (Copying Files Over SSH)

Secure copy is a really useful command, and it’s really easy to use. The basic format of the command is as follows:

安全复制是一个非常有用的命令,并且非常易于使用。 该命令的基本格式如下:

scp [options] original_file destination_file

scp [选项] original_file destination_file

The biggest kicker is how to format the remote part. When you address a remote file, you need to do it in the following manner:

最大的问题是如何格式化远程部分。 在寻址远程文件时,您需要按以下方式进行操作:

user@server:path/to/file

用户@服务器:路径/到/文件

The server can be a URL or an IP address. This is followed by a colon, then the path to the file or folder in question. Let’s look at an example.

服务器可以是URL或IP地址。 这之后是一个冒号,然后是该文件或文件夹的路径。 让我们来看一个例子。

scp –P 40050 Desktop/url.txt yatri@192.168.1.50:~/Desktop/url.txt

scp –P 40050桌面/url.txt yatri@192.168.1.50:〜/ Desktop / url.txt

This command features the [-P] flag (note that it’s a capital P). This allows me to specify a port number instead of the default 22. This is necessary for me because of the way I’ve configured my system.

此命令具有[-P]标志(请注意,它是大写字母P)。 这使我可以指定端口号而不是默认的22。这对我来说是必需的,因为我已经配置了系统。

Next, my original file is “url.txt” which is inside of a directory called “Desktop”. The destination file is in “~/Desktop/url.txt” which is the same as “/user/yatri/Desktop/url.txt”. This command is being run by the user “yatri” on the remote computer “192.168.1.50”.

接下来,我的原始文件是“ url.txt”,它位于名为“ Desktop”的目录中。 目标文件位于“〜/ Desktop / url.txt”中,与“ /user/yatri/Desktop/url.txt”相同。 该命令由远程计算机“ 192.168.1.50”上的用户“ yatri”运行。

What If you need to do the opposite? You can copy files from a remote server similarly.

如果您需要做相反的事情怎么办? 您可以类似地从远程服务器复制文件。

Here, I’ve copied a file from the remote computer’s “~/Desktop/” folder to my computer’s “Desktop” folder.

在这里,我已将文件从远程计算机的“〜/ Desktop /”文件夹复制到计算机的“ Desktop”文件夹。

To copy whole directories, you’ll need to use the [-r] flag (note that it’s a lowercase r).

要复制整个目录,您需要使用[-r]标志(请注意,它是小写的r)。

You can also combine flags. Instead of

您还可以组合标志。 代替

scp –P –r …

scp –P –r…

You can just do

你可以做

scp –Pr …

scp –Pr…

The toughest part here is that tab completion doesn’t always work, so it’s helpful to have another terminal with an SSH session running so that you know where to put things.

这里最难的部分是制表符补全并不总是起作用,因此让另一个带有SSH会话的终端运行很有帮助,这样您就可以知道在哪里放置东西。

没有密码的SSH和SCP (SSH and SCP Without Passwords)

Secure copy is great. You can put it in scripts and have it do backups to remote computers. The problem is that you may not always be around to enter the password. And, let’s be honest, it’s a real big pain to put in your password to a remote computer you obviously have access to all the time.

安全复制很棒。 您可以将其放在脚本中,并备份到远程计算机。 问题是您可能不总是要输入密码。 而且,说实话,将密码输入到您显然一直可以访问的远程计算机上确实是一个很大的痛苦。

Well, we can get around using passwords by using key files. We can have the computer generate two key files – one public that belongs on the remote server, and one private which is on your computer and needs to be secure – and these will be used instead of a password. Pretty convenient, right?

好吧,我们可以通过使用密钥文件来避免使用密码。 我们可以让计算机生成两个密钥文件-一个在远程服务器上的公用文件,另一个在计算机上的需要安全的专用文件-这些文件将代替密码使用。 很方便吧?

On your computer, enter the following command:

在您的计算机上,输入以下命令:

ssh-keygen –t rsa

ssh-keygen –t rsa

This will generate the two keys and put them in:

这将生成两个密钥并将其放入:

~/.ssh/

〜/ .ssh /

with the names “id_rsa” for your private key, and “id_rsa.pub” for your public key.

名称分别为“ id_rsa”和“ id_rsa.pub”。

After entering the command, you’ll be asked where to save the key. You can hit Enter to use the above-mentioned defaults.

输入命令后,将询问您将密钥保存在何处。 您可以按Enter键以使用上述默认值。

Next, you’ll be asked to enter a passphrase. Hit Enter to leave this blank, then do it again when it asks for confirmation. The next step is to copy the public key file to your remote computer. You can use scp to do this:

接下来,系统会要求您输入密码。 按Enter键,将其保留为空白,然后在要求确认时再次进行。 下一步是将公钥文件复制到远程计算机。 您可以使用scp执行此操作:

The destination for your public key is on the remote server, in the following file:

公共密钥的目标位于远程服务器上的以下文件中:

~/.ssh/authorized_keys2

〜/ .ssh / authorized_keys2

Subsequent public keys can be appended to this file, much like the ~/.ssh/known_hosts file. This means that if you wanted to add another public key for your account on this server, you would copy the contents of the second id_rsa.pub file into a new line on the existing authorized_keys2 file.

随后的公钥可以附加到此文件,非常类似于〜/ .ssh / known_hosts文件。 这意味着,如果要在此服务器上为您的帐户添加另一个公钥,则可以将第二个id_rsa.pub文件的内容复制到现有authorized_keys2文件的新行中。

安全注意事项 (Security Considerations)

Isn’t this less secure than a password?

这不是比密码安全吗?

In a practical sense, not really. The private key that’s generated is stored on the computer you’re using, and it is never transferred, not even to be verified. This private key ONLY matches with that ONE public key, and the connection needs to be started from the computer that has the private key. RSA is pretty secure and uses a 2048 bit-length by default.

从实际意义上讲,并非如此。 生成的私钥存储在您正在使用的计算机上,并且永远不会转移,甚至不会被验证。 该私钥仅与该一个公钥匹配,并且需要从具有私钥的计算机开始连接。 RSA非常安全,默认情况下使用2048位长度。

It’s actually pretty similar in theory to using your password. If someone has knows your password, your security goes out of the window. If someone has your private key file, then security is lost to any computer that has the matching pubic key, but they need access to your computer to get it.

从理论上讲,它实际上与使用密码相似。 如果有人知道您的密码,那么您的安全性就会消失。 如果某人拥有您的私钥文件,那么具有匹配的公钥的任何计算机的安全性都会丢失,但是他们需要访问您的计算机才能获取它。

Can this be more secure?

可以更安全吗?

You can combine a password with key files. Follow the steps above, but enter a strong passphrase. Now, when you connect over SSH or use SCP, you’ll need the proper private key file as well as the proper passphrase.

您可以将密码与密钥文件结合在一起。 请按照上述步骤操作,但请输入一个强密码。 现在,当您通过SSH连接或使用SCP时,您将需要正确的私钥文件以及正确的密码短语。

Once you enter your passphrase once, you won’t be asked again for it until you close your session. That means that the first time you SSH/SCP, you’ll need to enter your password, but all subsequent actions won’t require it. Once you log out of your computer (not the remote one) or close your terminal window, then you’ll have to enter it again. In this way, you’re not really sacrificing security, but you’re also not harassed for passwords all the time.

一次输入密码后,在关闭会话之前,不会再要求您输入密码。 这意味着您第一次使用SSH / SCP时,需要输入密码,但是随后的所有操作都不需要它。 从计算机(不是远程计算机)注销或关闭终端窗口后,就必须再次输入。 这样,您并没有真正牺牲安全性,但是也没有一直都在为密码而烦恼。

Can I reuse the public/private key pair?

我可以重用公钥/私钥对吗?

This is a really bad idea. If someone finds your password, and you use the same password for all of your accounts, then they now have access to all of those accounts. Similarly, your private key file is also super-secret and important. (For more information, take a look at How To Recover After Your Email Password Is Compromised)

这真是个坏主意。 如果有人找到了您的密码,并且您对所有帐户使用了相同的密码,那么他们现在就可以访问所有这些帐户。 同样,您的私钥文件也非常机密且重要。 (有关更多信息,请查看如何在电子邮件密码遭到破坏后恢复 )

It’s best to create new key pairs for every computer and account you want to link. That way, if one of your private keys get caught somehow, then you’ll only compromise one account on one remote computer.

最好为要链接的每台计算机和帐户创建新的密钥对。 这样,如果您的某个私钥以某种方式被捕获,那么您只会破坏一台远程计算机上的一个帐户。

It’s also really important to note that all of your private keys are stored in the same place: in ~/.ssh/ on your computer, you can use TrueCrypt to create a secure, encrypted container, then create symlinks in your ~/.ssh/ directory. Depending on what I’m doing, I use this super-paranoid super-secure method to put my mind at ease.

还要特别注意,所有私钥都存储在同一位置:在计算机上的〜/ .ssh /中,可以使用TrueCrypt创建安全的加密容器,然后在〜/ .ssh中创建符号链接 。 / 目录。 根据我在做什么,我使用这种超级偏执的超级安全方法来放心。



Have you used SCP in any scripts? Do you use key files instead of passwords? Share your own expertise with other readers in the comments!

您是否在任何脚本中使用过SCP? 您使用密钥文件代替密码吗? 在评论中与其他读者分享您自己的专业知识!

翻译自: https://www.howtogeek.com/66776/how-to-remotely-copy-files-over-ssh-without-entering-your-password/

ssh 远程复制文件

ssh 远程复制文件_如何在不输入密码的情况下通过SSH远程复制文件相关推荐

  1. qt 修改.exe文件图标_如何在没有错误的情况下更改压缩的.EXE文件上的图标

    qt 修改.exe文件图标 We've previously shown you how to modify the icon on an .EXE file, but if you've tried ...

  2. 在服务端没接显示器的情况下,nomachine远程连接客户端看到的是黑屏的问题

    这里遇到一个问题,在服务端没接显示器的情况下,nomachine远程连接客户端看到的是黑屏(我这里服务器是ubuntu18.04) 参考链接:https://blog.csdn.net/c133111 ...

  3. python远程桌面控制_手把手教你如何用Pycharm2020.1.1配置远程连接的详细步骤

    配置说明 使用Pycharm 2020.1.1 professional 专业版.(据说只有专业版可以远程连接)如果不是专业的伙伴,可以用校园邮箱注册一个专业版,免费的哦! 步骤 1. 设置Conne ...

  4. 克隆git文件_如何在Git中克隆,修改,添加和删除文件

    克隆git文件 在本系列有关Git入门的第一篇文章中 ,我们创建了一个简单的Git存储库,并通过将其连接到计算机将文件添加到其中. 在本文中,我们将学习有关Git的其他一些知识,即如何在Git存储库中 ...

  5. 怎么更新opengl.dll文件_微信又更新了:群接龙怎么玩?文件如何备份?怎么发高清大视频?...

    微信又更新了,这些新功能好用吗https://www.zhihu.com/video/1169996029444481024 微信在前两天又更新了,这次带来了几个全新的功能,比如微信电脑端可以直接打开 ...

  6. testdisk 恢复文件_如何在Linux上安装TestDisk并恢复已删除的文件

    testdisk 恢复文件 Ever stuck in a situation where you accidentally deleted a file? In this tutorial, we' ...

  7. bat 删除文件_利用电脑文本文档建立一个简单方便的删除文件的小程序

    删除文不需要的文件或者资料,是日常工作中必定会遇到了. 各种的杀毒软件或者防护软件都具备删除文件的功能,例如360.腾讯电脑管家.这些操作起来其实也不是太麻烦! 不过呢!今天来和大家分享一个更简单的方 ...

  8. java中io流如何创建一个文件_,Java中Io流操作-File类的常用操作-创建文件,创建文件夹...

    package com.hxzy.IOSer; import java.io.File; import java.io.IOException; public class Demo03 { publi ...

  9. mfc中怎么集成文件_怎么把几个pdf合并并打印在一份文件中?

    a为什么我们要把pdf文件合并呢?当然我们要知道什么是pdf文件才能进行更多的操作,Pdf是我们许多人都会用到的一种存储文件的格式,它的内容基本是以图像的形式表现,所以我们在播放和打印的时候可以不用担 ...

最新文章

  1. python使用fpdf创建pdf并写入hello world
  2. 从零实现一个简易jQuery框架之一—jQuery框架概述
  3. java浮点数原理,浮点型数据存储原理
  4. boost::hana::detail::create用法的测试程序
  5. mysql 数据结构设计_MYSQL 设计数据结构需注意问题
  6. Thread.getContextClassLoader与Thread.getClassLoader()区别
  7. 用vs2011 编译 orchard 源代码
  8. 凯兑换系统服务器角色,能够在所有局中通用的角色,游走于各个线路,单挑很强的凯...
  9. 2020计算机考试系统office,2020年3月计算机二级考试,大学生office考试教材,仿真考试系统...
  10. 计算机多媒体教室管理制度,新疆大学多媒体教室管理制度
  11. COMSOL Multiphysics多物理场仿真技术与应用
  12. 有AI就不搬砖?超乎你的想象!道翰天琼认知智能机器人平台API接口大脑为您揭秘。
  13. bim 模型web页面展示_HTML5/WebGL技术BIM模型轻量化Web浏览解决方案
  14. 某zhan sign
  15. java项目——大数据量的处理
  16. 30 岁的码农人生 ——人生至暗时,你依然能窥见光明
  17. python数据挖掘需要学的内容
  18. 程序员的五一是怎么过的?除了狗粮还是狗粮?
  19. 使用tkinter开发GUI程序4 -- tkinter常见控件的特征属性(第二部分)
  20. 计算机二进制表示小数,小数用二进制如何表示

热门文章

  1. VBS 入门交互实战
  2. Visual Studio2022安装及设置教程
  3. TCP 与 HTTP
  4. 树的根节点个数(0或者1)
  5. idea下载插件超时
  6. 谷歌战神Jeff Dean
  7. 产品经理如何写一看就想约的简历?
  8. 1.pytorch学习:安装pytorch
  9. 《Hadoop.The.Definitive.Guide.4th.Edition.2015.3》学习笔记
  10. VPLC系列机器视觉运动控制一体机快速入门(六)