"Key is invalid" message on GitHub
Solution 1
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
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.
Solution 3
I know this is question is a cpl years old now, but if someone in 2018 encounter this:
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
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:
I copied the content from the command line, and it WORKED!!!
Solution 5
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.

user2398029
Updated on February 04, 2022Comments
-
user2398029 8 months
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?
-
David Cain over 10 yearsJust to be sure, you did include the
ssh-rsa
that comes before the key itself, yes? -
Dio Phung over 1 yearAt least in CentOS 7.x , if you generate your SSH key using just
ssh-keygen
then the format won't work. I got this messageKey is not in OpenSSH format
. I fixed it usingssh-keygen -t rsa -b 4096
, then copy this key to Githubcat ~/.ssh/id_rsa.pub
-
-
user2398029 over 10 yearsFor 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. Since you gave me the idea of trying that, I'll award the answer to you!
-
Furgas about 9 yearsMy id_rsa.pub file was missing the "ssh-rsa" part. Adding it manually on Github did the trick.
-
DontVoteMeDown almost 9 yearsPost the image url, some user may add it to the body for you.
-
Hamad almost 9 yearsupload images on some site and share links.to solve the question
-
Sparks_Fly almost 9 years@DontVoteMeDown I have shared the url. Just copy the content and it will work!
-
Sparks_Fly almost 9 years@Hamad I have shared the url.
-
jdknight almost 8 yearsOn a related note, in GitLab (tested 7.4.3), SSH keys can only be added when (as suggested above) adding
ssh-rsa
and by not including the new-line characters. -
jpaugh about 7 yearsThis must be a putty specific issue; I had the same error with putty
-
jpaugh about 7 years@JamesLawruk BTW, GitHub (and, I believe, any ssh-using program) ignores all newline chars in the key file. (That's the reason you're allowed to split it into 3 lines --- at rather arbitrary points.)
-
Jeremiah about 7 yearsvi didn't work for me. I had to use the cat command (OSX) to print it to the terminal and copy its output. Thanks for putting me on the right path.
-
BradT almost 7 yearscopying out of vim worked for me, but copying the result of cat filename didn't
-
Samaursa over 5 yearsThanks, this did indeed work... everybody has their own theories. Things should not be this 'magical' when it comes to something like SSH keys. What a train wreck adding SSH keys is on github.
-
John Cramerus about 5 yearsFor whatever reason, my vim was failing to wrap the key. Copied out of sublime to get the whole key. Worked.
-
Ales over 4 yearsadding "ssh-rsa " with one space after that helped me. thanks a lot.
-
Alexandru Trandafir Catalin almost 3 yearsIt worked for me: Putty SSH from Windows to Linux, open key file with vi, then copy from putty by selecting on screen and pasting it. Weird cat didn't work.
-
Jacky over 2 years
vi
did the trick, I used nano, which the file is being truncated with$
symbol -
Johannes over 2 yearsSame here, using
nano
failed and led to the above-mentioned error; but usingvi
simply worked. -
Pysis over 2 yearsUsed puttygen which gave me the full line, not some pub file I had lying around.
-
Mmm about 2 yearsIf you have a key created by puttygen, you can use puttygen to load the key and then copy the public key text from there. Otherwise you'll have to manipulate the contents of the file to get it in the format above.
-
zyy about 2 yearsIn my case, there are some blanks preceding the line change, and that made the copy and pasting not work.
-
Dio Phung over 1 year
ssh-keygen -t rsa -b 4096
helped, obviously you can leave out-C [email protected]
-
Admin over 1 yeari love you. I have been trying to get this for hours and this answer was the only one that helped me :)
-
Bilal Saeed over 1 yearJeremiah saved me! I had spent days to resolve this issue but couldn't. Copying the SSH key in clipboard is the real trick and the best way to do it is copying it right from terminal and not notepad or anyother tool etc.
-
Vittore Marcas 9 months@Jeremiah You said, that works for me on mac. cat ~/.ssh/id_rsa.pub Thanks!
-
Luca Kiebel 8 monthsWalisson Rodrigues, a link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.