Why doesn't my SSH key work for connecting to github?

后端 未结 9 1214
-上瘾入骨i
-上瘾入骨i 2021-01-30 02:19

Note: I\'m not a newb, and I\'ve done this a gazillion times, but for some reason today it decided not to work.

I keep getting the Permission denied (publickey).

相关标签:
9条回答
  • 2021-01-30 02:32

    The GitHub ssh setup mentions testing your GitHub connection with:

    $ ssh -T git@github.com
    

    That follow the ssh uri syntax (also illustrated in "this answer").

    But you did:

    ssh github.com
    

    (without any user). In that case, ssh reverts to the SCP syntax, which relies on a ~/.ssh/config file, with a section "github.com", to list:

    • the user
    • the hostname
    • (and optionally the public key location, but by default it will try ~/.ssh/id_rsa.pub)

    To change it to a regular SSH URL, don't edit directly your .git/config file, as shown below.
    Use the command git remote set-url:

    git remote set-url origin git@github.com:username/repo.git
    
    0 讨论(0)
  • 2021-01-30 02:35

    I had a similar problem, github did not use my SSH key. I always had to enter my username and password.

    I've been looking at .git/config, under [remote "origin"] there was:

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

    or

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

    I changed the line into

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

    and then it worked.

    0 讨论(0)
  • 2021-01-30 02:36

    GitHub recently underwent an audit of ALL keys. Go to the key section of your account to re-approve it.

    0 讨论(0)
  • 2021-01-30 02:38

    If it works for other repositories, but not one in particular, then you are probably using the wrong remote url(i.e. https instead of git@github.com)

    1. First, double check that your git ssh connection is working:

      ssh -T git@github.com
      
    2. If it works, check your remote:

      git remote -v
      

      it will display something like this:

      origin  https://github.com/username/repo(fetch)
      origin  https://github.com/username/repo(push)
      
    3. If the remotes indicate https at the beginning, then you need to change this url with:

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

    Go here for more details.

    0 讨论(0)
  • 2021-01-30 02:41

    I had a similar issue when moving from Windows 7 to Windows 10 using Git for Windows.

    I had my SSH keys for a Gitlab of our company running on my old computer and using this ssh command in the Windows cmd or the Git Bash worked fine (with git.my_server.com replaced by the Git server domain) - so my Windows was able to use the key, but Git for Windows was not:

    ssh -T git@git.my_server.com
    

    (which displayed: "Welcome to GitLab, @my_username!). However, when trying to clone, push or pull with git, I got the "Permission denied (publickey)" error message.

    I initially couldn't find the location/environment of the SSH keys that Git uses, so I tried to copy/paste the ssh keys into this environment by using the Git Bash:

    1. Open the Git Bash from the Windows Start Menu (not from a directory). Enter

      pwd

      I later found out that this returns the location of your ssh keys. In my case this returned '/u/', which was a network drive mounted as "U:\" in my Windows account.

    2. Type cd .ssh, then dir. This may list your currently existing id files, like id_rsa and id_rsa.pub. I deleted these files since I didn't need them anymore (you might want to skip this if you successfully used other SSH keys on your installation, e.g. for other Git Servers):

      rm id_rsa

      rm id_rsa.pub

    3. Create a new id_rsa file (if you have an existing id_rsa file, you can also use another name, like id_rsa_gitlab_my_username or something like that. Add .pub to this name for the public key):

      vi id_rsa and press the 'i' on your keyboard to switch to text insertion mode. Now copy the content of your private key file (I had mine in C:\Users\my_windows_username.ssh\id_rsa and used Notepad++ to copy the complete content, Windows notepad works fine as well). Press Escape on your keyboard to exit text insertion mode, then enter ':' and 'x', then Enter to save the file. Repeat this for the public key file.

    4. If you use multiple SSH keys or used another name for the id_rsa file, you should also create a 'config' file or copy the content of your existing configuration file:

      vi config

      (Again, press 'i', insert text, press ':', 'x', then Enter.) My file looks like this (use your server, user and SSH file name):

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

    Now my Git for Windows was able to push, pull and clone without problems again.

    0 讨论(0)
  • 2021-01-30 02:44

    First, try this (changing your email address as needed)

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

    If you're creating using Cygwin, there might be confusion about the home directory, which is what happened for me, as I was referring to (copying to GitHub) an old/incorrect key from a different directory. So in case this happens for you, after creating the key, do:

    $ explorer .
    

    Which will pop up a windows explorer window, showing your full/absolute path. That's when I saw the ~ directory was actually my Cygwin directory

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

    I was then able to copy my private SSH key and paste in GitHub, and cloning, etc.. then worked.

    0 讨论(0)
提交回复
热议问题