43

I remember there is a command to send public key to the remote host that I want. I want to use that feature to send one of my public keys to the other host. How can I do that?

user482594
  • 16,878
  • 21
  • 72
  • 108

4 Answers4

72

You are looking for ssh-copy-id. All this command does is create .ssh and .ssh/authorized_keys and set their permissions appropriately if they don't exist. Then it appends your public key to the end of .ssh/authorized_keys.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
17

You might be looking for this command:

cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'

It appends your public key to the servers authorized keys.

Source

flaudre
  • 2,358
  • 1
  • 27
  • 45
  • 2
    This is more portable than the accepted answer, since not all systems (I'm looking at you, OS X) have `ssh-copy-id`. Although, personally, I'd modify it slightly to `cat ~/.ssh/id_rsa.pub | ssh user@hostname 'tee -a .ssh/authorized_keys'`, but that's just personal preference. – Dan Jones Jul 27 '16 at 15:17
  • 1
    Looks like ssh-copy-id is part of MacOS Sierra, which is nice. – Graham Oct 18 '16 at 16:37
6

If your server is already set up to not accept password-based login, you might get a Permission denied (publickey) error.

This is another method to send the key, using netcat, so you don't have to authenticate. It will only work over a local network, but you can use port forwarding to do this over the internet.

On the server:

$ nc -l 55555 >> ~/.ssh/authorized_keys

On the client (replace HOSTNAME with the hostname or IP of the server):

$ nc HOSTNAME 55555 < ~/.ssh/id_rsa.pub

You can replace 55555 with an open port of your choice.

source: chat over lan from linux to linux?


Appendix for total newbies: I don't think anyone's mentioned this yet, but if you get ERROR: failed to open ID file '/home/username/.pub': No such file, you need to generate a key first. The Ubuntu help pages have a great guide on Generating RSA Keys.

Community
  • 1
  • 1
ki9
  • 5,183
  • 5
  • 37
  • 48
3

In other answers there's no example for ssh-copy-id so here it is(first you need to generate the key)

ssh-copy-id user@url
vozman
  • 1,198
  • 1
  • 14
  • 19