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.
Stephan
February 13th, 2018 at 6:40 am
Thanks for this! Works like a charm
Karl Wagner
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!