如何在 Windows 上运行 ssh-add?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18683092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:51:21  来源:igfitidea点击:

How to run ssh-add on windows?

windowsssh

提问by cqcn1991

I'm following #335 Deploying to a VPS , and near the end of the episode, we need to run ssh-addto give server access to github repo.

我正在关注#335 Deploying to a VPS,在本集结束时,我们需要运行ssh-add以授予服务器访问 github 存储库的权限。

The problem is how do I run it in windows? What need to install?

问题是我如何在 Windows 中运行它?需要安装什么?

I know that to run sshto access the remote server, I can use Putty. But this command needs to run locally, I do know how to use Puttyto do this.

我知道要运行ssh以访问远程服务器,我可以使用Putty. 但是这个命令需要在本地运行,我知道如何使用Putty它。

回答by tamj0rd2

Original answer using git's start-ssh-agent

使用 git 的 start-ssh-agent 的原始答案

Make sure you have Git installed and have git's cmdfolder in your PATH. For example, on my computer the path to git's cmd folder is C:\Program Files\Git\cmd

确保你已经安装了 Git 并且cmd在你的 PATH 中有 git 的文件夹。例如,在我的电脑上,git 的 cmd 文件夹的路径是C:\Program Files\Git\cmd

Make sure your id_rsafile is in the folder c:\users\yourusername\.ssh

确保您的id_rsa文件在文件夹中c:\users\yourusername\.ssh

Restart your command prompt if you haven't already, and then run start-ssh-agent. It will find your id_rsaand prompt you for the passphrase

如果还没有,请重新启动命令提示符,然后运行start-ssh-agent. 它将找到您id_rsa并提示您输入密码

Update 2019 - A better solution if you're using Windows 10:OpenSSH is available as part of Windows 10 which makes using SSH from cmd/powershell much easier in my opinion. It also doesn't rely on having git installed, unlike my previous solution.

2019 年更新 - 如果您使用的是 Windows 10,则是一个更好的解决方案:OpenSSH 作为 Windows 10 的一部分提供,在我看来,这使得从 cmd/powershell 使用 SSH 变得更加容易。与我以前的解决方案不同,它也不依赖于安装 git。

  1. Open Manage optional featuresfrom the start menu and make sure you have Open SSH Clientin the list. If not, you should be able to add it.

  2. Open Servicesfrom the start Menu

  3. Scroll down to OpenSSH Authentication Agent> right click > properties

  4. Change the Startup type from Disabled to any of the other 3 options. I have mine set to Automatic (Delayed Start)

  5. Open cmd and type where sshto confirm that the top listed path is in System32. Mine is installed at C:\Windows\System32\OpenSSH\ssh.exe. If it's not in the list you may need to close and reopen cmd.

  1. Manage optional features从开始菜单打开并确保您Open SSH Client在列表中。如果没有,您应该可以添加它。

  2. Services从开始菜单打开

  3. 向下滚动到OpenSSH Authentication Agent> 右键单击​​ > 属性

  4. 将启动类型从禁用更改为其他 3 个选项中的任何一个。我有我的设置Automatic (Delayed Start)

  5. 打开 cmd 并键入where ssh以确认最上面列出的路径在 System32 中。我的安装在C:\Windows\System32\OpenSSH\ssh.exe. 如果它不在列表中,您可能需要关闭并重新打开 cmd。

Once you've followed these steps, ssh-agent, ssh-add and all other ssh commands should now work from cmd. To start the agent you can simply type ssh-agent.

按照这些步骤操作后,ssh-agent、ssh-add 和所有其他 ssh 命令现在应该可以在 cmd 中运行了。要启动代理,您只需键入ssh-agent.

  1. Optional step/troubleshooting: If you use git, you should set the GIT_SSHenvironment variable to the output of where sshwhich you ran before (e.g C:\Windows\System32\OpenSSH\ssh.exe). This is to stop inconsistencies between the version of ssh you're using (and your keys are added/generated with) and the version that git uses internally. This should prevent issues that are similar to this
  1. 可选步骤/故障排除:如果您使用 git,您应该将GIT_SSH环境变量设置为where ssh您之前运行的输出(例如C:\Windows\System32\OpenSSH\ssh.exe)。这是为了防止您正在使用的 ssh 版本(以及您的密钥是用它添加/生成的)与 git 内部使用的版本之间的不一致。这应该可以防止与此类似的问题

