Created
July 15, 2025 04:02
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions:
generate_ssh_key.sh
.chmod +x generate_ssh_key.sh
../generate_ssh_key.sh
.