Skip to content

Instantly share code, notes, and snippets.

@jacksonporter
Last active March 21, 2020 01:06
Show Gist options
  • Save jacksonporter/1bd7292cd5da603729b09424ca5df752 to your computer and use it in GitHub Desktop.
Save jacksonporter/1bd7292cd5da603729b09424ca5df752 to your computer and use it in GitHub Desktop.
Removes a user and their home directory as well as deleting any mention of their name in the sudoers file
#!/usr/bin/env bash
set -e
eval "$(curl -fsSL https://gist.github.com/jacksonporter/791145218e977ef9165abd9fdf8fbfd4/raw)" # obtains my common bash utilies script and "sources" it
check_if_root
read -p "Enter a username: " USERNAME
if [ "${USERNAME}" == "" ]; then
print_error "You did not enter a user name."
exit 1
fi
deluser ${USERNAME} && rm -rf /home/${USERNAME}
grep -v "${USERNAME}" /etc/sudoers > /tmp/sds && sudo visudo -c -f /tmp/sds
chown root:root /tmp/sds
chmod 0440 /tmp/sds
cp /tmp/sds /etc/sudoers
rm -rf /tmp/sds
@jacksonporter
Copy link
Author

Careful! This is a dangerous script. The awk command will delete an entire line from the sudoers file that contains the specified username.

@jacksonporter
Copy link
Author

jacksonporter commented Mar 21, 2020

How to run easily:

curl -fsSL https://gist.github.com/jacksonporter/1bd7292cd5da603729b09424ca5df752/raw > "./remove_user.sh" && sudo bash "./remove_user.sh" && rm "./remove_user.sh"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment