A successful SSH login without prior failed attempts might seem harmless, but it is a signal that warrants review. In this article, we walk through step-by-step how to investigate such an access and how to prevent it from happening again, based on a real incident: an SSH key from a decommissioned build server was still authorized in production.
The scenario
A Linux production server logged two successful SSH logins from an external IP overnight. What stood out: there were no prior failed password attempts. This rules out a brute-force attack and indicates that the access occurred using valid credentials.
Step 1: Identify the authentication method
Check the SSH logs and look for successful login lines. Depending on your system, they will be located in one of these places:
- systemd journal (default on recent distributions like Debian 13):
journalctl -u ssh | grep Accepted
On some systems, the service is called sshd; if the command returns nothing, try -u sshd.
- Traditional log files:
/var/log/auth.log(Debian/Ubuntu withrsyslog) or/var/log/secure(RHEL/CentOS).
If you see this:
Accepted publickey
it means authentication succeeded using a public key, not a password. Whoever logged in held the private key corresponding to an authorized key on the server.
Step 2: Get the fingerprint of the used key
The Accepted publickey line includes the fingerprint of the key used to authenticate the session, formatted similarly to this:
Accepted publickey for deploy from <IP> port <PUERTO> ssh2: ED25519 SHA256:xxxxxxxxxxxx
Take note of the fingerprint (SHA256:...), source IP, and timestamp.
Step 3: Compare it against authorized keys
Check the authorized keys file for the user that was logged into:
/home/deploy/.ssh/authorized_keys
To view the fingerprint of each key in the file:
ssh-keygen -lf /home/deploy/.ssh/authorized_keys
Compare each fingerprint against the one retrieved from the log. In our case, there was only one key, and it matched.
Step 4: Check the key comment
The comment at the end of each key usually hints at its origin. Here it was:
deploy@build-prerelease
Look for it in your server inventory and internal records. In our case, that build server had been decommissioned months earlier, but its key was never removed from the production server.
Note: The comment is just arbitrary text and can be edited. Use it as a clue, but always rely on the fingerprint for verification.
Step 5: Determine how long the key has existed
Check the file’s timestamp:
stat /home/deploy/.ssh/authorized_keys
The key had been there since October 2025. This indicates it was not newly planted persistence dropped by an attacker, but an old trust relationship that nobody had audited. Keep in mind that the modification timestamp reflects the last time the file as a whole changed, not necessarily when a specific key was appended.
Step 6: Confirm the origin of the sessions
Review the successful logins within the suspicious timeframe and verify whether both sessions originated from the same IP:
journalctl -u ssh --since "AAAA-MM-DD HH:MM" --until "AAAA-MM-DD HH:MM" | grep -E "Accepted|session opened"
This gives you the source IP, timestamp, and authentication method for each session, while the session opened / session closed lines allow you to estimate duration.
Note on
last: In recent distributions such as Debian 13, thelastcommand is no longer included by default (since util-linux 2.40, it was split into thewtmpdbpackage). You can install it withapt install wtmpdband runwtmpdb last, but it will only log sessions from the moment it is installed onward, making it useless for investigating past incidents. For that, stick to the journal orauth.log.
Step 7: Find what was touched during the sessions
Search for files created or modified within the time window of the sessions:
find /opt /home -type f -newermt "AAAA-MM-DD HH:MM" ! -newermt "AAAA-MM-DD HH:MM"
Adjust the dates and directories to match your specific case.
In this incident, a backup copy of the application’s .env file appeared in /opt/app/. Environment files typically contain database credentials, API keys, and application secrets, so you must assume they were compromised.
Step 8: Contain and remediate
With the evidence gathered, the remediation actions were:
- Disable the affected account (
deploy). - Remove the obsolete key from
authorized_keys. - Rotate all application secrets: database credentials, API keys, and any other secret values in the
.env. - Audit the rest of the infrastructure to find other servers that still trust that same key.
For step 4, searching by fingerprint across each server helps track down traces of the key:
ssh-keygen -lf /home/USUARIO/.ssh/authorized_keys
Repeat this across every user with SSH access and compare against the fingerprint you identified.
How to prevent it
- Maintain an inventory of SSH keys: Document which key belongs to which person, server, or service.
- Include key revocation in offboarding procedures: Whenever you decommission a server, offboard an employee, or retire a service, revoke its keys from every system where they were authorized.
- Audit
authorized_keysperiodically: Verify that every key has an active owner and a valid purpose. - Use scoped keys: In
authorized_keys, you can restrict a key using options likefrom="IP"orcommand="...", limiting the blast radius if the private key leaks. - Consider expiring SSH certificates: Unlike static public keys, SSH certificates expire automatically.
- Monitor unusual logins: Set up alerts for logins originating from new IP addresses, even when authentication succeeds.
Overlooked flaws ripe for exploitation
Not every security compromise starts with malware or a weak password. Sometimes all an attacker needs is a credential your team forgot even existed. Passwords expire, employees leave, and servers are decommissioned, but an SSH key can linger in authorized_keys for years if no one audits it.
Auditing and rotating keys is not glamorous, but it is one of the cheapest and most effective defenses you can maintain.