A bash script to streamline the generation of multiple SSH key pairs and automatically update your SSH config file.
- Generates multiple SSH key pairs using Ed25519 algorithm
- Automatically updates
~/.ssh/configwith host configuration blocks - Supports optional passphrases for all generated keys
- Adds keys to SSH agent automatically (if running)
- Handles existing keys gracefully
- User-friendly status messages and warnings
- Bash shell
ssh-keygenutility (usually included with OpenSSH)ssh-agentrunning (optional, but recommended for automatic key loading)
- Download the script or create a file with the script contents
- Make it executable:
chmod +x script_name.sh
./script_name.sh -n <key_name_1> [key_name_2] [...] [-p]-n(MANDATORY): Signals the start of the list of key names<key_name>: One or more names for the keys (e.g.,github,server_a)-p(OPTIONAL): Prompts for a passphrase to protect all generated keys
Generate two keys without passphrases:
./script_name.sh -n github_personal prod_eu_accessGenerate a single key with a passphrase:
./script_name.sh -n test_server_key -pGenerate multiple keys with passphrases:
./script_name.sh -n github gitlab server_a -p- Parses arguments to collect key names and options
- Prompts for passphrase (if
-pflag is used) with confirmation - Creates
~/.sshdirectory if it doesn't exist - Initializes SSH config file if missing
- Generates key pairs using Ed25519 algorithm
- Adds keys to SSH agent automatically (if agent is running)
- Updates SSH config with new host configuration blocks
The script automatically appends configuration blocks to your ~/.ssh/config file in the following format:
Host <key_name>
HostName CHANGEME
User CHANGEME
IdentityFile ~/.ssh/<key_name>
PreferredAuthentications publickey
IdentitiesOnly yes
AddKeysToAgent yes
Important: You must manually replace CHANGEME values with your actual server hostname and username.
Generated keys are created in the current directory:
<key_name>(private key)<key_name>.pub(public key)
It's recommended to move these to ~/.ssh/ after generation.
After running the script, verify your keys are loaded in the SSH agent:
ssh-add -lIf SSH agent is not running, you'll see a warning. To start it, run:
eval $(ssh-agent -s).sshdirectory:700(created automatically).ssh/configfile:600(created automatically)- Private keys:
600(created by ssh-keygen) - Public keys:
644(created by ssh-keygen)
- Keys not added to agent: Make sure SSH agent is running or check the passphrase
- Config already exists: If a
Hostentry already exists, the script skips the update to avoid duplicates - Permission denied: Ensure the script is executable (
chmod +x) and you have write permissions in the output directory - Script won't run: Verify bash is available and the file isn't corrupted (check for proper line endings)