How to Copy Public SSH Key to Clipboard in macOS?

less than a minute of reading

This quick article shows how to copy your public SSH key into macOS clipboard using a single command in terminal.

The following command copies your SSH public key to your clipboard.

cat ~/.ssh/id_rsa.pub | pbcopy

Let's quickly break it down and back it up with some facts.

  1. The cat command prints content of any given file into the standard output. Since we're interested in your SSH keys, their typical location is within .ssh inside home directory.
  2. Next, we use the pipe operator (represented by the | character). Think of it as it takes the output of the command on the left and uses it as the input to the command on the right.
  3. Finally the command on the right is pbcopy - macOS utility that copies whatever it receives from standard input into the clipboard.

Once executed, your public SSH key is ready to be pasted anywhere. Probably you want to use it in GitHub or server's authorized keys.

Keep in mind that your SSH key pair can me named differently, depending on the cryptographic algorithm being used to generate them. If your keys aren't RSA, they're probably generated using modern Ed25519, and therefore your public key will be named id_ed25519.pub instead of id_rsa.pub.

⛔️ Important note: As a final word, under no circumstances copy (and accidentally share) your private key - the one that does not end with the .pub extension.


Words: 236
Published in: macOS

Related Articles   📚