Skip to content

Instantly share code, notes, and snippets.

@kmf
Created September 9, 2025 09:22
Show Gist options
  • Select an option

  • Save kmf/c060acd502e87fa5f7815111ad048e45 to your computer and use it in GitHub Desktop.

Select an option

Save kmf/c060acd502e87fa5f7815111ad048e45 to your computer and use it in GitHub Desktop.
get-ssh-keys.sh
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# The URL of the SSH keys to download.
KEYS_URL="https://github.com/kmf.keys"
# The directory where SSH keys are stored.
SSH_DIR="$HOME/.ssh"
# The file where authorized keys are stored.
AUTH_KEYS_FILE="$SSH_DIR/authorized_keys"
# --- Main Script ---
# 1. Check for `curl` and `wget`. `curl` is preferred.
if ! command -v curl &> /dev/null
then
if ! command -v wget &> /dev/null
then
echo "Error: Neither 'curl' nor 'wget' could be found. Please install one of them."
exit 1
fi
fi
# 2. Create the .ssh directory if it doesn't exist and set permissions.
if [ ! -d "$SSH_DIR" ]; then
mkdir -p "$SSH_DIR"
echo "Created directory: $SSH_DIR"
fi
# Ensure the .ssh directory has secure permissions (700).
chmod 700 "$SSH_DIR"
echo "Set permissions for $SSH_DIR to 700."
# 3. Download the keys and append them to the authorized_keys file.
# Use `curl -s` for silent mode (no progress bar) or `wget -qO-` for quiet output to stdout.
if command -v curl &> /dev/null
then
echo "Downloading keys from $KEYS_URL using curl..."
curl -s "$KEYS_URL" >> "$AUTH_KEYS_FILE"
elif command -v wget &> /dev/null
then
echo "Downloading keys from $KEYS_URL using wget..."
wget -qO- "$KEYS_URL" >> "$AUTH_KEYS_FILE"
fi
# 4. Set secure permissions for the authorized_keys file.
chmod 600 "$AUTH_KEYS_FILE"
echo "Set permissions for $AUTH_KEYS_FILE to 600."
echo "✅ Script complete. SSH keys have been added to $AUTH_KEYS_FILE."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment