Skip to content

Instantly share code, notes, and snippets.

@elonmallin
Last active June 13, 2024 09:36
Show Gist options
  • 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'
@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

@fquinner
Copy link

Since google took me here when searching for a powershell ssh-copy-id, I thought I'd share my modifications to the above which allows for configurable host and uses default ssh pub key as well as ensuring permissions are correct (I just stick this in my profile script):

function ssh-copy-id($server) {
    type $env:USERPROFILE\.ssh\id_rsa.pub | ssh $server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
}

Thank you very much for sharing this!

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