Some nice things about this solution:

关于这个解决方案的一些好处:

  • You won't need to start the ssh-agent every time you restart your computer
  • Identities that you've added (using ssh-add) will get automatically added after restarts. (It works for me, but you might possibly need a config file in your c:\Users\User\.ssh folder)
  • You don't need git!
  • You can register any rsa private key to the agent. The other solution will only pick up a key named id_rsa
  • 每次重新启动计算机时都不需要启动 ssh-agent
  • 您添加的身份(使用 ssh-add)将在重新启动后自动添加。(它对我有用,但你可能需要在你的 c:\Users\User\.ssh 文件夹中有一个配置文件)
  • 你不需要 git!
  • 您可以向代理注册任何 rsa 私钥。另一个解决方案只会拿起一个名为id_rsa

Hope this helps

希望这可以帮助

回答by RaymondChou

One could install Git for Windowsand subsequently run ssh-add:

可以为 Windows安装Git并随后运行ssh-add

Step 3: Add your key to the ssh-agent

第 3 步:将您的密钥添加到 ssh-agent

To configure the ssh-agent program to use your SSH key:

If you have GitHub for Windows installed, you can use it to clone repositories and not deal with SSH keys. It also comes with the Git Bash tool, which is the preferred way of running git commands on Windows.

  1. Ensure ssh-agent is enabled:

    • If you are using Git Bash, turn on ssh-agent:

      # start the ssh-agent in the background
      ssh-agent -s
      # Agent pid 59566
      
    • If you are using another terminal prompt, such as msysgit, turn on ssh-agent:

      # start the ssh-agent in the background
      eval $(ssh-agent -s)
      # Agent pid 59566
      
  2. Add your SSH key to the ssh-agent:

    ssh-add ~/.ssh/id_rsa
    

要配置 ssh-agent 程序以使用您的 SSH 密钥:

如果您安装了 Windows 版 GitHub,您可以使用它来克隆存储库而不是处理 SSH 密钥。它还附带了 Git Bash 工具,这是在 Windows 上运行 git 命令的首选方式。

  1. 确保 ssh-agent 已启用:

    • 如果您使用的是 Git Bash,请打开 ssh-agent:

      # start the ssh-agent in the background
      ssh-agent -s
      # Agent pid 59566
      
    • 如果您正在使用其他终端提示,例如 msysgit,请打开 ssh-agent:

      # start the ssh-agent in the background
      eval $(ssh-agent -s)
      # Agent pid 59566
      
  2. 将您的 SSH 密钥添加到 ssh-agent:

    ssh-add ~/.ssh/id_rsa
    

回答by Brian Ng

If you are trying to setup a key for using git with ssh, there's always an option to add a configuration for the identity file.

如果您尝试设置一个密钥以将 git 与 ssh 一起使用,则始终可以选择为身份文件添加配置。

vi ~/.ssh/config

Host example.com
IdentityFile ~/.ssh/example_key

回答by 030

In order to run ssh-addon Windows one could install git using choco install git. The ssh-addcommand is recognized once C:\Program Files\Git\usr\binhas been added as a PATH variable and the command prompt has been restarted:

为了ssh-add在 Windows上运行,可以使用 .git 安装 git choco install gitssh-add一旦将命令C:\Program Files\Git\usr\bin添加为 PATH 变量并重新启动命令提示符,就会识别该命令:

C:\Users\user\Desktop\repository>ssh-add .ssh/id_rsa
Enter passphrase for .ssh/id_rsa:
Identity added: .ssh/id_rsa (.ssh/id_rsa)

C:\Users\user\Desktop\repository> 

回答by farizmamad

I have been in similar situation before. In Command prompt, you type 'start-ssh-agent' and voila! The ssh-agent will be started. Input the passphrase if it asked you.

我以前也遇到过类似的情况。在命令提示符下,您输入“start-ssh-agent”,瞧!ssh-agent 将启动。如果它询问您,请输入密码。

回答by Arne Olafson

The Git GUI for Windows has a window-based application that allows you to paste in locations for ssh keys and repo url etc:

适用于 Windows 的 Git GUI 有一个基于窗口的应用程序,允许您粘贴 ssh 密钥和 repo url 等的位置:

https://gitforwindows.org/

https://gitforwindows.org/