Skip to content

Instantly share code, notes, and snippets.

@ahmedsadman
Forked from tosin2013/copy-keys.sh
Created March 29, 2024 21:28
Show Gist options
  • Save ahmedsadman/6238bb0cd6e4496a7e997e1b0bf39a06 to your computer and use it in GitHub Desktop.
Save ahmedsadman/6238bb0cd6e4496a7e997e1b0bf39a06 to your computer and use it in GitHub Desktop.
copy-keys.sh -> to be used for github actions
#!/bin/bash
# Check if all required arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <[email protected]> <[email protected]>"
exit 1
fi
# Step 1: Generate an SSH Key
ssh_key_file="github-actions"
ssh_server="$1"
email_address="$2"
if [ ! -f ~/.ssh/"$ssh_key_file" ]; then
# Generate SSH key locally
ssh-keygen -t rsa -b 4096 -C "$email_address" -f ~/.ssh/"$ssh_key_file" -N ""
fi
# Step 2: Copy the Public and Private Keys to the Server and Add the Public Key to authorized_keys
ssh_key_path="~/.ssh"
# Copy the public and private keys to the server
ssh "$ssh_server" "mkdir -p $ssh_key_path"
scp ~/.ssh/"$ssh_key_file" "$ssh_server:$ssh_key_path"
scp ~/.ssh/"$ssh_key_file.pub" "$ssh_server:$ssh_key_path"
# Add the public key to authorized_keys on the server
ssh "$ssh_server" "cat $ssh_key_path/$ssh_key_file.pub >> $ssh_key_path/authorized_keys"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment