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?
4 Answers
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
.

- 158,093
- 24
- 286
- 300
-
21Great. Just run `ssh-copy-id root@server`. – Fedir RYKHTIK Jan 07 '14 at 10:08
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.

- 2,358
- 1
- 27
- 45
-
2This 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
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.
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

- 1,198
- 1
- 14
- 19