Linux SSH Authorized Keys Persistence
Adding your public key to a user's authorized_keys file gives passwordless SSH access that survives password changes, reboots, and session termination. It requires write access to the target user's home directory: either directly or through a path traversal, file write vulnerability, or elevated shell.
The technique works for any user account on the system, not just root. Low-privilege user persistence is often overlooked during incident response. Multiple keys in authorized_keys are allowed, so appending does not remove existing entries. Confirm that SSH is listening and reachable before writing the key.
Steps for SSH key persistence:
- Generate an ed25519 key pair on your attack box
- Append the public key to the target's
~/.ssh/authorized_keys - Set correct permissions (
.ssh700,authorized_keys600) - Test the connection before closing your current shell
Check /etc/ssh/sshd_config for a custom port if port 22 is filtered.
Generate a key pair on your attack box
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... attacker@kali
Add key to target user
OpenSSH enforces permissions strictly. If .ssh is not mode 700 or authorized_keys is not mode 600, authentication fails silently. The client sees a password prompt instead of a key error. Always set permissions immediately after writing the key. If you write the file as a different user, the chown step is required. A root-owned authorized_keys in a user's home directory will not authenticate that user. Test the key before closing your current session. Connect from the attack box while the original shell is still open. Run ssh -v if the login fails to see exactly where authentication breaks.
SSH config persistence
Beyond authorized_keys, the sshd configuration can be modified to allow root login or password authentication if currently disabled: making other accounts or methods viable for re-entry. The grep output shows the current effective settings before you change anything. If PermitRootLogin shows prohibit-password, key-based root login already works and you only need to add the key. If PasswordAuthentication is no, enabling it lets you authenticate with any cracked credential. A reload sends SIGHUP to sshd and applies changes without dropping active sessions. A full restart would disconnect existing shells, including your own.
SSH config changes are visible and often monitored. The authorized_keys method is less detectable because it adds a key to an existing file rather than modifying a system configuration file. On systems with AuthorizedKeysCommand configured, the static authorized_keys file may be bypassed: check the sshd config before relying on this technique. If AuthorizedKeysCommand is set, keys are fetched dynamically from an external source such as LDAP or a custom script. Your written key will be ignored entirely. Read the command value in sshd_config to understand where keys are sourced, then target that system instead.