【问题标题】:Why doesn't my SSH key work for connecting to github?为什么我的 SSH 密钥无法连接到 github?
【发布时间】:2012-04-15 04:43:03
【问题描述】:

注意:我不是新手,我已经这样做了无数次,但由于某种原因,今天它决定不工作。

我在尝试通过 SSH 连接到 github 或尝试克隆存储库时不断收到 Permission denied (publickey). 错误消息,即使在重新制作 ssh 密钥并将其添加到我帐户中的“SSH 密钥”之后也是如此。

这就是我今天尝试做了十次但没有成功:

  1. 使用 ssh-keygen 创建密钥。
  2. 使用 Gedit 或 Notepad++ 打开 ~/.ssh/id_rsa.pub 并复制内容。
  3. 转到 github.com 上的帐户设置
  4. 转到 SSH 密钥
  5. 单击“添加密钥”按钮。
  6. 给键一个标题
  7. 将钥匙粘贴到钥匙盒中。
  8. 保存密钥(输入我的github密码进行验证)。

而现在,当我尝试做ssh github.com 时,它就是行不通.... 到底是什么?是我现在太累了还是错过了什么?

这是ssh -vvv github.com的输出

OpenSSH_5.9p1, OpenSSL 1.0.0f 4 Jan 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Connecting to github.com [207.97.227.239] port 22.
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug3: Could not load "/home/trusktr/.ssh/id_rsa" as a RSA1 public key
debug1: identity file /home/trusktr/.ssh/id_rsa type 1
debug1: identity file /home/trusktr/.ssh/id_rsa-cert type -1
debug1: identity file /home/trusktr/.ssh/id_dsa type -1
debug1: identity file /home/trusktr/.ssh/id_dsa-cert type -1
debug1: identity file /home/trusktr/.ssh/id_ecdsa type -1
debug1: identity file /home/trusktr/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5github2
debug1: match: OpenSSH_5.1p1 Debian-5github2 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "github.com" from file "/home/trusktr/.ssh/known_hosts"
debug3: load_hostkeys: found key type RSA in file /home/trusktr/.ssh/known_hosts:16
debug3: load_hostkeys: loaded 1 keys
debug3: order_hostkeyalgs: prefer hostkeyalgs: ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-dss-cert-v00@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: mac_setup: found hmac-md5
debug1: kex: server->client aes128-ctr hmac-md5 none
debug2: mac_setup: found hmac-md5
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug2: dh_gen_key: priv key bits set: 122/256
debug2: bits set: 510/1024
d    ebug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug3: load_hostkeys: loading entries for host "github.com" from file "/home/trusktr/.ssh/known_hosts"
debug3: load_hostkeys: found key type RSA in file /home/trusktr/.ssh/known_hosts:16
debug3: load_hostkeys: loaded 1 keys
debug3: load_hostkeys: loading entries for host "207.97.227.239" from file "/home/trusktr/.ssh/known_hosts"
debug3: load_hostkeys: found key type RSA in file /home/trusktr/.ssh/known_hosts:16
debug3: load_hostkeys: loaded 1 keys
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/trusktr/.ssh/known_hosts:16
debug2: bits set: 497/1024
debug1: ssh_rsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/trusktr/.ssh/id_rsa (0x14cce60)
debug2: key: trusktr@rocketship (0x14ce2b0)
debug2: key: /home/trusktr/.ssh/id_dsa ((nil))
debug2: key: /home/trusktr/.ssh/id_ecdsa ((nil))
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/trusktr/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: trusktr@rocketship
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/trusktr/.ssh/id_dsa
debug3: no such identity: /home/trusktr/.ssh/id_dsa
debug1: Trying private key: /home/trusktr/.ssh/id_ecdsa
debug3: no such identity: /home/trusktr/.ssh/id_ecdsa
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

