Skip to content

Instantly share code, notes, and snippets.

@1x24
Last active April 14, 2023 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1x24/c96a93d3c749e996319ac6001ef4e3ab to your computer and use it in GitHub Desktop.
Save 1x24/c96a93d3c749e996319ac6001ef4e3ab to your computer and use it in GitHub Desktop.
MacOS / Linux BASH Script to create and add SSH keys to a Linux server
#!/bin/bash
############################################################################################################################################################
############################################################################################################################################################
# https://github.com/1x24
# 1x24 AT tuta.io
# March 31, 2023
############################################################################################################################################################
# This BASH script checks if you have SSH keys on your iMac or MacOS laptop, or your Linux machine.
# If you do not have the SSH keys, it generates a modern one for you.
# In either case, it copies the key to your server and makes some configuration changes to make it easy to login with the keys.
# Save then use as follows:
# (1) Open Terminal and navigate to the folder where this script is saved.
# (2) Enter the following command to make the script executable
# chmod +x use_ssh_keys.sh
# (3) Now run the script as follows, substituting in your server username (to replace USERNAME), server host (to replace MY.SERVER.HOST)
# and the server port to replace SERVER_SSH_PORT (unless told otherwise, use the number 22)
# ./use_ssh_keys.sh USERNAME MY.SERVER.HOST SERVER_SSH_PORT
# (4) From now on you can simply open terminal and run ssh MY.SERVER.HOST e.g. ssh fancy.server.com
#############################################################################################################################################################
#############################################################################################################################################################
if [ $# -ne 3 ]; then
echo "Usage: $0 server_username server_host server_ssh_port"
exit 1
fi
USERNAME="$1"
SERVER_HOST="$2"
SERVER_PORT="$3"
KEYS_FOUND=0
KEY_PATH=""
# Check for existing keys in the common locations
for key_type in ed25519 rsa; do
KEY_PATH="$HOME/.ssh/id_${key_type}.pub"
if [ -f "$KEY_PATH" ]; then
KEYS_FOUND=1
break
fi
done
# Generate a new SSH key if none are found
if [ $KEYS_FOUND -eq 0 ]; then
echo "No SSH keys found. Generating a new one..."
ssh-keygen -t ed25519 -f "$HOME/.ssh/id_ed25519" -N ""
KEY_PATH="$HOME/.ssh/id_ed25519.pub"
fi
# Copy the key to the remote server
echo "Copying SSH key to the remote server..."
PUB_KEY_CONTENT=$(cat "$KEY_PATH")
sshCommands="mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$PUB_KEY_CONTENT' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
ssh -o StrictHostKeyChecking=no -o PreferredAuthentications=password -o PubkeyAuthentication=no -p "$SERVER_PORT" "${USERNAME}@${SERVER_HOST}" "$sshCommands"
# Check the return status of the SSH command
if [ $? -eq 0 ]; then
# Append configuration to the local SSH config file
SSH_CONFIG="$HOME/.ssh/config"
echo "Appending configuration to the local SSH config file..."
{
echo "Host $SERVER_HOST"
echo " Hostname $SERVER_HOST"
echo " User $USERNAME"
echo " Port $SERVER_PORT"
echo " IdentityFile ${KEY_PATH%*.pub}"
echo " PreferredAuthentications publickey"
echo " GSSAPIAuthentication no"
echo " ChallengeResponseAuthentication no"
echo " HashKnownHosts no"
echo " KbdInteractiveAuthentication no"
echo " TCPKeepAlive yes"
echo " ServerAliveInterval 25"
echo " ServerAliveCountMax 600"
} >> "$SSH_CONFIG"
echo "Done!"
else
echo "SSH key copying failed. Please check the error message and try again."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment