【问题标题】:Setting up sshkey for git bash on windows 10 VM在 Windows 10 VM 上为 git bash 设置 ssh 密钥
【发布时间】:2020-05-19 18:29:52
【问题描述】:

操作系统:带有 Windows 10 虚拟机的 Ubuntu

目标:在 Windows 10 虚拟机上设置 git bash,并使用 ssh 密钥与私有 Bitbucket 一起使用。

概述: 将 SSH 密钥生成到非默认位置以使用 git bash 不起作用。一个尝试的解决方案是创建默认位置并将密钥移动到该位置进行测试。我不太了解默认位置是如何生成的,或者 git bash 如何搜索 sshkey 以与 bitbucket 进行比较。我怀疑网络驱动器/IT 可能会影响问题,但不知道要问的正确问题。步骤和细节如下。是的,ssh 密钥已添加到 bitbucket 帐户。

生成 ssh 密钥时,建议的位置是 /home/username/.ssh... 我不确定这个建议的路径来自哪里。我已经用 git bash/ssh 键设置了几台计算机,这个建议的路径各不相同。 如何设置此建议路径?如何设置 ssh 密钥搜索路径?

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/plane/.ssh/id_rsa): /c/Users/username/.ssh/id_rsa
~ pwd = c/Users/username

/确实存在,但home不存在

$ ls -l /
total 3076
drwxr-xr-x  1 username 1061577       0 Feb  3 15:57 bin/
drwxr-xr-x  1 username 1061577       0 Feb  3 15:57 cmd/
drwxr-xr-x  1 username 1061577       0 Feb  3 15:57 dev/
drwxr-xr-x  1 username 1061577       0 Feb  3 15:57 etc/
-rwxr-xr-x  1 username 1061577  152112 Jan 13 20:31 git-bash.exe*
-rwxr-xr-x  1 username 1061577  151600 Jan 13 20:31 git-cmd.exe*
-rw-r--r--  1 username 1061577   18765 Dec 10 18:25 LICENSE.txt
drwxr-xr-x  1 username 1061577       0 Feb  3 15:56 mingw64/
dr-xr-xr-x 13 username 1061577       0 Feb  3 17:57 proc/
-rw-r--r--  1 username 1061577  160771 Jan 13 21:08 ReleaseNotes.html
drwxr-xr-x  1 username 1061577       0 Feb  3 17:02 tmp/
-rw-r--r--  1 username 1061577 1161914 Feb  3 15:57 unins000.dat
-rwxr-xr-x  1 username 1061577 1299888 Feb  3 15:51 unins000.exe*
-rw-r--r--  1 username 1061577   22795 Feb  3 15:57 unins000.msg
drwxr-xr-x  1 username 1061577       0 Feb  3 15:57 usr/

权限被拒绝

mkdir -p /home/username
mkdir: cannot create directory ‘/home’: Permission denied

Git 克隆:

$ git clone ssh://git@git.example.com/project.git
Cloning into 'project'...
Could not create directory '/home/username/.ssh'.
The authenticity of host 'example' can't be established.
RSA key fingerprint is SHA256.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Failed to add the host to the list of known hosts (/home/username/.ssh/known_hosts).
example: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

编辑:

我也尝试过以下方法。

为了指出 .ssh 的实际位置以及我想要的位置。 /c/Users/plane/.ssh

git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa.pub"

1) 无法创建 '/home/plane/.ssh'

2) 未能将主机添加到已知主机列表(/home/plane/.ssh/known_hosts)

3) 加载密钥“/c/Users/plane/.ssh/id_rsa.pub”:格式无效

git clone ssh://git@git.project
Cloning into 'project'...
Could not create directory '/home/plane/.ssh'.
The authenticity of host can't be established.
RSA key fingerprint is SHA256:.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Failed to add the host to the list of known hosts (/home/plane/.ssh/known_hosts).
Load key "/c/Users/plane/.ssh/id_rsa.pub": invalid format
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

谢谢,

【问题讨论】:

  • 这真的很旧/晚了,但你应该使用私钥进行身份验证(将 pub 密钥添加到你的 Github 用户设置中)。

标签: bitbucket virtual-machine git-bash ssh-keygen


【解决方案1】:

你应该使用:

  • 具有简化路径的常规 CMD(在您的 Windows 10 虚拟机中)

    set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
    set GH=C:\path\to\git
    set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
    
  • 在默认 %USERPROFILE%.ssh 文件夹中生成的密钥

    ssh-keygen -P "" -t rsa -m PEM
    
  • id_rsa.pub内容注册到your BitBucket profile


known_hosts 仍被默认位置 /home/username/.ssh 引用。
如何更改此默认位置?

这是由$HOME/%HOME% 环境变量管理的。
SSH 将在$HOME/.ssh 中寻找known_hosts

【讨论】:

  • 我看不到 PATH 与默认 ssh-keygen 目录的来源之间的联系。 env 显示 HOME 和 USERPROFILE,我找不到对 /home/username 的引用。 env HOME=/c/Users/plane USERPROFILE=C:\Users\plane
  • @plane 您是否在 Windows 10 (VM) 会话中使用 Git?如果是这样,PATH 可以确保任何其他程序都没有副作用。这只是为了测试。
  • 我能够生成一个 sshkey 并将其添加到 bitbucket,我遇到的具体问题是 git/gitbash 认为 ssh 密钥默认在哪里。我尝试使用以下命令修改 ssh 位置:``` git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa.pub" ``` 仍然在默认位置 /home 引用 known_hosts /用户名/.ssh如何更改此默认位置?
  • @plane 你在 Windows 10 (VM) 会话中使用 Git 吗?
  • 我在 Windows 10 VM 上使用 git for windows。主机操作系统 Ubuntu,来宾操作系统 Windows 10。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2016-08-17
  • 2017-07-13
  • 2012-08-15
  • 1970-01-01
  • 2015-03-26
  • 2012-05-05
相关资源
最近更新 更多