6 SSH Key Hygiene Checks That Close Cloud Backdoors

SSH key hygiene decides whether an old key on a forgotten bastion host becomes a permanent backdoor: anyone who obtains an EC2 private key can connect to every instance configured with that key pair, and the provider keeps no recoverable copy. This guide gives you six concrete checks — inventory, authorized_keys sweep, format review, repository scanning, agent policy and keyless migration — that you can run in a single sprint to shrink standing SSH access across AWS, Azure and GitHub.
Unlike passwords, SSH keys do not age out on their own. A public key installed on a golden image keeps working for as long as the image is deployed, regardless of who originally requested the key, and a private key committed to a repository stays valid until someone revokes the public key everywhere it was installed. That asymmetry is why key sprawl is a hygiene problem rather than an exotic attack technique: most intrusions that start with a leaked key begin with a credential nobody remembered owning.
Why SSH Keys Become Backdoors
A private key is a bearer credential with no central revocation point in most stacks. AWS states plainly that it does not keep a copy of your private key and cannot recover it if you lose it; the flip side is that there is no cloud-side switch that disables a compromised public key across your estate. Revocation must happen host by host, in every authorized_keys file, every image and every deployment pipeline that ever carried the key.
The blast radius compounds quietly. Keys accumulate on departed engineers’ laptops, inside CI runners, in Terraform modules that inject keys at boot and on instances that outlive the teams that built them. Because the console listing of key pair names and the set of keys actually installed on hosts are two different things, a key can vanish from the dashboard while still granting access in production. Treat every key you cannot attribute to a living owner and an active workload as compromised by default.
Inventory Every Key Pair
Start by building a single inventory that spans accounts, regions and platforms, then reconcile it against reality on the hosts:
- Export key pair names from every AWS region with describe-key-pairs and match each one to the instances and launch templates that reference it.
- List SSH public keys stored in Azure, both standalone key resources and keys embedded in VM definitions, and record the owner of each.
- Collect the contents of authorized_keys from golden images and long-lived VMs; diff them against the inventory to find keys nobody declared.
- Search CI secret stores, runner disks and Git history for PEM and OpenSSH key material, including deploy keys with write access to repositories.
Format review belongs in the same pass. Azure documents support for SSH-2 RSA keys with a minimum length of 2048 bits alongside ED25519, and unsupported or deprecated formats such as ECDSA should be regenerated rather than grandfathered into the next bake of your images. Apply the same layered discipline you use for workload controls in the Kubernetes container hardening layers: enumerate what exists before deciding what to enforce.
Rotate and Revoke Stale Keys
Rotation is a two-sided operation. Generate the replacement key, install its public key through your configuration management, confirm the new access path works end to end, and only then remove the old public key line from every host. Deleting a key pair from a console listing is bookkeeping, not revocation — verify removal on the instances themselves before closing the ticket, and re-check any images that still bake the old key at build time.
Repository leaks follow a stricter rule. GitHub secret scanning walks the entire Git history of a repository for hardcoded credentials, and the documented remediation is direct: rotate the affected credential immediately when a leak is detected, because rewriting history is slow and often unnecessary once the credential is dead. Treat an exposed private key as compromised from the moment it was pushed, not from the moment you noticed the alert.
Restrict Agents and Forwarding
Agent forwarding turns every host you hop through into a holder of your authentication capability, so a compromise of an intermediate server becomes a compromise of everything the agent can reach. Disable forwarding by default, use ProxyJump with per-host keys for bastion patterns, and require passphrases on every private key that leaves a secrets manager. On the server side, restrict which key maps to which account so a single stolen key does not unlock every role.
Remember that key-based VMs remove one control while adding another: Linux VMs created with SSH keys have password authentication disabled by default in Azure, so key hygiene replaces password policy entirely on those hosts. Shrinking network reachability in parallel — for example, auditing Kubernetes NetworkPolicies alongside security group reviews — limits what an unrevoked key can touch while rotation completes.
A Keyless Default With Session Manager
The strongest hygiene control is not rotating keys but eliminating them where the platform allows it. AWS Systems Manager Session Manager provides an interactive browser-based shell or CLI session without a key pair, removing the standing credential from routine admin paths entirely. Migrate day-to-day access first, keep a small documented set of break-glass keys for tooling that genuinely requires SSH, and revisit the exception list each quarter. Platforms differ in what they support, so scope the migration per cloud rather than assuming parity between providers.
Six-Point SSH Audit Checklist
| # | Check | Pass criterion |
|---|---|---|
| 1 | Regional key pair inventory | Every named key maps to an owner and an active workload; orphans flagged for revocation |
| 2 | authorized_keys sweep | Golden images and long-lived VMs contain only keys present on the inventory |
| 3 | Format and strength review | No keys weaker than the accepted RSA minimum; deprecated formats replaced or scheduled |
| 4 | Repository and CI scan | No private key material in Git history, secret stores or runner disks without an owner |
| 5 | Agent and forwarding policy | Forwarding disabled by default; bastion access uses ProxyJump with per-host keys |
| 6 | Keyless coverage | Session-based access is the default where supported, with a documented exception list |