Last Updated: February 25, 2016
·
952
· jonlunsford

SSH: Simple CLI function to add keys to server.

This is how I quickly add SSH keys to any remote server. I always found myself messing up the cat syntax when adding keys. This function just takes your SSH user@hostname, and assuming your authorized_keys file exists on the server, adds your public key.

function add-ssh-keys() {
  cat ~/.ssh/id_rsa.pub | ssh $1 'cat >> ~/.ssh/authorized_keys'
}

Simply add this to your .zshrc or .bash_profile file, and restart your session or reload the profile.

Usage:

add-ssh-keys root@myhostname

You will be prompted to enter the password for your user, and your'e good to go.

2 Responses
Add your response

Thank you for your tip! I preferred to use ssh-copy-id.

over 1 year ago ·

Thanks! Yes, ssh-copy-id is much more robust.

over 1 year ago ·