163

I'm generating an SSH key but I don't know how to copy the key from id_rsa.pub to the clipboard. I'm using BackBox Linux.

alex
  • 6,818
  • 9
  • 52
  • 103
Ravi Shankar
  • 2,101
  • 4
  • 14
  • 15
  • 9
    It is infuriating that there is no clue why the question is unsuitable, and equally infuriating that we can't vote out the administrator who is responsable, for he is anonymous. – Albert van der Horst Dec 14 '21 at 10:20
  • @AlbertvanderHorst The people responsible for closing questions are not anonymous; you can find them in [the timeline](https://stackoverflow.com/posts/32039476/timeline#history_6ccd9074-0448-4f8b-9a1f-a9256d1583b6). – smitop Dec 18 '21 at 12:34

5 Answers5

173
xclip -sel c < input_file

will copy the contents of input_file to clipboard. xclip requires installation. To install

sudo apt install xclip

-sel stands for -selection. c is for clipboard. Interchangeable.

Capable of much more, I advise reading its man page.

There is also xsel. This answer on Unix SE gives a very thorough answer to this exact question.

Haggra
  • 3,539
  • 2
  • 20
  • 28
  • 5
    if someone wants to use `alias` for this you can do this: `alias cpc="xclip -sel c < "` and you can use it like so: `cpc file_name.txt`; cpc stands for copy contents – alexzander Jun 28 '21 at 11:23
  • 1
    This only works when you remain in the linux sphere, if it needs copy to clipboard from a Windows app/tool, you're still confined to cat and then selecting the text on display. Anyone's got a better solution for that? – Julius Aug 30 '21 at 13:55
  • 6
    Why not just `cat input_file | xclip`? – Yannick Copin Dec 10 '21 at 18:32
  • 1
    This is the best answer. Thank you! – Qwerty Jul 26 '23 at 05:51
58

In macOS (but maybe also in Linux operating systems) you can do the following that will directly copy the content of id_rsa.pub to clipboard:

pbcopy < ~/.ssh/id_rsa.pub
Valerio Bozz
  • 1,176
  • 16
  • 32
Yu N.
  • 1,765
  • 11
  • 9
  • 1
    thank you for posting this answer despite the fact that others would insist on ONLY "linux". I certainly found this useful when i searched for "how to copy from unix terminal clipboard in mac" – Sidharth Ghoshal Oct 14 '22 at 17:31
33

This command copies the content of a file into your clipboard:

xclip -selection clipboard -i < file.txt

Also, this shorter command copies the content of a file into the mouse middle click:

xclip < file.txt
Valerio Bozz
  • 1,176
  • 16
  • 32
Firoj Siddiki
  • 1,649
  • 1
  • 20
  • 22
4

If you're copying from terminal (like if you use the cat command already posted), highlight the key details and use Ctrl + Shift + C. This should put it on your clipboard. You can also right click and select 'copy' from terminal.

e_m
  • 57
  • 1
  • 4
  • 12
    If the file is larger than the screen (for instance when using vim) then this doesn't really work. Only copies the part you have selected – Joe Phillips May 28 '21 at 18:56
-76

You can use:

cat ~/.ssh/id_rsa.pub

I hope that help you, if not:

Set up SSH for Git and Mercurial on Mac OSX/Linux

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
william.bonilla
  • 392
  • 4
  • 5