"Key is invalid" message on GitHub

GithubSsh Keys

Github Problem Overview


I have generated SSH keys for a new server installation according to the procedure outlined here. However, when I copy the contents of id_rsa.pub to my keys list on GitHub, I get the error message "Key is invalid. Ensure you've copied the file correctly."

The generated key is valid, and I am copying it exactly as is (all in one line with no whitespace at the beginning/end).

Anybody have an idea what could be going on?

Github Solutions


Solution 1 - Github

I came here because I had the same problem. From your question, I realized that I was copying the contents from the wrong file, without the .pub extension (it was my private key, i.e. the id_rsa file, which should be kept private at all times!)

From a MAC:

vi ~/.ssh/id_rsa.pub

Copy exactly from the end (with be the last letter of your email domain extension) of the block of characters to the beginning, which starts with ssh-rs ...

If it doesn't work, you are surely not copying properly.

It worked.

If you are on Windows, use notepad and copy-paste as described above.

I hope that helped.

Solution 2 - Github

Here are some steps that are not at all clear when copy/pasting your public key. (For the record, I used Putty Key Generator for my keys.)

  • On the first line, enter ssh-rsa.
  • Make sure you add one space after ssh-rsa
  • When you copy/paste the key into the Key textbox it should preserve the \n characters (x0A). If you are using Notepad on Windows, you won't see these new lines until you copy the text to Github.

Notice on this screenshot, there's a space after ssh-rsa (See the cursor.) The public key text also includes three \n characters, so you can see the public key text bumps down to the next line three times in the textbox.![SSH RSA Key] 1

Solution 3 - Github

I know this is question is a cpl years old now, but if someone in 2018 encounter this: https://i.stack.imgur.com/V2XgK.png" width="400"/>

then below is a no-brainer:

my problem was I tried to copy the private key

> id_rsa

instead of public key content

> id_rsa.pub

as my SSH gitlab repository key, so after you:

ssh-keygen -t rsa -C "[email protected]" -b 4096

please remember about the .pub file extension to copy from:

  • OSX:

  pbcopy < ~/.ssh/id_rsa.pub

  • Linux:

  xclip -sel clip < ~/.ssh/id_rsa.pub

  • Windows PowerShell

  cat ~/.ssh/id_rsa.pub | clip


then paste it to your gitlab/github or other repository as your SSH key

this makes perfect sense, because you should always only share your public key, not the private one :)

Solution 4 - Github

I try several ways, but none of them helps(my os is ubuntu). Then I thought if I can print the content of ~/.ssh/id_rsa.pub by using command "more".

I sorry than I have no reputation to post images! when i have, i will post it!

Here is a url where you can see the image:

enter image description here

I copied the content from the command line, and it WORKED!!!

Solution 5 - Github

Someone suggested to use $ vi ~/.ssh/id_rsa.pub however it didn't work for me.

So instead of that , I used $ cat ~/.ssh/id_rsa.pub command on windows machine to print out on the terminal then copy it.

Solution 6 - Github

If you have indeed copied the content of your id_rsa.pub key (in one continuous line), the result should look like (from The GitHub ssh help page):

ssh key

The OP louism confirms a copy-paste issue:

> - For a very obscure reason, copy-pasting from emacs removed the \n after ssh-rsa, and that was causing the problem.

  • Copy-pasting from vi solved the problem.

Solution 7 - Github

Recommended by GitHub

Copying the SSH-KEY :

I worked with xclip to copy the id_rsa.pub key, like so:

1. Install xclip

sudo apt-get install xclip

2. Copy with xclip

xclip -sel clip < ~/.ssh/id_rsa.pub


Good Luck.

Solution 8 - Github

In windows,

Just Copy the naked key data from key file and remove new lines. Write this below things in dialogue box to solve your problem. ssh-rsa Key

NOTE : There is space between ssh-rsa and key

And you are done.

Solution 9 - Github

Just for those looking in the future, another common issue is that word-wrapping. For example, if you run cat ~/.ssh/id_rsa.pub it could wrap lines, thus adding extra \ characters at each line break. Remove these and your key should work fine.

Solution 10 - Github

