Skip to content

Instantly share code, notes, and snippets.

@elonmallin
Last active March 22, 2024 12:02
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save elonmallin/2cc94c45ab57e2060498e855acefade0 to your computer and use it in GitHub Desktop.
Save elonmallin/2cc94c45ab57e2060498e855acefade0 to your computer and use it in GitHub Desktop.
One-liner ssh-keygen and ssh-copy-id for Windows powershell
ssh-keygen && cat $env:userprofile/.ssh/id_rsa.pub | ssh user@linuxserver 'cat >> .ssh/authorized_keys'
@BerezkovN
Copy link

Thanks!

@ZilchBloke
Copy link

ZilchBloke commented Mar 20, 2023

There is a typo!?

Before the pipe( | ) , $env and userprofile must have : in between.
Also all the / is replaced by \ , before the piping into linux server.
ssh-keygen && cat $env:userprofile\.ssh\id_rsa.pub | ssh user@linuxserver 'cat >> .ssh/authorized_keys'

I Recommend :

ssh-keygen -o -a 100 -t ed25519 -f id_ed25519.pub -C "user@windowsclient" && cat $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@linuxserver "mkdir ~/.ssh/ ; touch ~/.ssh/authorized_keys ; cat >> ~/.ssh/authorized_keys"

Goodluck

@elonmallin
Copy link
Author

There is a typo!?

Before the pipe( | ) , $env and userprofile must have : in between. Also all the / is replaced by \ , before the piping into linux server. ssh-keygen && cat $env:userprofile\.ssh\id_rsa.pub | ssh user@linuxserver 'cat >> .ssh/authorized_keys'

I Recommend :

ssh-keygen -o -a 100 -t ed25519 -f id_ed25519.pub -C "user@windowsclient" && cat $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@linuxserver "mkdir ~/.ssh/ ; touch ~/.ssh/authorized_keys ; cat >> ~/.ssh/authorized_keys"

Goodluck

Thanks, I added the : to the env var. Don't know what you mean by the slashes since what is being piped to the ssh command is the content so slashes should have no impact. I'll keep the command shorter and ppl can feel free to add more specific params if they want =)

@ZilchBloke
Copy link

ZilchBloke commented Mar 21, 2023

Don't know what you mean by the slashes since what is being piped to the ssh command is the content so slashes should have no impact.

usually windows have '\' and linux and unix have '/' so i was concerned. It seems, when used with $env:USERPROFILE , either / or \ is fine.
i also followed chris's article on https://chrisjhart.com/Windows-10-ssh-copy-id/#copy-ssh-key-to-remote-linux-device

Thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment