Is there an equivalent to ssh-copy-id for Windows?

Solution 1:

ssh-copy-id is a pretty simple script that should be pretty easy to replicate under windows.

If you ignore all the parameter handling, error handling, and so on, these are the two commands from ssh-copy-id that are actually doing the work most of the time.

GET_ID="cat ${ID_FILE}"
{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1

Using the putty tools a command like this should be equivalent (not tested).

type  public_id | plink.exe username@hostname "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys"

If you want to do all the same error handling, and the automatic key location, I am sure writing a script under Windows will be a lot trickier, but certainly possible.

Solution 2:

These answers didn't help me out. I really didn't need any crazy scripts. I had created a public key on my client machine in git bash and was trying to copy it to a VPS.

After creating your public key, the key should be stored as "(whatever folder you started in)/.ssh/id_rsa.pub"

So use this command:
cat ~/.ssh/id_rsa.pub | ssh [email protected] "cat >> ~/.ssh/authorized_keys" where user is your username (sometimes "root", or whatever you may have set up), and replace 123.45.67.89 with your machine / host / VPS's IP address.

If the directory .ssh is not yet created on the host machine, use this small variation:
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"


Solution 3:

ssh-copy-id does a couple of things (read the man page for details), but the most important thing it does is append the contents of your local public key file to a remote file called authorized_keys.

  • You could do this yourself by opening the key file with a text editor and pasting the contents in the Kitty terminal.
    echo 'long_line_with_contents_of_public_key_file' >> .ssh/authorized_keys

  • Alternatively, you could upload the file using WinSCP (which uses sftp, or scp as a fallback) and do something similar to my previous suggestion, without the ugly copy/pasting.
    cat id_rsa.pub >> .ssh/authorized_keys
    where id_rsa.pub is the filename of the public key you uploaded.


Solution 4:

Inspired by zoredache's answer, I've created a bunch of scripts that are the windows version. However they all depend on plink. Please take a look here

https://github.com/VijayS1/Scripts/blob/master/ssh-copy-id/

I also have a winscp script that can be used as per another answer. :) Excerpt from the readme:

Attempted methods so far:

  • DOS(.cmd) - Success
  • VBS (.vbs) - Success
  • Powershell(.ps1) - Success
  • mremoteNG (ext app) - Success
    • Select Host, right click, external tools, select Scriptname
  • WinSCP script (.bat) - Success
    • # "WinSCP.com" /script=".\Scriptname" /parameter "user[:password]@example.com" "id_rsa.pub" [/log=".\copyssh.log]"

Solution 5:

In Windows 7 there is a ssh.exe

Here is what worked for me:

1. create identity (on windows)

c:\>ssh-keygen

That created an identity file in the home directory. I changed the name of the public key to "id_rsa"

2. copy the file to the target linux system using the ssh Credits to https://serverfault.com/users/984/zoredache for his answer

c:\>ssh user@lnxhost "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" < \\path_to_where_the_file_was_generated_from_ssh_key_gen\id_rsa.pub

Note: For some reason piping didn't work for me:

# this should work but it didn't work for me 
type file | ssh user@lnxhost "cat >> /tmp/t.txt"

3. Correct the file on linux The id_rsa.pub file on windows is multiline where linux expects it in in a single line so we have to correct it a bit. Login to linux and open the file:

vi ~/.ssh/authorized_keys

For example:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "2048-bit RSA, user@winhost"
AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla33
5flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMC
Y58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVy
ax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdT
LFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHE
Qkw+1wuV6dFoT1/hngSw==
---- END SSH2 PUBLIC KEY ----

should become

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla335flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMCY58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVyax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdTLFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHEQkw+1wuV6dFoT1/hngSw== user@winhost

4. test it

c:\>ssh user@lnxhost "ls -al /tmp/"

This should list the content of /tmp without asking for the password.