I was having the same issue, despite copying the public key from cat output directly from the terminal (Cmder/ConEmu). It seems that the method that you use to copy the public key seems to make a difference. Here is a way to check:

$ ssh-keygen -l -f ~/.ssh/id_rsa.pub
4096 SHA256:...

Then paste the key that fails into a new file, and try to verify that:

$ ssh-keygen -l -f test.pub
test.pub is not a public key file.

In my case I had been copying the key from terminal output using cat; instead getting the key from vim worked. Here is way to open the key in vim with word wrap enabled:

$ vim "+set wrap" ~/.ssh/id_rsa.pub

I suspect that the terminal emulator may have something to do with it as I haven't had this issue with Terminal.app.

Solution 11 - Github

It's common issue we face while generating the key. While the SSH generating, we will get two diff files one is id_rsa and id_rsa.pub. here the file with pub extension is the public key holder file. You must copy the content from this file and add to the Github.

NOTES:

  1. public key file content starts with 'SSH-RSA'
  2. end with your Github email id. ex: if you registered Github with @gmail.com then the pub file has the specified email id at the last

Solution 12 - Github

My laptop is on macOS Big Sur version 11.3.1 and the ssh-keygen -t rsa generated a very long key with [email protected] at the beginning. ssh-keygen -t rsa -b 4096 worked; it created a key with ssh-rsa algorithm.

Solution 13 - Github

If you use putty (puttygen), copy this!!!!

enter image description here

Solution 14 - Github

If you using Sublime you can see ssh-key correctly. Check if there is any id_rsa.pub

ls -al ~/.ssh

If it is in place open it with sublime text

sudo subl /home/user_name/.ssh/id_rsa.pub

Then copy and paste your public ssh key.

Solution 15 - Github

In my case, I had to add an extra white rule after my e-mail (after the entire key string) before it got accepted. I found this out by accident, after trying to copy my ssh key as entered on Gitlab.

pbcopy from my pub key straight form the command line didn't work. The copy from gitlab with the extra white rule at the end, did the trick.

Solution 16 - Github

I just had this issue. Super frustrating. I figured my situation might have affected the casue of the problem. I am using a tablet, trying to setup the key on githubs site through my tablet browser (Chrome) and I am copying the key from an ssh app connected to my remote linux server.

What ended up happening is that somehow when I copied the key to the browser, it introduced a number of line breaks. Not only that, but it also removed a character from each point where a new line was inserted.

So to get it to work, I had to manually find each new line break, then hunt for that part of the key to find the missing character. I then added the character and removed the new line. After I hunted down each and every change, the key worked fine.

By the way, I did not add any new line breaks myself, such as the one mentioned by others after the ssh-rsa.

Hopefully this helps others. I have no idea why it happened. Silly that copying to the clipboard would alter the text copied. Oh, I also copied it right out of Vim.

Solution 17 - Github

If you are using bash for windows as your terminal and you are copying via shift + highlighting + ctrl-c, then this solution might work for you:

  1. After copying the key and pasting it into notepad:

example ssh key

  1. Now go to Edit > Select All or Ctrl-A

text highlighted

  1. The large amounts of blue areas are whitespace. You need to delete them with either the backspace or the delete key. Leave the whitespace that in the green circles.

enter image description here

  1. You should now be able to copy and paste this edited version into github.

enter image description here

Solution 18 - Github

if using vs code

code ~/.ssh/id_rsa.pub will open .pub file copy it to github

Solution 19 - Github

To Copy the entire content of id_rsa.pub to clipboard:

Run > pbcopy ~/.ssh/id_rsa.pub

Solution 20 - Github

I was trying to upload my public key and was getting "Invalid Key" message.My format was not proper as there was extra spaces at the end of the key. It should be in format : ssh-rsa <public-key>

Solution 21 - Github

If you are on linux or mac, just do the following:

$ cat ~/.ssh/id_rsa.pub

The output should be in the form like below:

ssh-rsa somerandomcharacters user@domain

Final step is to copy the contents and paste it in your destination

Solution 22 - Github

