What It Does
This script does nothing but list what it would remove until you set DRY_RUN=false and APPROVE=remove-ssh-authorized-key, and it will not remove more than 25 keys in one run unless you raise MAX_CHANGES.
It finds keys in the authorized-keys files of every account with a home folder, using the paths from sshd's AuthorizedKeysFile output or the default paths if no setting is returned. A line matches its fingerprint or its comment after whitespace is normalized. A shared comment can match several distinct keys. When both are given, both must match. There are no wildcards and no partial matches, and a comment with * ? [ or fewer than 3 characters is refused. ACCOUNT limits the search to one account.
Before any file is edited it is copied to /var/lib/mspoutpost/backups/remove-ssh-authorized-key/ in a folder named for the UTC time, and the last 10 runs are kept. Only the matching key lines are deleted. Every other line, the owner and the permissions stay as they were. After the edit it scans again to verify no eligible matches remain.
When the script runs inside an SSH login, it never touches keys in the authorized_keys of the account that login belongs to, because it cannot tell which key opened the session. Such matches are skipped with a warning, and if they are the only matches the run stops with an error. Run it from your RMM agent, or from an SSH session of a different account. Keys handed out by AuthorizedKeysCommand and SSH certificate authorities are not covered, and a removed key can come back from whatever put it there.
Before You Run It
- Run get-ssh-authorized-keys first to see the keys and copy the fingerprint exactly.
- Run this one as a dry run and read the list of what it would remove.
- Make sure the person whose key you remove has another way in, or does not need one.
- Find what put the key there, such as a provisioning tool, or it can come back.
- Run it from your RMM agent, not from an SSH session of the account that holds the key.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DRY_RUN=false and APPROVE=remove-ssh-authorized-key, and it stops before making more than 25 changes in one run unless you raise MAX_CHANGES. Run it once as a dry run and read the list first.
- What it changes: Deletes the matching key line from authorized_keys files, after backing each file up.
- Restart: It never restarts the machine.
- Typical run: Under 10 seconds.
- Example answer: "Removed 2 key(s) SHA256:zZH4y0W2... from 2 file(s); backups in /var/lib/mspoutpost/backups/remove-ssh-authorized-key"
- Tested before listing on Ubuntu 24.04 (Arm64 VM) with bash, run as root, against its own test cases.
Inputs
Pass an input as an argument (--input-name value), as an environment variable named as listed, which is how most RMMs pass script variables, or change its default in the DEFAULT_ lines near the top of the script. An argument wins over an environment variable, which wins over the default.
- KEY_FINGERPRINT: SHA256 fingerprint of the key to remove (SHA256:abc...). One of KEY_FINGERPRINT or KEY_COMMENT is required.
- KEY_COMMENT: Exact comment of the key to remove (the text after the key). At least 3 characters, no wildcards.
- ACCOUNT: Only look in this account. Default: every account.
Every script also takes DRY_RUN, OUTPUT_FORMAT (Text, Answer, Json or Csv), EXPORT_PATH and RESULT_FIELD, which writes the answer to an RMM custom field, unless the list above words one differently.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.