901

I've just generated my RSA key pair, and I wanted to add that key to GitHub.

I tried cd id_rsa.pub and id_rsa.pub, but no luck. How can I access my SSH public key?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sscirrus
  • 55,407
  • 41
  • 135
  • 228
  • possible duplicate of [How do I find my RSA key fingerprint?](http://stackoverflow.com/questions/9607295/how-do-i-find-my-rsa-key-fingerprint) – kenorb Mar 26 '15 at 21:37
  • 4
    @kenorb You mean that Q is a duplicate of this? This question was asked two years earlier. :) – sscirrus Apr 03 '15 at 16:45
  • Yes, the other seems to be better positioned (based on the wording, etc.), it has more views and votes within shorter period of time which indicates it's much more popular. See: [Should I vote to close a duplicate question, even though it's much newer, and has more up to date answers?](http://meta.stackexchange.com/q/147643/191655) Once duplicate, both answers could be merged into one. – kenorb Apr 03 '15 at 17:14
  • 8
    `pbcopy < ~/.ssh/id_rsa.pub` worked for me! Check this [GitHub article](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/) – Connor Leech Jun 18 '18 at 17:23

24 Answers24

1513

cat ~/.ssh/id_rsa.pub or cat ~/.ssh/id_dsa.pub

You can list all the public keys you have by doing:

$ ls ~/.ssh/*.pub

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
288

Copy the key to your clipboard.

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Warning: it's important to copy the key exactly without adding newlines or whitespace. Thankfully the pbcopy command makes it easy to perform this setup perfectly.

and paste it wherever you need.

More details on the process, check: Generating SSH Keys.

DD_
  • 7,230
  • 11
  • 38
  • 59
65

You may try to run the following command to show your RSA fingerprint:

ssh-agent sh -c 'ssh-add; ssh-add -l'

or public key:

ssh-agent sh -c 'ssh-add; ssh-add -L'

If you've the message: 'The agent has no identities.', then you've to generate your RSA key by ssh-keygen first.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 3
    This is good for when you don't know the path of the key beforehand. It's not always in `~/.ssh/`. – rudolfbyker Jun 25 '19 at 06:37
  • Tried it out, works on Mac, Win10 (in git bash) and Ubuntu. – Filip Haglund May 12 '20 at 19:22
  • 2
    Using `ssh-add -L` is by far the better option as not every SSH key is an RSA key sitting in the `~/.ssh` folder. I much prefer to use my PGP key for authentication and so I do not have a `~/.ssh/id_rsa.pub` file at all. – steinybot May 23 '20 at 02:40
39

If you're on Windows use the following, select all, and copy from a Notepad window:

notepad ~/.ssh/id_rsa.pub  

If you're on OS X, use:

pbcopy < ~/.ssh/id_rsa.pub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nsuinteger
  • 1,503
  • 12
  • 21
30

Mac, Ubuntu, Linux compatible machines, use this command to print public key, then copy it:

$ cat ~/.ssh/id_rsa.pub
Tellisense
  • 1,858
  • 12
  • 11
21

Here's how I found mine on OS X:

  1. Open a terminal
  2. (You are in the home directory) cd .ssh (a hidden directory)
  3. pbcopy < id_rsa.pub (this copies it to the clipboard)

If that doesn't work, do an ls and see what files are in there with a .pub extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 765
  • 8
  • 12
16

On terminal cat ~/.ssh/id_rsa.pub

explanation

  1. cat is a standard Unix utility that reads files and prints output
  2. ~ Is your Home User path
  3. /.ssh - your hidden directory contains all your ssh certificates
  4. id_rsa.pub OR id_dsa.pub are RSA public keys, (the private key located on the client machine). the primary key for example can be used to enable cloning project from remote repository securely to your client end point.
avivamg
  • 12,197
  • 3
  • 67
  • 61
13

After you generate your SSH key you can do:

cat .ssh/id_rsa.pub |pbcopy

which will copy your ssh key into your clipboard.

Brett
  • 558
  • 4
  • 11
12

If you're using windows, the command is:

type %userprofile%\.ssh\id_rsa.pub

it should print the key (if you have one). You should copy the entire result. If none is present, then do:

ssh-keygen -t rsa -C "your.email@example.com" -b 4096
Jghayes525
  • 123
  • 1
  • 5
11

If you are using Windows PowerShell, the easiest way is to:

cat ~/.ssh/id_<key-type-here>.pub | clip

That will copy the key to your clipboard for easy pasting.

So, in my instance, I use ed25519 since RSA is now fairly hackable:

cat ~/.ssh/id_ed25519.pub | clip

Because I find myself doing this a lot, I created a function and set a simple alias I could remember in my PowerShell profile (learn more about PowerShell profiles here. Just add this to your Microsoft.PowerShell_profile.ps1:

function Copy-SSHKey {
    Get-Content ~/.ssh/id_ed25519.pub | clip
}

Set_Alias -Name sshkey -Value Copy-SSHKey

Then, in a PowerShell console, run . $profile to load the functions. Then from now on all you will need to do is run sshkey, and then paste the key into wherever you need via the clipboard.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Riley Taylor
  • 111
  • 1
  • 3
10

If you only have your private key available, you can generate the public key from it:

ssh-keygen -y

or

ssh-keygen -y -f path/to/private_key
Kontrollfreak
  • 1,800
  • 12
  • 24
8

The following command will save the SSH key on the clipboard. You only need to paste at the desired location.

cat ~/.ssh/id_rsa.pub | pbcopy
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prabhat Kasera
  • 1,129
  • 11
  • 28
7

Open your id_dsa.pub or some_name.pub file with gedit and copy-paste the contents!

Just use:

~/.ssh$ gedit some_name.pub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sbdv0
  • 79
  • 1
  • 1
  • When i do so, its opening a blank file. Its not showing any text in it. But when i browse through file manager, i'm able to see the text. – iamprem Mar 07 '15 at 20:51
7

Use:

# sudo su
# cd /home/user/.ssh
.ssh# gedit id_rsa.pub

Then copy the entire file without any spaces. Click your icon at the top right of the GitHub page, go to settings, and add ssh.

Paste the copy into the space. It may prompt for your GitHub password. Enter it. Save.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yavorcik
  • 111
  • 2
  • 5
7

In UBUNTU +18.04

         ssh-keygen -o -t rsa -b 4096 -C "email@example.com" 

And After that Just Copy And Paste

         cat ~/.ssh/id_rsa.pub 

or

         cat ~/.ssh/id_dsa.pub
Shabeer K
  • 1,489
  • 16
  • 23
6

It can be found on this path (default path):

/Users/john/.ssh

john is your Mac username.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Waseem Sarwar
  • 2,645
  • 1
  • 21
  • 18
4

It is very simple. After you generated ssh key on your computer, you can access your public ssh key by following command

 cat ~/.ssh/id_rsa.pub

You should see output similar to the following:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyVGaw1PuEl98f4/7Kq3O9ZIvDw2OFOSXAFVqilSFNkHlefm1iMtPeqsIBp2t9cbGUf55xNDULz/bD/4BCV43yZ5lh0cUYuXALg9NI29ui7PEGReXjSpNwUD6ceN/78YOK41KAcecq+SS0bJ4b4amKZIJG3JWmDKljtv1dmSBCrTmEAQaOorxqGGBYmZS7NQumRe4lav5r6wOs8OACMANE1ejkeZsGFzJFNqvr5DuHdDL5FAudW23me3BDmrM9ifUzzjl1Jwku3bnRaCcjaxH8oTumt1a00mWci/1qUlaVFft085yvVq7KZbF2OPPbl+erDW91+EZ2FgEi+v1/CSJ5 your_username@hostname

Also note that the public key begins with ssh-rsa and ends with your_username@hostname.

3

Open terminal nano ~/.ssh/id_rsa.pub

Nick Wood
  • 680
  • 6
  • 10
3

Just to give a new perspective to that question, if you use github, you could find your public key at: https://github.com/${USERNAME}.keys

JOduMonT
  • 41
  • 3
3

id_rsa.pub is not a directory so you can not use cd to open it.

instead you can use the command cd ~/.ssh to access the folder containing your keys, then use cat id_rsa.pub, nano id_rsa.pub or gedit id_rsa.pub to access the key file.

2

On a Mac, you can do this to copy it to your clipboard (like cmd + c shortcut)
cat ~/Desktop/ded.html | pbcopy
pbcopy < ~/.ssh/id_rsa.pub

and to paste pbpaste > ~Documents/id_rsa.txt

or, use cmd + v shorcut to paste it somewhere else.

~/.ssh is the same path as /Users/macbook-username/.ssh
You can use Print work directory: pwd command on terminal to get the path to your current directory.

Jun
  • 2,942
  • 5
  • 28
  • 50
2

I use Git Bash for my Windows.

$ eval $(ssh-agent -s) //activates the connection

  • some output

$ ssh-add ~/.ssh/id_rsa //adds the identity

  • some other output

$ clip < ~/.ssh/id_rsa.pub //THIS IS THE IMPORTANT ONE. This adds your key to your clipboard. Go back to GitHub and just paste it in, and voilá! You should be good to go.

J.W
  • 1,135
  • 4
  • 13
  • 21
1

On Mac/unix and Windows:

ssh-keygen then follow the prompts. It will ask you for a name to the file (say you call it pubkey, for example). Right away, you should have your key fingerprint and your key's randomart image visible to you.

Then just use your favourite text editor and enter command vim pubkey.pub and it (your ssh-rsa key) should be there.

Replace vim with emacs or whatever other editor you have/prefer.

sawreals
  • 338
  • 4
  • 15
  • even though I cannot find the file on terminal or finder, opening the file with text editor works... thanks – dvdmn Jun 25 '20 at 14:58
0

ssh-add is used to show the public key.

man ssh-add

-L  Lists public key parameters of all identities currently repre‐
    sented by the agent.

On my Linux system I copy it using xclip

ssh-add -L | xclip
manero
  • 1
  • 1
    Hi manero :) Welcome! There are already a number of answers arond the usage of `ssh-add`, could you provide more specifics that would make your Answer more unique? It may otherwise get flagged as a duplicate answer (not a bad thing, just a tidy up process in stackoverflow). Having said that I would suggest checking all existing answers for any overlap in your thinking in the future. Happy StackOverflowing! – tjheslin1 Mar 24 '22 at 16:53