What, exactly, does ssh-copy-id do?

This little one liner script works on sh, bash, and zsh. I use it every time there is no ssh-copy-id, for example when I’m on older version of OSX.

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> ~/.ssh/authorized_keys'

How it works

I am sending the public keay to the Unix standard output (STDOUT) using the cat command. I then connect the STDOUT of cat to the standard input (STDIN) of the ssh.

The ssh executes the cat command on the server. Remember that the we have our key in the STDIN now? This key gets passed from ssh to the cat command executed on a server. The >> operator redirects the STDOUT of the cat to the end of the ~/.ssh/authorized_keys file. This way the key from public keys is appended to the authorized_keys on the server.

IMO It’s better than manual copying and pasting: in this case you know exactly what content will end up in the file

Leave a Comment