One-line to add public ssh key to authorized keys file on remote system

Sometimes I feel like Linux hasn’t been accepted publicly because people are concerned about getting their public ssh key appended to the authorized_keys file on the servers they are trying to administer. They think they have to scp it over, then ssh in, and finally append it to the correct file.

It’s such a shame because it can easily be accomplished with this one command.

ssh me@somespecialserver.local “cat >> ~/.ssh/authorized_keys” < ~/.ssh/id_rsa.pub

This executes the cat command on the remote server and takes the input from the local ~/.ssh/id_rsa.pub file.

If you don’t have an id_rsa.pub file, run the following command and follow the instructions.

ssh-keygen -t rsa

Once you’re authorized, you wont have to type a password to log on to that server anymore. However, you will still need it for sudo commands.

Leave a Reply