Git 使用 SSH 并指定凭据时,推送失败并显示 public 密钥

Git push fails with public key when using SSH and specifying credentials

我这里有一个奇怪的问题。我们的 Maven 发布插件失败,因为它无法将标签推送到 Git。以下失败:

git push ssh://PU0S:xL8q@git-eim.fg.com/u0r0-SS/workspace-proxy.git workspace-proxy-server-1.10.1
[ERROR] Permission denied (publickey).
[ERROR] fatal: Could not read from remote repository.
[ERROR] 
[ERROR] Please make sure you have the correct access rights
[ERROR] and the repository exists.

如果我远程进入机器并尝试使用表单的 URL 进行推送,我会得到同样的错误:

git push ssh://PU0S:xL8q@git-eim.fg.com/u0r0-SS/workspace-proxy.git

如果我只是使用定义的遥控器推送,它会成功:

git push origin master

以上内容让我确定 .ssh 键在机器上可用。为什么第一个表格失败?

git 在您的 .ssh 文件夹下的特定位置查找 ssh。

在配置文件中设置密钥,您应该可以克隆它。

打开一个终端window。 编辑 ~/.ssh/config 文件。
如果您没有配置文件,请创建一个。 为每个身份组合添加一个别名,例如:

Host workid
HostName github.com 
IdentityFile ~/.ssh/workid

Any idea why the following command fails git clone ssh://git-eim.fg.com/u0r0-SS/workspace-proxy.git but the following succeeds ssh://git@git-eim.fg.com/u0r0-SS/workspace-proxy.git?
What is special about git@?

git@ 表示将接收推送的用户将是 git。然后,身份验证由用于 ssh 的 public 密钥管理。
这不同于 PU0S:xL8q,后者是 username/password,只有在使用 https url.
时才需要 对于 ssh url,您永远不会推送 "as you" (PU0S),而是作为在服务器端管理 git 存储库的帐户。
这就是为什么,例如,你总是按 git@github.com.

如果 git push origin master 成功,则表示与名为“origin”的远程关联的 url 格式正确。
通常 ssh://git@git-eim.fg.com/u0r0-SS/workspace-proxy.git.

检查 git remote -v,或者 since git 2.7git remote get-url origin

以下线索有助于诊断问题。

首先,让我们试试不指定用户的 ssh -vT。请注意,会话假定用户是登录的帐户,具有特殊字符 ('SE121947+PVHF0ONE_SS')!

 $ ssh -vT git-eim.fg.com
OpenSSH_7.1p2, OpenSSL 1.0.2g  1 Mar 2016
....
debug1: Authenticating to git-eim.fg.com:22 as'SE121947+PVHF0ONE_SS'
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/PVHF0ONE_SS/.ssh/id_rsa
debug1: No more authentication methods to try.

Permission denied (publickey).

现在让我们尝试指定 git@git-eim.fg.com

$ ssh -vT git@git-eim.fg.com
OpenSSH_7.1p2, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to git-eim.fg.com [10.238.35.34] port 22.
debug1: Connection established.
debug1: identity file /c/Users/PVHF0ONE_SS/.ssh/id_rsa type 1
...
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
Authenticated to git-eim.fg.com ([10.238.35.34]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi PU0R0SRVDEVOPS! You've successfully authenticated, but GitHub does not provide shell access.

奇怪的是 git@ 可以替换为 foo@ 并且身份验证有效!它几乎就像它只是一个占位符。所以修复是确保 pom.xml 中的 developerConnection 有一个 git@ 以便 git 能够通过身份验证。