Revoke SSH user keys and verify both new logins and existing sessions

Revoke SSH user keys and verify both new logins and existing sessions

Revoke SSH user keys and verify both new logins and existing sessions

When someone leaves or a deployment private key is compromised, deleting a public key from the server does not complete the access removal process. An authenticated SSH connection can still run commands. If the client uses connection multiplexing, even a terminal opened afterward may not need to authenticate again. For multi-store deployments, file transfers, and automated tasks on dedicated servers, treat “reject new authentication” and “terminate existing connections” as separate acceptance criteria.

1. Define the revocation scope and establish recovery access

This guide uses OpenSSH on Ubuntu Server 24.04 to test revoking a regular user's public key. It does not cover rotating server host keys. Before starting, prepare a dedicated test account, the public key to revoke, and a separate recovery key whose login access and required administrative privileges have already been verified.

After paying for an Evpn dedicated server, check the console for connection credentials. If multiple addresses are listed, use the first address for SSH. The following procedure does not change any addresses or connection parameters, nor does it assume a particular username or port.

Keep a recovery connection open, then verify the recovery key from another terminal. A single administrative session that has not yet disconnected is not proof that recovery access works. Record the account, source address, public key fingerprint, files to be changed, and intended scope of access removal. Do not copy private keys to the server.

Public key revocation only affects subsequent authentication. If a private key has been compromised, also investigate newly added keys, accounts, and persistence tasks. One rejected login does not mean incident response is complete.

2. Check public key sources, then build a KRL

Start by inspecting the server's effective configuration, rather than reading just one configuration fragment. Set the variables to the actual account, client source address, and hostname. If other Match conditions apply, supply the corresponding connection parameters as well.

sudo /usr/sbin/sshd -T \
  -C "user=$TARGET_USER,addr=$CLIENT_ADDR,host=$CLIENT_HOST" |
  grep -E '^(pubkeyauthentication|authorizedkeysfile|authorizedkeyscommand|trustedusercakeys|revokedkeys) '
ssh-keygen -lf retired-key.pub

Check AuthorizedKeysFile, AuthorizedKeysCommand, and the sources of certificate trust. Deleting a public key from one file does not prevent an external command from returning the same key again. A shared key also cannot distinguish between its holders, so identify the affected tasks before revoking it.

A KRL is an OpenSSH key revocation list. The following commands are only for a test environment with no revocation list currently configured. retired-key.pub must be the actual test public key whose fingerprint you have checked:

umask 077
ssh-keygen -k -f revoked-keys.krl retired-key.pub
ssh-keygen -Q -f revoked-keys.krl retired-key.pub
sudo install -o root -g root -m 0644 \
  revoked-keys.krl /etc/ssh/revoked-keys.krl

The query should report that the key is revoked and return a nonzero exit status. Do not mistake this expected status for a failure to generate the list. Query the recovery public key separately to confirm that it is not revoked.

If a KRL already exists, back it up and copy it to a working file, then update the copy with ssh-keygen -k -u -f. Do not overwrite previous revocations with a new list containing only the key being revoked now. Only after validation should you atomically replace the live file within the same filesystem.

3. Validate the configuration and reject fresh authentication

Confirm that the main configuration includes the configuration fragment directory and that no earlier setting for the same directive takes precedence. Then create /etc/ssh/sshd_config.d/60-key-revocation.conf:

RevokedKeys /etc/ssh/revoked-keys.krl

Both configuration order and Match scope affect the result, so check the effective value again. An unreadable KRL can cause all public key authentication to be rejected; the path and permissions are therefore part of the acceptance checks too.

sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T | grep '^revokedkeys '
sudo systemctl reload ssh

If the syntax check fails, stop and do not reload. Once it passes, establish an independent connection from the client with multiplexing explicitly disabled. Set the variables to the actual private key path, port, account, and primary address. This example assumes a direct connection and does not read jump host settings from the client configuration.

ssh -F /dev/null \
  -o ControlMaster=no -o ControlPath=none \
  -o IdentitiesOnly=yes -o BatchMode=yes \
  -o PreferredAuthentications=publickey \
  -o PasswordAuthentication=no \
  -o KbdInteractiveAuthentication=no \
  -i "$RETIRED_KEY" -p "$SSH_PORT" \
  "$TARGET_USER@$SERVER_ADDR" true

Authentication with the revoked key should fail. Repeat the same test with the recovery key; authentication should succeed. Both results are required. If both fail, the path or configuration may be wrong. If both succeed, investigate whether the configuration took effect, the test used the wrong key, or an authentication source was overlooked.

4. Identify multiplexed connections and existing sessions separately

Successful multiplexing does not mean revocation failed

Before revocation, you can establish a dedicated ControlMaster connection using the test key and record its control socket path. Commands sent through that socket may still succeed after revocation because authentication took place beforehand. To determine whether new authentication is being rejected, close the master connection and run the independent connection test from the previous section.

To close the local multiplexing master connection, use ssh -S "$CONTROL_SOCKET" -O exit with the original destination parameters. However, a client voluntarily disconnecting is no substitute for server-side access removal, especially when a key has been compromised.

Terminate only confirmed target sessions

On the server, cross-check sessions, process trees, and network connections to identify the target:

loginctl list-sessions
ps -eo user,pid,ppid,lstart,args --forest
sudo ss -tnp

For sessions registered through PAM, inspect the details with loginctl session-status, then run loginctl terminate-session against the clearly identified test session. For connections that are not registered as login sessions, verify the corresponding SSH process tree before terminating the specific target.

Do not indiscriminately kill processes by account. Doing so may stop databases, transfer jobs, and automation that must remain running. Record the target and expected impact before termination, then confirm that the original channel can no longer execute commands. Ending a session does not guarantee that every background task has exited; tasks running independently of the session require separate checks.

5. Finish with positive and negative tests, and preserve evidence

Acceptance testing must cover at least four points: new authentication with the revoked key is rejected, new authentication with the recovery key succeeds, the specified old connection has been disconnected, and non-target services have not been inadvertently terminated. Cross-check the relevant logs so that a network timeout or an incorrect destination address is not mistaken for successful key revocation.

On Ubuntu Server 24.04, use journalctl -u ssh to retrieve authentication and reload records. Log contents depend on the current logging configuration; do not assume every entry includes a full fingerprint. Preserve the effective configuration output, KRL query results, test exit statuses, and session termination records. Remove sensitive connection information before archiving them.

If rollback is necessary, restore the previous configuration and revocation list, then run the syntax check, reload, and independent connection tests. Rolling back the configuration does not make a compromised key trustworthy again. Restore normal access using a newly generated and verified key. The final deliverable is a verifiable access boundary, not merely an edited public key file.

Frequently asked questions

Does removing a key from authorized_keys disconnect active SSH sessions?

No. Removing a key or revoking it through a KRL affects subsequent authentication. Established sessions and multiplexed connections must be identified and terminated separately.

Why does SSH still work after revoking a key?

The client may reuse an existing connection or select another key or authentication method. Test a fresh connection with one explicit key and check the effective server-side RevokedKeys path.

What happens if the KRL file cannot be read?

An unreadable configured RevokedKeys file can cause all public-key authentication to be refused. Check its path and permissions, test a fresh login, and retain a verified recovery connection.

AMD EPYC 7543P Dedicated Server

From $457.5/month

Review server specs for your workload

Dedicated IPv4, bandwidth, storage, and OS licensing are listed individually on the configuration page. The total is calculated monthly and based on the selected rental term.