SSH public Key distribution to remote servers using bash script

Andrius Ordojan
Nov 15, 2023

--

The provided Bash script is designed to facilitate the addition of multiple SSH public keys to the authorized_keys file on a remote server. The PUBLIC_KEYS array contains individual strings, each representing a unique public key. The script establishes a connection to the remote server, iterates through the array, and appends each public key to the designated authorized_keys file. The user can customize the REMOTE_SERVER variable to specify the SSH username and IP address of the target server.


#!/bin/bash

# Remote server details
REMOTE_SERVER="your_username@your_remote_server_ip"
AUTHORIZED_KEYS_FILE="$HOME/.ssh/authorized_keys"

# Public keys to be added
PUBLIC_KEYS=(
"keys"
"more keys..."
)

# Loop through each public key and append it to the authorized_keys file on the remote server
for key in "${PUBLIC_KEYS[@]}"; do
ssh "$REMOTE_SERVER" "echo '$key' >> $AUTHORIZED_KEYS_FILE"
done

# Display a message indicating success
echo "Public keys have been added to $REMOTE_SERVER:$AUTHORIZED_KEYS_FILE"

--

--

Andrius Ordojan
0 Followers

Aiming to write about diverse range of topics. Everything from Kubernetes and web development to combat sports, home labs, and even the world of fungiculture.