One way to copy an ssh key to another server is to use ssh-copy-id here is an article showing how.
However you might not have ssh-copy-id, or you might want to do something else, like copy several ssh keys (public keys) to another server.
You dont need ssh-copy-id to copy the keys. But if you wish to use it, you can its less typing after all. Using this cat,ssh,cat method you can copy any number of keys to any user on any server.
Many Keys
1)
First add all of your keys to “authorized_keys.org” where each line will have your wanted public keys.
2)
cat authorized_keys.org | ssh -p 22 root@server.com "mkdir -p /root/.ssh/ 2> /dev/null; cat - >> /root/.ssh/authorized_keys"
One Key
If you just want to copy 1 key:
cat id_rsa.pub | ssh -p 22 root@server.com "mkdir -p /root/.ssh/ 2> /dev/null; cat - >> /root/.ssh/authorized_keys"
Explanation
We take a file (which has our public key, one on each line). We read it into ssh via a pipe. And we run 2 commands on the remote server. We create the .ssh folder just incase it doesnt exist. And we append the key (which is in stdin), so we “catch” it with “cat -” and append it with “>>” to the appropriate file.
3 things to keep in mind:
- if your login in to the root user you can add to any users keys. Your not limited to only changing roots keys. All you would need to do is change double quotes to this: “mkdir -p /home/user1/.ssh/ 2> /dev/null; cat – >> /home/user1/.ssh/authorized_keys” (assuming your user is user1).
- if your login in to another user (not root) then you can just add the key very simply like so: “mkdir -p ~/.ssh/ 2> /dev/null; cat – >> ~/.ssh/authorized_keys“
- If your remote server requires a specific ssh key to login before it allows you to do anything, than you can add in “-i /path/to/private.key“. Add it in right before the double quoted remote command & preferably before the user@server (“root@server.com” in the above case)
- if the ssh server is listening on port 22, then putting “-p 22” is not required. However and for example if your ssh server is listening on port 1022 then change that to read “-p 1022“