SSH Key Management in Bash
Generate, rotate, and distribute SSH keys with bash scripts
Overview
SSH keys are how most teams authenticate to remote servers, CI runners and Git hosts. Managing them by hand across more than a handful of machines is error-prone. This approach handles generating ed25519 keys, distributing them to servers, rotating compromised keys, and disabling password authentication.
When to Use
- You need to generate SSH keys for new team members
- You want to distribute your public key to a fleet of servers at once
- You need to rotate keys after a security incident
- You’re hardening server security by disabling password auth
- Don’t use long-lived key rotation at all if you run an SSH CA that issues short-lived certificates — signing replaces
authorized_keysdistribution (see the CA variant below)
Solution
Generate an ed25519 key
#!/bin/bash
KEY_NAME="${1:-default}"
KEY_PATH="$HOME/.ssh/${KEY_NAME}"
if [ -f "$KEY_PATH" ]; then
echo "Key already exists: $KEY_PATH"
exit 1
fi
ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)-${KEY_NAME}" -f "$KEY_PATH" -N ""
echo "Generated: $KEY_PATH"
echo "Public key: ${KEY_PATH}.pub"
Distribute public key to multiple servers
#!/bin/bash
PUBLIC_KEY="$HOME/.ssh/id_ed25519.pub"
SERVERS=(
"user@server1.example.com"
"user@server2.example.com"
"user@server3.example.com"
)
for server in "${SERVERS[@]}"; do
echo "Distributing key to $server..."
ssh-copy-id -i "$PUBLIC_KEY" "$server"
done
echo "Key distributed to ${#SERVERS[@]} servers"
Distribute key without ssh-copy-id
#!/bin/bash
PUBLIC_KEY=$(cat "$HOME/.ssh/id_ed25519.pub")
SERVERS_FILE="servers.txt"
while IFS= read -r server; do
[ -z "$server" ] && continue
echo "Adding key to $server..."
ssh "$server" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$PUBLIC_KEY' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
done < "$SERVERS_FILE"
Rotate compromised keys
#!/bin/bash
OLD_KEY="$1"
SERVERS_FILE="servers.txt"
if [ -z "$OLD_KEY" ]; then
echo "Usage: $0 <old-public-key-string>"
exit 1
fi
# Remove old key from all servers
while IFS= read -r server; do
[ -z "$server" ] && continue
echo "Removing old key from $server..."
ssh "$server" "sed -i '/${OLD_KEY}/d' ~/.ssh/authorized_keys"
done < "$SERVERS_FILE"
# Generate new key
ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)-rotated" -f "$HOME/.ssh/id_ed25519_rotated" -N ""
echo "Old key removed. New key: $HOME/.ssh/id_ed25519_rotated"
echo "Distribute the new key with the distribute script."
Disable password authentication
#!/bin/bash
SSHD_CONFIG="/etc/ssh/sshd_config"
# Backup original config
cp "$SSHD_CONFIG" "${SSHD_CONFIG}.bak"
# Disable password auth
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' "$SSHD_CONFIG"
sed -i 's/^#*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/' "$SSHD_CONFIG"
sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' "$SSHD_CONFIG"
# Restart SSH
systemctl restart sshd
echo "Password authentication disabled. Key-based auth only."
Audit SSH keys on a server
#!/bin/bash
echo "=== SSH Key Audit ==="
echo "Date: $(date)"
echo ""
for user_home in /home/* /root; do
user=$(basename "$user_home")
auth_file="${user_home}/.ssh/authorized_keys"
if [ -f "$auth_file" ]; then
key_count=$(wc -l < "$auth_file")
echo "User: $user ($key_count keys)"
while IFS= read -r key; do
[ -z "$key" ] && continue
key_type=$(echo "$key" | awk '{print $1}')
key_comment=$(echo "$key" | awk '{print $3}')
key_fingerprint=$(echo "$key" | ssh-keygen -lf - 2>/dev/null | awk '{print $1}')
echo " - $key_type | $key_fingerprint | $key_comment"
done < "$auth_file"
echo ""
fi
done
List all SSH keys on local machine
#!/bin/bash
echo "=== Local SSH Keys ==="
for key in ~/.ssh/id_* ~/.ssh/*.pub; do
[ -f "$key" ] || continue
if [[ "$key" == *.pub ]]; then
fingerprint=$(ssh-keygen -lf "$key" 2>/dev/null)
echo "Public: $key"
echo " $fingerprint"
fi
done
Explanation
Ed25519 keys are smaller, faster, and more secure than RSA. A 256-bit ed25519 key matches the security of a 3072-bit RSA key. Always prefer ed25519 for new keys.
ssh-copy-id copies your public key into the remote ~/.ssh/authorized_keys and fixes directory permissions along the way (700 for ~/.ssh, 600 for authorized_keys). On minimal systems without ssh-copy-id — some containers and older macOS images — a plain ssh command does the same job.
Key rotation removes the old public key from authorized_keys files across all servers. The sed -i call drops every line containing the old key string — blunt, but effective since key lines are unique. After removal, generate a new key and distribute it.
Disabling password authentication is the single most effective SSH hardening step. That single change removes the entire class of brute-force password attacks.
Variants
| Approach | Key Type | Use When |
|---|---|---|
| ed25519 | Ed25519 | New keys, modern servers |
| RSA 4096 | RSA | Legacy servers without ed25519 support |
| ECDSA | ECDSA | Specific compliance requirements |
| Hardware key (FIDO2) | Ed25519-SK | High-security environments with YubiKey |
Guidelines
- Use ed25519 for all new keys — signature verification is measurably faster than RSA and there’s no modulus size to argue about.
- Private keys that live on a laptop need a passphrase — a stolen machine shouldn’t hand over your fleet. Load it once into
ssh-agentper session instead of typing it on every connection. - Use
~/.ssh/configto keep per-host keys and aliases in one place. - Rotate keys after team member departures or suspected compromises.
- Audit
authorized_keysfiles quarterly. Remove keys belonging to people who left or rotated off the project.
Common Mistakes
- Using RSA keys shorter than 3072 bits. RSA 2048 still works, but current guidance puts its long-term horizon around 2030 — don’t start anything new with it.
- Leaving private keys without a passphrase. Anyone with file access can use the key.
- Not removing old keys from
authorized_keysafter rotation. Any copy of the old public key left behind keeps working as if nothing happened. - Disabling password auth before verifying key-based login works. You lock yourself out.
- Sharing one private key across several people or jobs — when it leaks you can’t tell who used it, and revoking it breaks everyone at once.
Advanced Solutions
Companion files: every script below is available as a runnable file in the bash-ssh-key-manager companion repo, including the verified fleet rotation, the CSV audit, the CI/CD agent setup, and the Python key manager.
SSH config with jump host (bastion)
Connect to internal servers through a bastion host without exposing them to the internet. The ProxyJump directive hops through the bastion automatically — no manual ssh -A chains:
# Bastion host (reachable from internet)
Host bastion
HostName bastion.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_bastion
Port 2222
# Internal servers (only reachable via bastion)
Host *.internal.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_internal
ProxyJump bastion
# Disable agent forwarding to bastion for security
ForwardAgent no
# Database server (two-hop: bastion -> jump -> db)
Host db-prod.internal.example.com
User dbadmin
IdentityFile ~/.ssh/id_ed25519_db
ProxyJump jump-vm.internal.example.com
FIDO2 hardware-backed SSH keys (YubiKey)
Generate SSH keys that require physical hardware presence. The private key never leaves the YubiKey:
#!/bin/bash
# Generate a FIDO2-backed ed25519 key
# Requires OpenSSH 8.2+ and a FIDO2-compatible device (YubiKey 5 Series)
ssh-keygen -t ed25519-sk -C "$(whoami)@$(hostname)-fido2" -f "$HOME/.ssh/id_ed25519_sk"
# With a passphrase and requiring touch for every use
ssh-keygen -t ed25519-sk -O resident -O verify-required \
-C "$(whoami)@$(hostname)-fido2-strict" \
-f "$HOME/.ssh/id_ed25519_sk_strict"
echo "FIDO2 key generated. Touch YubiKey when prompted during use."
# Resident key can be recovered on a new machine
# ssh-keygen -K # downloads resident keys from FIDO2 device
SSH Certificate Authority (CA) for key signing
Instead of distributing public keys to every server, set up an SSH CA. Servers trust the CA, and users get signed certificates with expiration:
#!/bin/bash
# === On the CA host ===
# Generate CA key pair (do this once, keep the private key secure)
ssh-keygen -t ed25519 -f /etc/ssh/ca_key -N "" -C "SSH CA"
# === On each server (trust the CA) ===
# Add CA public key to trusted user CA
echo "TrustedUserCAKeys /etc/ssh/ca_key.pub" >> /etc/ssh/sshd_config
systemctl restart sshd
# === Sign a user's public key (on CA host) ===
# Sign with validity period and principal restrictions
ssh-keygen -s /etc/ssh/ca_key \
-I "user-$(date +%Y%m%d)" \
-n deploy,admin \
-V +1d \
~/.ssh/id_ed25519.pub
# This generates ~/.ssh/id_ed25519-cert.pub
# The user connects with their key + certificate:
# ssh -i ~/.ssh/id_ed25519 user@server
# The certificate is valid for 1 day and only for principals 'deploy' and 'admin'
Bulk key rotation across server fleet
Rotate keys across hundreds of servers using a parallel SSH tool and a rotation manifest:
#!/bin/bash
# rotation-manifest.txt format: server old_key_fingerprint new_key_path
MANIFEST="rotation-manifest.txt"
KEYS_DIR="$HOME/.ssh"
while IFS=$'\t' read -r server old_fp new_key; do
[ -z "$server" ] && continue
echo "Rotating key on $server..."
# Remove old key by fingerprint
ssh "$server" "ssh-keygen -R -f ~/.ssh/authorized_keys -F '$old_fp'" 2>/dev/null
# Add new key
NEW_PUB=$(cat "${KEYS_DIR}/${new_key}.pub")
ssh "$server" "echo '$NEW_PUB' >> ~/.ssh/authorized_keys"
echo " Done: $server"
done < "$MANIFEST"
echo "Rotation complete for $(wc -l < "$MANIFEST") servers"
Verified fleet-wide key rotation
A manifest-based rotation removes the old key before confirming the new one works — if an upload fails halfway, you lose access. This variant adds the new key to every server first, verifies login with it, and only then deletes the old key:
#!/usr/bin/env bash
set -euo pipefail
# servers.txt: one user@host per line
OLD_KEY="$HOME/.ssh/id_ed25519"
NEW_KEY="$HOME/.ssh/id_ed25519_new"
SERVERS_FILE="${1:-servers.txt}"
ssh-keygen -t ed25519 -a 100 -f "$NEW_KEY" -N "" -C "rotated-$(date +%Y-%m-%d)"
# 1. Push the new public key everywhere, authenticating with the old key
while IFS= read -r server; do
[[ -z "$server" || "$server" == \#* ]] && continue
ssh -i "$OLD_KEY" -o BatchMode=yes -o ConnectTimeout=10 "$server" \
"mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys" \
< "${NEW_KEY}.pub" \
&& echo "OK: $server" || echo "FAIL: $server"
done < "$SERVERS_FILE"
# 2. Verify the new key on every server
ALL_OK=true
while IFS= read -r server; do
[[ -z "$server" || "$server" == \#* ]] && continue
ssh -i "$NEW_KEY" -o BatchMode=yes -o ConnectTimeout=10 "$server" true 2>/dev/null \
|| { echo "FAIL: $server rejects new key"; ALL_OK=false; }
done < "$SERVERS_FILE"
# 3. Only when all servers accept the new key, remove the old one.
# authorized_keys stores the public key blob, not its fingerprint,
# so match the whole line from the .pub file.
OLD_PUB=$(cat "${OLD_KEY}.pub")
if $ALL_OK; then
while IFS= read -r server; do
[[ -z "$server" || "$server" == \#* ]] && continue
ssh -i "$NEW_KEY" -o BatchMode=yes "$server" \
"grep -vxF '$OLD_PUB' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp \
&& mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys"
done < "$SERVERS_FILE"
mv "$NEW_KEY" "$OLD_KEY"
mv "${NEW_KEY}.pub" "${OLD_KEY}.pub"
echo "Rotation complete."
else
echo "ABORT: keeping old key; rerun after fixing the failed servers."
rm -f "$NEW_KEY" "${NEW_KEY}.pub"
exit 1
fi
The abort path matters: on any verification failure the old key stays on the servers and the new key is deleted locally, so the script is safe to rerun.
Fleet-wide authorized_keys audit to CSV
The single-server audit above reads one machine. This variant loops over a fleet and writes a CSV report with a bits column, which makes weak-key flagging explicit instead of parsing it out of the fingerprint field:
#!/usr/bin/env bash
set -euo pipefail
# Usage: ./audit-keys.sh server1 server2 server3
REPORT_FILE="ssh-key-audit-$(date +%Y%m%d).csv"
echo "server,user,key_type,bits,fingerprint,comment" > "$REPORT_FILE"
for server in "$@"; do
ssh -o BatchMode=yes -o ConnectTimeout=10 "$server" \
'for user_dir in /home/* /root; do
auth_keys="$user_dir/.ssh/authorized_keys"
[ -f "$auth_keys" ] || continue
while IFS= read -r line; do
[ -z "$line" ] || [[ "$line" == \#* ]] && continue
info=$(echo "$line" | ssh-keygen -lf - 2>/dev/null) || continue
bits=$(echo "$info" | awk "{print \$1}")
fp=$(echo "$info" | awk "{print \$2}")
ktype=$(echo "$line" | awk "{print \$1}")
comment=$(echo "$line" | awk "{print \$3}")
echo "'"$server"',$(basename "$user_dir"),$ktype,$bits,$fp,$comment"
done < "$auth_keys"
done' >> "$REPORT_FILE" 2>/dev/null \
|| echo "WARN: could not audit $server" >&2
done
# ssh-keygen -lf prints "<bits> <SHA256:fp> <comment> (<TYPE>)" — the CSV above
# carries bits in column 4, so flagging weak keys is a plain numeric check:
awk -F',' 'NR>1 && ($3=="ssh-dss" || $3=="ecdsa" || ($3=="ssh-rsa" && $4+0 < 4096)) \
{print "WEAK: " $0}' "$REPORT_FILE"
ssh-agent automation for CI/CD
CI runners need keys loaded non-interactively. Store the private key as a base64-encoded secret and feed it to a disposable agent — see the CI/CD guide for the GitHub Actions side of this:
#!/usr/bin/env bash
set -euo pipefail
# Start an agent if none is running
ssh-add -l > /dev/null 2>&1 || eval "$(ssh-agent -s)"
# CI: key arrives base64-encoded in a secret env var
if [[ -n "${SSH_KEY:-}" ]]; then
echo "$SSH_KEY" | base64 -d | ssh-add -
else
for key in "$HOME"/.ssh/id_ed25519*; do
[[ -f "$key" && "$key" != *.pub ]] && ssh-add "$key" 2>/dev/null || true
done
fi
# Pin host keys instead of disabling verification entirely.
# accept-new trusts the first scan but still catches key changes later.
mkdir -p "$HOME/.ssh"
for host in ${SSH_HOSTS:-github.com gitlab.com}; do
ssh-keyscan -H "$host" >> "$HOME/.ssh/known_hosts" 2>/dev/null
done
export GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=accept-new"
SSH key management in Python
For inventory and audit tasks, a small Python wrapper around ssh-keygen is easier to extend than bash. Note the -lf output format: <bits> <SHA256:fingerprint> <comment> (<TYPE>), so the key type is the last field and the comment is everything in between:
import os
import subprocess
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Optional
@dataclass
class SSHKeyInfo:
key_type: str
bits: int
fingerprint: str
comment: str
file_path: str
class SSHKeyManager:
"""Generate, rotate and audit SSH keys in ~/.ssh."""
def __init__(self, key_dir: str = "~/.ssh"):
self.key_dir = Path(key_dir).expanduser()
self.key_dir.mkdir(parents=True, exist_ok=True)
self.key_dir.chmod(0o700)
def generate_key(self, name: str, key_type: str = "ed25519",
passphrase: str = "", comment: Optional[str] = None) -> Path:
key_path = self.key_dir / name
if key_path.exists():
raise FileExistsError(f"Key already exists: {key_path}")
comment = comment or (
f"{os.getenv('USER', 'user')}@{os.uname().nodename}"
f"-{datetime.now():%Y-%m-%d}"
)
cmd = ["ssh-keygen", "-t", key_type, "-f", str(key_path),
"-N", passphrase, "-C", comment]
if key_type == "ed25519":
cmd += ["-a", "100"]
subprocess.run(cmd, check=True, capture_output=True)
key_path.chmod(0o600)
key_path.with_suffix(".pub").chmod(0o644)
return key_path
def get_key_info(self, pub_key_path: Path) -> SSHKeyInfo:
out = subprocess.run(
["ssh-keygen", "-lf", str(pub_key_path)],
capture_output=True, text=True, check=True,
).stdout.strip().split()
return SSHKeyInfo(
key_type=out[-1].strip("()"), # (ED25519) -> ED25519
bits=int(out[0]),
fingerprint=out[1],
comment=" ".join(out[2:-1]),
file_path=str(pub_key_path),
)
def list_all_keys(self) -> list[SSHKeyInfo]:
keys = []
for pub_file in self.key_dir.glob("*.pub"):
try:
keys.append(self.get_key_info(pub_file))
except subprocess.CalledProcessError:
continue
return keys
def audit_weak_keys(self) -> list[SSHKeyInfo]:
weak = []
for key in self.list_all_keys():
if key.key_type in ("DSA", "ECDSA"):
weak.append(key)
elif key.key_type == "RSA" and key.bits < 4096:
weak.append(key)
return weak
manager = SSHKeyManager()
for k in manager.list_all_keys():
print(f"{k.file_path}: {k.key_type} {k.bits} {k.fingerprint} ({k.comment})")
for k in manager.audit_weak_keys():
print(f"WEAK: {k.file_path} ({k.key_type} {k.bits})")
Two traps the code above avoids: ssh-keygen -lf reports the type as RSA/ED25519, not the ssh-rsa/ssh-ed25519 names used inside authorized_keys; and a multi-word comment shifts the type field, so it must be read from the end of the line, not position 4.
Further Reading
- ssh-keygen(1) man page — every flag used here, including
-aKDF rounds and the-lfoutput format. - sshd_config(5) man page —
PasswordAuthentication,TrustedUserCAKeysand the rest of the hardening knobs. - OpenSSH release notes — FIDO2-backed
ed25519-skkeys landed in OpenSSH 8.2.
Frequently Asked Questions
How do I use a specific key for a specific server?
Use ~/.ssh/config:
Host server1.example.com
IdentityFile ~/.ssh/id_ed25519_server1
User deploy
Host *.internal
IdentityFile ~/.ssh/id_ed25519_internal
User admin
How do I set up SSH agent forwarding safely?
Add the key to the agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Then connect with -A to forward the agent. Only use agent forwarding with trusted servers. A compromised server can use your forwarded agent to connect to other servers as you.
How do I enforce key-based auth without locking myself out?
Test key login in a second terminal before disabling passwords:
# Terminal 1: keep this open
ssh user@server
# Terminal 2: test key login
ssh -o PasswordAuthentication=no user@server
# If that works, disable passwords in Terminal 1
What is the difference between authorized_keys and known_hosts?
authorized_keys lists public keys that can log into this machine. known_hosts lists servers this machine has connected to. The two files point in opposite directions: the first decides who gets in, the second records which hosts you already trust.
Related Resources
Bash Scripting for DevOps Automation and System Tasks
How to write reliable Bash scripts for automating deployments, system monitoring, log rotation, and routine maintenance tasks
DocSSL Certificate Management Template
A template for tracking TLS/SSL certificate inventory, renewals, deployments, and expiration risks across domains and services.
DocAPI Security Review Template (OWASP Checklist)
A fillable checklist template for reviewing API authentication, authorization, rate limiting, and OWASP API Security Top 10 coverage.
GuideAPI Security Checklist — Authentication to Encryption
A thorough security checklist for APIs: authentication, authorization, input validation, rate limiting, encryption, logging, and deployment hardening.
GuideSecurity Best Practices Guide
A thorough guide to application security: authentication, authorization, input validation, secrets management, and common vulnerability prevention.
RecipeConfigure iptables Firewall Rules with Bash
Set up a hardened iptables firewall with bash: SSH-safe rules, NAT port forwarding, IP blocking and reboot-proof persistence.