Bash

Generate SSH Keypair Non-Interactively

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
ssh-keygen normally prompts. Pass -N "" for empty passphrase, -f for the path, -t ed25519 for modern key type. Idempotent — skip if a key already exists.
Bash
Raw
KEY="$HOME/.ssh/deploy_key"

# Generate only if it doesn't exist
if [[ ! -f "$KEY" ]]; then
    ssh-keygen -t ed25519 -f "$KEY" -N "" -C "deploy@$(hostname)"
fi

# Show the public key (for pasting into GitHub deploy keys, etc.)
cat "$KEY.pub"

# Get the fingerprint
ssh-keygen -lf "$KEY.pub"
# 256 SHA256:abc...  deploy@host (ED25519)

# Add to ssh-agent (so you don't have to specify -i every time)
eval "$(ssh-agent -s)"
ssh-add "$KEY"

# Configure git to use this specific key for one host
cat >> ~/.ssh/config <<EOF
Host github-deploy
    HostName github.com
    User git
    IdentityFile $KEY
    IdentitiesOnly yes
EOF
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.