Skip to content

Instantly share code, notes, and snippets.

@JosiahSiegel
Last active May 15, 2024 15:20
Show Gist options
  • Save JosiahSiegel/3b94324024c4500cad3526e8a90c0e86 to your computer and use it in GitHub Desktop.
Save JosiahSiegel/3b94324024c4500cad3526e8a90c0e86 to your computer and use it in GitHub Desktop.
SSH Key Authentication

SSH Key Authentication

Generate key pair

ssh-keygen -t ed25519 -b 4096

Restrict private key access

icacls "C:/Users/<user>/.ssh/id_ed25519.pub" /grant :R

Authorize connection

$USER_AT_HOST="<remote user>@<remote hostname>"
$PUBKEYPATH="$HOME\.ssh\id_ed25519.pub"

$pubKey=(Get-Content "$PUBKEYPATH" | Out-String); ssh -p 2222 "$USER_AT_HOST" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '${pubKey}' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

doc

#!/bin/bash
set -e
DEFAULT_KEY_PATH="$HOME"
prompt() {
local prompt=$1
read -p "$prompt: " value
echo "${value}"
}
prompt_with_default() {
local prompt=$1
local default_value=$2
read -p "$prompt [$default_value]: " value
echo "${value:-$default_value}"
}
remote_user=$(prompt "Enter remote username")
remote_hostname=$(prompt "Enter remote hostname")
remote_port=$(prompt "Enter remote port")
key_path=$(prompt_with_default "Enter the key path" "$DEFAULT_KEY_PATH")
echo "y" | ssh-keygen -t ed25519 -b 4096 -f $key_path/.ssh/id_ed25519 -N ""
USER_AT_HOST="${remote_user}@${remote_hostname}"
PUBKEYPATH="$key_path/.ssh/id_ed25519.pub"
pubKey=$(cat "$PUBKEYPATH")
ssh -p $remote_port "$USER_AT_HOST" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$pubKey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment