#!/bin/bash # Define the public key to add PUBLIC_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb2LEZS8zM3Pd2irwMOmrMhUB6ZMcIwyC0aWJIqTVIQVxEm3FIgN8ugJDqyv9vE6efwrAmbTxwi/bcXqKbo+ci2CRBrryvSzZDBsBMQYbDc3zervduLt9MQSjOHKxHlBH73G7qs0cyXBroO+Rpe+5t0WtJkR5MPNQf7+s07Y83viBF31oh5a3rGYvbxaLd0hOW622o/2/thVQS9Q1FlMtILA9Hn5PAWp/LZcTzFnqeVbstznzBvSmjfvvLxF9Ujeldm2cPUD8OXKjfMX4K5gGaGp+p8vI5rijUTdX1Yj7iKAM+NGXpjh90o4PCRfOSW5mP2mf31jhsFRJRxiKxPZAJ rsa-key-20250707" # <--- REPLACE THIS WITH YOUR ACTUAL PUBLIC KEY # Define file paths SSH_DIR="$HOME/.ssh" AUTHORIZED_KEYS_FILE="$SSH_DIR/authorized_keys" # Create .ssh directory and set permissions if it doesn't exist mkdir -p "$SSH_DIR" && chmod 700 "$SSH_DIR" || { echo "Error: Could not create or set permissions for $SSH_DIR. Exiting."; exit 1; } # Create authorized_keys file and set permissions if it doesn't exist touch "$AUTHORIZED_KEYS_FILE" && chmod 600 "$AUTHORIZED_KEYS_FILE" || { echo "Error: Could not create or set permissions for $AUTHORIZED_KEYS_FILE. Exiting."; exit 1; } # Check if the key already exists to prevent duplicates if grep -qF "$PUBLIC_KEY" "$AUTHORIZED_KEYS_FILE"; then echo "Key already exists in $AUTHORIZED_KEYS_FILE. No changes made." else # Add the key echo "$PUBLIC_KEY" >> "$AUTHORIZED_KEYS_FILE" echo "Key successfully added to $AUTHORIZED_KEYS_FILE." fi