Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created July 15, 2025 04:02
Show Gist options
  • Save isurfer21/202ea5ed4b149e4b8d08ea4847b13a40 to your computer and use it in GitHub Desktop.
Save isurfer21/202ea5ed4b149e4b8d08ea4847b13a40 to your computer and use it in GitHub Desktop.
Here's a simple Bash script that generates an SSH key pair (if one doesn't already exist) and displays the public key so you can copy it to a remote system for SSH access:
#!/bin/bash
# Set the key file path
KEY_PATH="$HOME/.ssh/id_rsa"
# Check if the SSH key already exists
if [ -f "$KEY_PATH.pub" ]; then
echo "SSH key already exists at $KEY_PATH.pub"
else
echo "Generating a new SSH key..."
ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -N ""
echo "SSH key generated."
fi
# Display the public key
echo "Your public SSH key is:"
cat "$KEY_PATH.pub"
# Optional: Copy to remote system (uncomment and edit the line below)
# ssh-copy-id user@remote_host
@isurfer21
Copy link
Author

Instructions:

  1. Save this script to a file, e.g., generate_ssh_key.sh.
  2. Make it executable: chmod +x generate_ssh_key.sh.
  3. Run it: ./generate_ssh_key.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment