How to append authorized_keys on the remote server with id_rsa.pub key

SshPublic KeyAuthorized Keys

Ssh Problem Overview


How to append authorized_keys on the remote server with id_rsa.pub key from the local machine with a single command?

Ssh Solutions


Solution 1 - Ssh

ssh-copy-id user@remote_server

http://linux.die.net/man/1/ssh-copy-id

Solution 2 - Ssh

Adding an authorized key could be one-lined this way (use double-quotes so it's interpreted before sent):

ssh user@server "echo \"`cat ~/.ssh/id_rsa.pub`\" >> .ssh/authorized_keys"

Solution 3 - Ssh

This does the trick:

cat ~/.ssh/id_rsa.pub | (ssh user@host "cat >> ~/.ssh/authorized_keys")

Appends the local public key the remote authorized_keys file.

Solution 4 - Ssh

The ssh-copy-id program is the standard way but the key can be appended manually to the ~/.ssh/authorized_keys file:

cat ~/.ssh/id_rsa.pub | ssh username@host "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

This does not check if the key already exists and can lead to duplicates.

Solution 5 - Ssh

The most convenient option is the ssh-copy-id command. It can append the public key to ~/.ssh/authorized_keys. For example:

ssh-copy-id -f -i id_rsa.pub username@host

Where:

  • -f: force mode -- copy keys without trying to check if they are already installed
  • -i: [identity_file]

Solution 6 - Ssh

You can avoid some of the quoting with:

ssh user@host tee -a .ssh/authorized_keys < ~/.ssh/id_rsa.pub

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshilovkView Question on Stackoverflow
Solution 1 - SshDougView Answer on Stackoverflow
Solution 2 - SshMoseView Answer on Stackoverflow
Solution 3 - SshwcochranView Answer on Stackoverflow
Solution 4 - Sshuser4804619View Answer on Stackoverflow
Solution 5 - SshMiroslawView Answer on Stackoverflow
Solution 6 - Sshuser260532View Answer on Stackoverflow