157

I am trying to copy a public key to the clipboard on macOS, but I keep getting "no such file or directory." The command I am using is pasted below

pbcopy < ~/.ssh/id_rsa.pub
Michael
  • 8,362
  • 6
  • 61
  • 88
user1850254
  • 2,091
  • 3
  • 16
  • 17

13 Answers13

126

To copy your public key to the clipboard

cat ~/.ssh/id_rsa.pub | pbcopy

This pipes the output of the file to pbcopy.

iAmWillShepherd
  • 1,317
  • 1
  • 9
  • 6
122

cat ~/.ssh/id_rsa.pub

then you can copy your ssh key

T J
  • 42,762
  • 13
  • 83
  • 138
Palermo Andre Deschamps
  • 1,651
  • 1
  • 13
  • 11
38

Another alternative solution, that is recommended in the github help pages:

pbcopy < ~/.ssh/id_rsa.pub

Should this fail, I recommend using their docs to trouble shoot or generate a new key - if not already done.

Github docs

Powderham
  • 1,510
  • 12
  • 14
30

Check the path where you have generated the public key. You can also copy the id_rsa by using this command:

clip < ~/.ssh/id_rsa.pub
Eugene S
  • 6,709
  • 8
  • 57
  • 91
sandip divekar
  • 1,628
  • 5
  • 22
  • 40
18

Your command is right, but the error shows that you didn't create your ssh key yet. To generate new ssh key enter the following command into the terminal.

ssh-keygen

After entering the command then you will be asked to enter file name and passphrase. Normally you don't need to change this. Just press enter. Then your key will be generated in ~/.ssh directory. After this, you can copy your key by the following command.

pbcopy < ~/.ssh/id_rsa.pub 

or

cat .ssh/id_rsa.pub | pbcopy

You can find more about this here ssh.

Muzahid
  • 5,072
  • 2
  • 24
  • 42
10

For using Git bash on Windows:

cat ~/.ssh/id_rsa.pub > /dev/clipboard

(modified from Jupiter St John's post on Coderwall)

eh1160
  • 674
  • 1
  • 6
  • 14
9

Windows:

cat ~/.ssh/id_rsa.pub

Mac OS:

cat ~/.ssh/id_rsa.pub | pbcopy
Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32
6

With PowerShell on Windows, you can use:

Get-Content ~/.ssh/id_rsa.pub | Set-Clipboard
Wilka
  • 28,701
  • 14
  • 75
  • 97
3

To copy your public ssh key on a Windows machine you can do:

Go to the "/ssh" folder

cd  C:\Users\<your-user>\.ssh\

List to see the keys

ls ~/.ssh

Copy the public key to clipboard(starts with "id_" and ends with ".pub")

type id_xxxxxxx.pub | clip
Anas
  • 1,345
  • 14
  • 15
2

Does the file ~/.ssh/id_rsa.pub exist? If not, you need to generate one first:

ssh-keygen -t rsa -C "your_email@example.com"
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
1

Another alternative solution:

cat  ~/.ssh/id_rsa.pub |  xsel -i -b

From man xsel :

-i, --input

read standard input into the selection.

-b, --clipboard

operate on the CLIPBOARD selection.

Michal Przybylowicz
  • 1,558
  • 3
  • 16
  • 22
1

Although the OP mentions one possible ssh key file name (id_rsa.pub), no one has mentioned that there are different possible names for your ssh key. Github accepts three, for example:

id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

You would be better off checking if you have any keys, such as:

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

Based on what you find, then use your copy command, such as

pbcopy < ~/.ssh/<your_key>

See Github's Documentation on checking for existing keys.

Reid
  • 21
  • 3
0
cat .ssh/id_rsa.pub | bcopy

This works for me.

mic
  • 927
  • 8
  • 17