Notebook

One-Click SSH Key Login Setup

2025-01-02

There are many people, especially beginners, struggle with configuring SSH key logins on Linux. To make things easier, I wrote a simple one-click script that handles everything for you. (only suitable for MacOS or Linux)

Introduction

This one-click solution makes SSH key setup fast and hassle-free, perfect for anyone who wants to skip the tedious manual configuration.

What It Does

  • Generates or Uses an Existing SSH Key Pair:
    The script checks if your key exists and asks whether to replace it, so you don't have to worry about generating keys manually.

  • Updates Your SSH Config:
    It automatically adds your server details under a custom alias in your ~/.ssh/config file. This means you can connect to your server with a simple command like ssh myserver.

  • Uploads Your Public Key:
    Your public key is automatically uploaded to the remote server using ssh-copy-id.

  • Tests the Connection:
    Finally, it verifies the setup by testing the SSH connection.

How to Use

  1. Download the script here: setup_ssh_key.sh

  2. Make it executable: chmod +x setup_ssh_key.sh

  3. Run the script: ./setup_ssh_key.sh

    Follow the on-screen prompts to enter:

    • The server’s IP address.

    • Your username

    • An SSH alias (e.g., myserver)

    • A name for your SSH key (e.g., id_rsa_server1)

  4. Connect to your server: Once the script completes, you can connect with: ssh myserver

The following is the code:

#!/bin/bash

# Set variables
read -p "Enter the remote server IP address: " SERVER_IP
read -p "Enter the remote server username: " SERVER_USER
read -p "Enter the SSH alias to set (e.g., myserver; afterwards, you can connect using 'ssh myserver'): " SSH_ALIAS
read -p "Enter the key name to use (customizable, e.g., id_rsa_server1): " KEY_NAME

# Set the full key path
KEY_PATH="$HOME/.ssh/$KEY_NAME"

# Check if the key already exists
if [ -f "$KEY_PATH" ]; then
    read -p "Key $KEY_PATH already exists. Replace it? (y/n): " REPLACE
    if [ "$REPLACE" = "y" ]; then
        echo "Creating a new key pair..."
        rm -f "$KEY_PATH" "$KEY_PATH.pub"
        ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -N ""
    else
        echo "Using the existing key pair..."
    fi
else
    # Generate a new SSH key pair
    echo "Generating a new key pair..."
    ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -N ""
fi

# Create the SSH config file if it doesn't exist
mkdir -p ~/.ssh
touch ~/.ssh/config

# Backup the existing config
cp ~/.ssh/config ~/.ssh/config.backup

# Clean up and re-add configuration
# First, remove any existing configuration block for the alias
awk -v host="$SSH_ALIAS" '
    $0 ~ "^Host[[:space:]]+" host "$" {skip=1; next}
    skip && /^[[:space:]]*$/ {skip=0}
    skip && /^[^[:space:]]/ {skip=0; print; next}
    skip {next}
    {print}
' ~/.ssh/config > ~/.ssh/config.tmp

# Add the new configuration
cat >> ~/.ssh/config.tmp << EOF

Host $SSH_ALIAS
    HostName $SERVER_IP
    User $SERVER_USER
    IdentityFile $KEY_PATH
    IdentitiesOnly yes

EOF

# Replace the original config file
mv ~/.ssh/config.tmp ~/.ssh/config
chmod 600 ~/.ssh/config

# Upload the public key to the server
echo "Uploading the SSH public key to the server..."
echo "Please enter the server password when prompted:"
ssh-copy-id -i "$KEY_PATH.pub" "$SERVER_USER@$SERVER_IP"

# Test the SSH connection
echo "Testing the SSH connection..."
ssh -o StrictHostKeyChecking=no $SSH_ALIAS exit
if [ $? -eq 0 ]; then
    echo "✅ SSH configuration successful! You can now connect to the server using the following command:"
    echo "ssh $SSH_ALIAS"
else
    echo "❌ There may be an issue with the SSH configuration. Please check the settings."
    echo "You can follow these manual steps:"
    echo "1. On the server, ensure that the ~/.ssh directory exists:"
    echo "   mkdir -p ~/.ssh"
    echo "   chmod 700 ~/.ssh"
    echo "2. Create or edit the authorized_keys file:"
    echo "   vim ~/.ssh/authorized_keys"
    echo "3. Paste the following public key content into the file:"
    cat "$KEY_PATH.pub"
    echo "4. Set the correct permissions:"
    echo "   chmod 600 ~/.ssh/authorized_keys"
fi