Copy SSH key to clipboard

September 20, 2012 130k views
Hello, I created an SSH key so that I can add it to Github. I am trying to copy the contents of /.ssh/id_rsa.pub to the clipboard but I am getting errors in both PuTTY and the browser console. I tried installing xclip but that did not work. Any suggestions? I'm currently running an Ubuntu server 12.04 droplet. This should hopefully be my last SSH question! James
1 comment
  • By this command, in your terminal, you can copy your ssh key to your clipboard:
    pbcopy < ~/.ssh/id_rsa.pub

12 Answers
If you don't have pbcopy, xclip or vim just type:
cat ~/.ssh/id_rsa.pub

and copy terminal output
Easiest way I'm aware of is:

pbcopy < ~/.ssh/id_rsa.pub

Copies it straight to the clipboard. Credit to BitBucket for that one.

cat ~/.ssh/id_rsa.pub | pbcopy

Hi James!

I think the easiest way to copy the SSH keys is to open the public SSH key file (as you did) and manually copy and paste the key. This can be done in PuTTy by selecting your text with the left mouse button. Once it is selected, it should already be copied to your clipboard.

You cannot copy the text from the console viewer.

Please let me know if you continue having issues.

Hi Etel,

I really appreciate the assistance! Thanks for the reply. I am able to copy the SSH key to clipboard using PuTTY but GitHub doesn't like the key. I have a feeling that I'm not copying the entire SSH key. Double clicking the left mouse button will highlight/copy the line but only what you see on your line, not the entire line. This is the problem because even if I stretch my Putty window as wide as it allows, it seems that I'm still not copying the entire SSH key.

Is there a way to copy the entire line in the file, even if my console doesn't display it all?

Hello again,

I figured it out. Instead of nano, I should have used the vi text editor. Doh! Such a simple solution. Thanks again for all of your help!

For anyone else that ran into this issue, follow these instructions:

1. cd ~/.ssh
2. sudo vi id_rsa.pub
3. Click and hold down the left mouse button to highlight the entire SSH key but do not highlight any spaces after your email address at the end of the SSH key.
4. Paste into Github :)

Thank you for the update and the walkthrough! I'm glad you got it working :)
Awesome, worked great, thanks. Was using nano too.
Thank you james, also worked for me after I used vi editor

You can also use xclip (assumes you're running a Debian based distro):

$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Credit goes to github.

If you're running Windows, using Git Bash you can do this:

$ clip < ~/.ssh/id_rsa.pub

install GIT GUI using : sudo apt-get install git-gui
open it using git citool to open git gui
click help > show ssh key > copy to clipboard

Have another answer? Share your knowledge.