为什么我的SSH密钥不能用于连接到github?

时间:2012-04-01 00:25:37

标签: ssh github ssh-keys

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

当尝试通过SSH连接到github或尝试克隆repo时,我不断收到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密码进行验证)。
  9. 现在,当我尝试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).
    

10 个答案:

答案 0 :(得分:65)

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

我一直在看.git / config,在[remote&#34; origin&#34;]下面有:

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

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

我将行改为

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

然后它奏效了。

答案 1 :(得分:52)

GitHub ssh setup提到用以下方法测试您的GitHub连接:

$ ssh -T git@github.com

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

但你做到了:

ssh github.com

(没有任何用户)。在这种情况下,ssh将恢复为SCP syntax,该command git remote set-url依赖于~/.ssh/config文件,其中包含“github.com”部分,列出:

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

要将其更改为常规SSH URL,请不要直接编辑.git / config文件,如下所示 使用{{3}}:

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

答案 2 :(得分:25)

创建配置文件(~/.ssh/config)后,它有效。这就是我必须加入的内容:

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

感谢@VonC带我到评论中那里。

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

答案 3 :(得分: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了解更多详细信息。

答案 4 :(得分:1)

GitHub最近接受了对所有密钥的审核。转到the key section of your account重新批准。

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

从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 @之前退格,并确保那里只有一个空格并且它可以工作。

答案 7 :(得分:0)

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

我有我公司的Gitlab的SSH密钥,该SSH密钥在旧计算机上运行,​​并且在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
    

现在,我的Windows版Git能够再次顺利地进行推,拉和克隆操作。

答案 8 :(得分:0)

首先,尝试此操作(根据需要更改您的电子邮件地址)

 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中,然后进行克隆等。

答案 9 :(得分:0)

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