Copying the ssh public key into clipboard is mainly the trick here and the best way to do it is right from terminal. You must output this key onto the terminal by following command cat ~/.ssh/id_rsa.public and press enter. The ssh public will be displayed on the GitBash terminal screen and then you just need to copy it carefully by selecting from last charachter of your email to the begining of key i.e ssh rsa... I spent days and then came to this solution in end.

Solution 23 - Github

I'm using window OS and for some reasons it's doesn't work for me:

$ clip < ~/.ssh/id_ed25519.pub
bash: clip: command not found

I located the hidden .ssh folder at addess: C:\Users\g1100_000\.ssh You will see the pub file as below:

enter image description here

Open it with your code editor and simply copy the key to github. Hope this can help you out.

Solution 24 - Github

$ cat /c/Users/User/.ssh/id_rsa.pub use this command instad of $ cat /c/Users/User/.ssh/id_rsa

Solution 25 - Github

Just try installing wps2019 and open the .pub on wps2019 and copy the code and paste it :

Solution 26 - Github

Follow the instructions below from scratch and you will be able to sort this issue.

Many Git servers authenticate using SSH public keys. In order to provide a public key, each user in your system must generate one if they don’t already have one. This process is similar across all operating systems. First, you should check to make sure you don’t already have a key. By default, a user’s SSH keys are stored in that user’s ~/.ssh directory. You can easily check to see if you have a key already by going to that directory and listing the contents:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub

You’re looking for a pair of files named something like id_dsa or id_rsa and a matching file with a .pub extension. The .pub file is your public key, and the other file is the corresponding private key. If you don’t have these files (or you don’t even have a .ssh directory), you can create them by running a program called ssh-keygen, which is provided with the SSH package on Linux/macOS systems and comes with Git for Windows:

$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 [email protected]

First it confirms where you want to save the key (.ssh/id_rsa), and then it asks twice for a passphrase, which you can leave empty if you don’t want to type a password when you use the key. However, if you do use a password, make sure to add the -o option; it saves the private key in a format that is more resistant to brute-force password cracking than is the default format. You can also use the ssh-agent tool to prevent having to enter the password each time.

Now, each user that does this has to send their public key to you or whoever is administrating the Git server (assuming you’re using an SSH server setup that requires public keys). All they have to do is copy the contents of the .pub file and email it. The public keys look something like this:

$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== [email protected]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2398029View Question on Stackoverflow
Solution 1 - GithubShailenView Answer on Stackoverflow
Solution 2 - GithubJames LawrukView Answer on Stackoverflow
Solution 3 - GithubPiotr ZView Answer on Stackoverflow
Solution 4 - GithubSparks_FlyView Answer on Stackoverflow
Solution 5 - GithubrollykayView Answer on Stackoverflow
Solution 6 - GithubVonCView Answer on Stackoverflow
Solution 7 - GithubAakashView Answer on Stackoverflow
Solution 8 - Githubuser3497443View Answer on Stackoverflow
Solution 9 - GithubCurtis HomanView Answer on Stackoverflow
Solution 10 - GithubJamesView Answer on Stackoverflow
Solution 11 - GithubGopi PView Answer on Stackoverflow
Solution 12 - GithubKirti VView Answer on Stackoverflow
Solution 13 - GithubkyakyaView Answer on Stackoverflow
Solution 14 - GithubKirill ZhuravlovView Answer on Stackoverflow
Solution 15 - GithubstijndmdView Answer on Stackoverflow
Solution 16 - GithubJordanView Answer on Stackoverflow
Solution 17 - GithubIsaac PakView Answer on Stackoverflow
Solution 18 - GithubMuhammed MoussaView Answer on Stackoverflow
Solution 19 - GithubSandioView Answer on Stackoverflow
Solution 20 - GithubPrakhar GurawaView Answer on Stackoverflow
Solution 21 - GithubDiljit PRView Answer on Stackoverflow
Solution 22 - GithubBilal SaeedView Answer on Stackoverflow
Solution 23 - GithubTuan LuuView Answer on Stackoverflow
Solution 24 - GithubSomnath MoreView Answer on Stackoverflow
Solution 25 - Githubharitanshu93View Answer on Stackoverflow
Solution 26 - GithubWalisson RodriguesView Answer on Stackoverflow