【问题讨论】:

  • 如果您向下滚动该路线页面,它会告诉您使用ssh-add 添加密钥。你这样做了吗?
  • 您是尝试 ssh 进入 github,还是使用 ssh 身份验证推送/拉取到 repo?
  • 如果您使用的是 Windows,您是否定义了 %HOME%?无论如何,ssh -vvv github.com 会有所帮助:stackoverflow.com/questions/6018551/…stackoverflow.com/questions/922210/…
  • @AdamMihalcin 是的,我试过了,但没有区别。
  • @YenTheFirst 是的,我以前做过很多次都没有问题。

标签: ssh github ssh-keys


【解决方案1】:

GitHub ssh setup 提到测试您的 GitHub 连接:

$ ssh -T git@github.com

即遵循ssh uri syntax(也说明在“this answer”中)。

但你做到了:

ssh github.com

(没有任何用户)。在这种情况下,ssh 会恢复到 SCP syntax,它依赖于带有“github.com”部分的 ~/.ssh/config 文件来列出:

  • 用户
  • 主机名
  • (以及可选的公钥位置,但默认情况下会尝试~/.ssh/id_rsa.pub

要将其更改为常规 SSH URL,请不要直接编辑 .git/config 文件,如下所示。
使用command git remote set-url

git remote set-url origin git@github.com:username/repo.git

【讨论】:

  • 我认为你的意思是 git@guthub.com:port/repo.git。您已经在 @ 符号之前提供了用户名,特别是对于那些访问他们自己的 git 服务器的用户,这些服务器可能使用自定义端口号进行 SSH(例如,Atlassian 的 BitBucket 这样做),冒号通常是端口号所在的位置。还是我在你的语法中遗漏了什么?
  • @SeligkeitIstInGott 使用远程端口意味着使用 URI (en.wikipedia.org/wiki/Uniform_Resource_Identifier) 而不是 SCP 语法(早于 OpenSSH URI -- ssh.com/ssh/#history-of-the-ssh-protocol):ssh://git@github.com:port/repo.git。如果不使用ssh://方案,则为SCP语法,其中单独指定端口。
  • 我遇到了一个问题,我可以在 sourcetree 中“连接”到我的 github 帐户,但是如果我尝试克隆存储库,它会要求输入密码并且不接受任何内容。运行 ssh -T git@github.com 之后,我立即能够在 SourceTree 中成功地从 Github 克隆一个 repo。我敢打赌,Atlassian(sourcetree)不想让连接到 Github 变得容易,因为他们希望你使用 Bitbucket。哦,好吧,它现在可以工作了!
【解决方案2】:

我遇到了类似的问题,github 没有使用我的 SSH 密钥。我总是必须输入我的用户名和密码。

我一直在查看 .git/config,在 [remote "origin"] 下有:

    url = http://github.com/path/to/repository

    url = https://github.com/path/to/repository

我把线改成

    url = ssh://git@github.com/path/to/repository

然后它起作用了。

【讨论】:

  • 好收获。在不编辑配置文件的情况下执行此操作的 git 方式如下: 1. git remote,它将列出您的所有远程存储库。就我而言,它是origin 2. git remote remove &lt;remote name that you found&gt; 3. git remote add origin git@github.com:username/repository-name.git
  • 除了@nomad 的建议外,你还需要在下次推送时(或者之前)使用--set-upstream origin master
  • 在 Windows 10 上为我工作
  • 这对我有用,但破坏了我创建的另一个功能。我创建了 git-open 命令来在浏览器中打开 repo URL,并获取它从远程源读取的 URL。我对这一切还很陌生,但有没有办法让我两全其美?
  • 谢谢!我到处找这个。
【解决方案3】:

创建配置文件 (~/.ssh/config) 后,它工作了。这是我必须放入的内容:

Host github.com
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/id_rsa
TCPKeepAlive yes
IdentitiesOnly yes

感谢@VonC 带我参加 cmets。

我不明白为什么我以前从不需要这个。

【讨论】:

  • 如果您的 ssh 地址以某种方式遵循 SCP 语法,您将需要它。有关详细信息,请参阅我的答案。
  • 这救了我!!!经过几个小时的尝试。谢谢你,伙计!
  • GitHub 便携版自动更新,我不再能够使用 git。我不得不再次将 git 放在环境路径中并添加这个文件,然后一切都恢复正常了。谢谢!
  • 这对我有帮助。我有一个旧版本的 git (1.9.1),它似乎不尊重 sshCommand。但我可以使用这种技术来指定我想使用的IdentityFile
  • 很棒的小贴士,挽救了我几个小时的生命!
【解决方案4】:

如果它适用于其他存储库,但不是一个特定的,那么您可能正在使用错误的远程网址(即https 而不是git@github.com

  1. 首先,仔细检查您的 git ssh 连接是否正常:

    ssh -T git@github.com
    
  2. 如果可行,请检查您的遥控器:

    git remote -v
    

    它将显示如下内容:

    origin  https://github.com/username/repo(fetch)
    origin  https://github.com/username/repo(push)
    
  3. 如果遥控器开头是https,那么你需要把这个url改成:

    git remote set-url origin git@github.com:usertname/repo.git
    

here了解更多详情。

【讨论】:

  • 我还要补充一点,当您使用 https 版本而不是 ssh 版本的克隆选项克隆存储库时,可能会发生这种情况。在这种情况下,您需要更改存储库链接的设置以指向本解决方案中描述的 SSH 版本。
  • 当心.git!
  • 我只是在您的描述中遗漏了第 3 步,谢谢!
【解决方案5】:

这对我有用:

git remote set-url origin git@github.com:<YOUR-USERNAME>/<YOUR-REPOSITORY>.git

更改中的项目并运行命令,然后尝试git push。

Github.com 在managing remote repositories 上的页面帮助了我。

【讨论】:

    【解决方案6】:

    GitHub 最近对所有密钥进行了审核。去the key section of your account重新批准。

    【讨论】:

    • 我已经批准/删除了所有的 SSH 密钥。我现在正在添加新密钥(再次查看我的问题),但它不起作用......我之前已经做过很多次了,没有问题。
    【解决方案7】:

    对于 ssh,您需要更改 url。打开 git 配置文件并将 url 更改为

    url = git@github.com:username/repository.git
    

    【讨论】:

      【解决方案8】:

      从 github 复制克隆地址时,我在 Windows 中遇到了一些奇怪的行尾问题。

      注意 git 之前的多余字符。它没有出现在我的控制台中。它是看不见的。

      git clone git@github.com:user/repo.git ./docker
      Cloning into './docker'...
      Warning: Permanently added the RSA host key for IP address '140.82.XXX.X' to the list of known hosts.
      \302\226git@github.com: Permission denied (publickey).
      fatal: Could not read from remote repository.
      

      我花了一段时间才注意到响应中 git 之前的 \302\226。看到这一点后,我在 git@ 之前退格并确保那里只有一个空格并且它有效。

      【讨论】:

      • 似乎ssh-agent 已停止。您可能需要通过在 git bash 中运行命令 `eval "$(ssh-agent -s)"` 来启动 ssh-agent。然后,您可以通过运行命令 ssh-add ~/.ssh/* 再次将所有 ssh 密钥添加到 ssh-add
      【解决方案9】:

      在使用 Git for Windows 从 Windows 7 迁移到 Windows 10 时,我遇到了类似的问题。

      我的 SSH 密钥用于我们公司的 Gitlab 在我的旧计算机上运行,​​并在 Windows cmd 或 Git Bash 中使用此 ssh 命令运行良好(将 git.my_server.com 替换为 Git 服务器域) - 所以我的 Windows 能够使用密钥,但 Windows 版 Git 不能:

      ssh -T git@git.my_server.com
      

      (显示:“欢迎来到 GitLab,@my_username!)。但是,当尝试使用 git 进行克隆、推送或拉取时,我收到了“权限被拒绝(公钥)”错误消息。

      我最初找不到 Git 使用的 SSH 密钥的位置/环境,因此我尝试使用 Git Bash 将 ssh 密钥复制/粘贴到此环境中:

      1. 从 Windows 开始菜单(不是从目录)打开 Git Bash。输入

        pwd

        我后来发现这会返回您的 ssh 密钥的位置。在我的情况下,这返回了“/u/”,它是在我的 Windows 帐户中安装为“U:\”的网络驱动器。

      2. 输入 cd .ssh,然后输入 dir。这可能会列出您当前存在的 id 文件,例如 id_rsa 和 id_rsa.pub。我删除了这些文件,因为我不再需要它们(如果您在安装中成功使用了其他 SSH 密钥,例如其他 Git 服务器,您可能想跳过此步骤):

        rm id_rsa

        rm id_rsa.pub

      3. 创建一个新的 id_rsa 文件(如果您有一个现有的 id_rsa 文件,您也可以使用其他名称,例如 id_rsa_gitlab_my_username 或类似名称。将 .pub 添加到此名称以作为公钥):

        vi id_rsa 并按键盘上的“i”切换到文本插入模式。现在复制您的私钥文件的内容(我在 C:\Users\my_windows_username.ssh\id_rsa 中有我的,并使用 Notepad++ 复制了完整的内容,Windows 记事本也可以正常工作)。 按键盘上的 Escape 退出文本插入模式,然后输入“:”和“x”,然后按 Enter 保存文件。对公钥文件重复此操作。

      4. 如果您使用多个 SSH 密钥或为 id_rsa 文件使用其他名称,您还应该创建一个“配置”文件或复制现有配置文件的内容:

        vi config

        (再次按“i”,插入文本,按“:”,“x”,然后按 Enter。) 我的文件如下所示(使用您的服务器、用户和 SSH 文件名):

        #SCC Gitlab
            Host git.my_server.com
            HostName git.my_server.com
            User git
            IdentityFile ~/.ssh/id_rsa
        

      现在我的 Git for Windows 能够再次毫无问题地推送、拉取和克隆。

      【讨论】:

        【解决方案10】:

        首先,试试这个(根据需要更改您的电子邮件地址)

         ssh-keygen -t rsa -b 4096 -C "you@yourdomain.com"
        

        如果您使用 Cygwin 创建,可能会对主目录感到困惑,这就是发生在我身上的事情,因为我指的是(复制到 GitHub)来自不同目录的旧/不正确的密钥。 因此,如果您发生这种情况,请在创建密钥后执行以下操作:

        $ explorer .
        

        这将弹出一个 Windows 资源管理器窗口,显示您的完整/绝对路径。这时候我才看到~目录其实是我的Cygwin目录

        C:\Program Files\cygwin64\home\{your_username}\.ssh
        

        然后我可以复制我的私人 SSH 密钥并粘贴到 GitHub 中,然后进行克隆等操作。

        【讨论】:

          【解决方案11】:

          我在 Gnome 上遇到了类似的问题。配置没问题,但没有将密钥添加到 ssh-agent。重新启动就可以了。 Gnome Keyring 使用它自己的 ssh-agent。

          【讨论】:

            【解决方案12】:

            就我而言,这是因为我有 2 个用户,他们都有一个 SSH 密钥。第一个经过身份验证的不是我需要的。所以我从中删除了 SSH 密钥。

            此命令帮助我了解正在使用哪个用户,因为它的输出:

            ssh -T git@github.com
            

            【讨论】:

              【解决方案13】:

              另一个可能的原因是权限。

              这个周末我的新笔记本电脑也遇到了同样的问题,即使我的 ssh 密钥已经在 Github 中多年了,我也无法推送到 github。

              ~/.ssh/ 目录的权限应为 700id_rsaid_rsa.pub 应为 600

              当我将密钥复制到新笔记本电脑时,我的权限过于宽松。

              更多信息:https://help.ubuntu.com/community/SSH/OpenSSH/Keys

              【讨论】:

                猜你喜欢
                • 2019-02-09
                • 2022-01-14
                • 2016-11-10
                • 1970-01-01
                • 2021-12-08
                • 1970-01-01
                • 2019-02-07
                • 2018-12-15
                • 1970-01-01
                相关资源
                最近更新 更多