Copy SSH Public Key to Server Without ssh-copy-id

This command will copy your ssh public key from your local machine to your server. This is an alternative for when you don’t have the ssh-copy-id command (for example, on Windows). Replace [user]@[host] with your own username and host.

ssh [user]@[host] 'mkdir -m 700 ~/.ssh; echo ' $(< ~/.ssh/id_rsa.pub) ' >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys'

This creates the .ssh directory on the remote host with proper permissions, if it doesn’t exist. It creates the authorized_keys file and adds your public key to it. If it already exists on the server, this appends your public key to authorized_keys, and verifies it has proper permissions.

See more:

We've 2 Responses

  1. May 2nd, 2022 at 9:14 pm

    Thanks so much for this! Works perfectly.
    I use alternate ports on my VPS and the command ssh-copy-id doesn’t work with ports other than 22, unless a special parameter is issued on the command line.
    With this, I just add -p and the port number after the ssh command and everything works!

    Karl Wagner

Leave a Reply to Stephan Cancel reply

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]