15

I want to copy an SSH public key to a remote device on Windows. I'm not using PuTTYgen rather just OpenSSH through PowerShell. I ran

ssh-keygen -t rsa

And it gave me a public and private key in my .ssh folder but if I try to do

ssh-copy-id -i ~/.ssh/id_rsa.pub <name@machine>

I get the same error:

ssh-copy-id : The term 'ssh-copy-id' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Perhaps the ssh-copy-id command was meant for Linux, but I was able to get some other commands meant for Linux to run.

How do I copy the key from my Windows laptop to my remote machine? Is it even possible to do just in Windows without an app like PuTTYgen?

squillman
  • 8,551

4 Answers4

23

(given id_rsa.pub exists as when previously generated with "ssh-keygen -t rsa")

This single line answer is barely the same as that given by barlop without cygwin, mentioning that with windows native ssh it works as well...

C:\> type %USERPROFILE%\.ssh\id_rsa.pub | ssh user@host "cat >> .ssh/authorized_keys"
barlop
  • 25,198
jambalaio
  • 331
  • 2
  • 4
3

All you are actually doing is adding the contents of your id_rsa.pub file to your ~/.ssh/authorized_keys file on your linux host. (You may need to run 'systemctl restart ssh'.)

On your windows machine you then use the -i parameter:

ssh -i {path to private key file}\id_rsa {name}@{machine}

If you used a passphrase you will be prompted for that, otherwise it will let you right in.

Benj Sanders
  • 131
  • 5
3

I've got cygwin installed, and that has ssh and includes ssh-copy-id (which is a script btw).

$ which ssh-copy-id
/usr/bin/ssh-copy-id

By the way if you get an error like what you got then a basic troubleshooting step to simplify is just to try $ ssh-copy-id <ENTER> so as you know it's nothing to do with all the parameters you put after ssh-copy-id.

This article mentions the manual method of ssh-copy-id https://chrisjhart.com/Windows-10-ssh-copy-id/

type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {IP-ADDRESS-OR-FQDN} "cat >> .ssh/authorized_keys"

and he gives an example

PS C:\Users\Christopher> type $env:USERPROFILE\.ssh\id_rsa.pub | ssh 192.168.30.31 "cat >> .ssh/authorized_keys"

a bit more info, related To use ssh-id-copy do you need both id_rsa.pub and id_rsa?

barlop
  • 25,198
0

When installing OpenSSH, should include scp commands. just type scp on your command prompt to ensure this program is installed.

You could use this command:

  1. First change prompt to working directory

    cd /D %HOMEDRIVE%%HOMEPATH%"/.ssh" (CMD) or

    Set-Location -Path $Env:HOMEDRIVE$Env:HOMEPATH"/.ssh" (PowerShell)

  2. Using scp command

    scp -v -P {{port}} {{source}} {{userID}}@{{instanceIP}}:{{target}}

    In your case

    scp -v "id_rsa.pub" user@host:/home/directory/ then type your password.

You could check the file on target machine.

Ygautomo
  • 129