Prompt
How do I store SSH keys for automation securely?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
For automation, the main goal is to reduce key exposure while keeping the process non-interactive and auditable. Good options, from strongest to weakest in most cases:
Best practices
1) Use a dedicated key per automation job
- Create a separate SSH key pair for each service, environment, or pipeline.
- Don’t reuse personal keys.
- Limit the account the key can access to the minimum required.
2) Prefer short-lived credentials over static private keys
If possible, avoid long-lived SSH private keys on disk altogether:
- Use ephemeral SSH certificates signed by a central CA.
- Use a secrets manager that can issue keys just-in-time.
- Rotate credentials automatically.
Examples:
- OpenSSH certificates
- Vault SSH secrets engine
- Cloud-native bastion / identity solutions
3) Store secrets in a secrets manager, not in code or plain files
Use:
- HashiCorp Vault
- AWS Secrets Manager / Systems Manager Parameter Store
- Azure Key Vault
- Google Secret Manager
- Kubernetes Secrets + encryption at rest, if you’re in k8s
Then inject the key at runtime into:
- a locked-down temp file
- an ssh-agent session
- or a container-mounted secret
4) Use ssh-agent when appropriate
Instead of writing the key to disk permanently:
- Load the key into
ssh-agent - Keep the key only in memory
- Set a short lifetime if possible
Example:
eval "$(ssh-agent -s)"
ssh-add -t 1h /path/to/key
5) If you must store a private key on disk, lock it down hard
- File permissions:
chmod 600 - Owned by the automation user only
- Store in an encrypted disk or protected directory
- Restrict backup access
- Never commit to git
- Never put it in environment variables if logs/process inspection are concerns
6) Use passphrases only when automation can unlock them safely
A passphrase helps if the file is stolen, but automation needs a secure way to supply it:
- Prefer agent-backed unlock from a secrets manager
- Avoid hardcoding passphrases in scripts
- Avoid plaintext env vars unless threat model is low
7) Limit server-side impact
On the target host:
- Restrict the key in
authorized_keyswith options like:from=command=no-ptyno-port-forwardingno-agent-forwardingno-X11-forwarding
- Use a dedicated low-privilege account
- Consider chroot/jail/container boundaries where possible
Example:
from="10.0.0.0/24",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty ssh-ed25519 AAAAC3Nza...
8) Rotate and revoke
- Rotate keys regularly
- Keep an inventory of which automation uses which key
- Revoke immediately if a host, pipeline, or credential store is compromised
Good storage patterns by environment
CI/CD
- Store keys in the CI system’s secret store
- Inject at job runtime
- Use short-lived certs if available
- Mask secrets in logs
Servers / cron jobs
- Use a local secrets manager or
ssh-agentwith systemd service integration - Restrict filesystem access tightly
Containers / Kubernetes
- Mount secrets at runtime, not baked into images
- Prefer external secret managers + init container or sidecar to fetch short-lived creds
What not to do
- Don’t store private keys in source control
- Don’t email keys
- Don’t bake keys into container images
- Don’t reuse one key across many systems
- Don’t leave unlocked keys on shared machines
- Don’t rely on obscurity; use proper access controls and rotation
A practical secure baseline
If you want a simple, solid default:
- Generate a dedicated ed25519 key for the automation task.
- Store it in a secrets manager.
- Retrieve it at job start.
- Load it into
ssh-agenttemporarily. - Use a restricted server account and authorized_keys restrictions.
- Rotate it on a schedule.
If you want, I can give you:
- a recommended setup for Linux cron/systemd
- a CI/CD example
- or a Vault-based SSH workflow.