Last active
January 3, 2020 22:21
-
-
Save davidlares/83f86113bee25c704f54728c8a07bdf6 to your computer and use it in GitHub Desktop.
Deleting/Disabling local system Linux Users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# deleting - disabling linux accounts | |
readonly ARCHIVE_DIR='/archive' # program variable | |
usage() { | |
echo "Usage: ${0} [-dra] USER [USERN]" | |
echo "Disable a local Linux account" | |
echo "-d Deletes accounts instead of disabling them" | |
echo "-r Removes the home directory associated with the account(s)" | |
echo "-a Creates an archive of the home directory associated with the account(s)" | |
exit 1 | |
} | |
# root exec | |
if [[ "${UID}" -ne 0 ]] | |
then | |
echo "Run it with sudo privileges or as root" | |
exit 1 | |
fi | |
# options | |
while getopts dra OPTION | |
do | |
case ${OPTION} in | |
d) DELETE_USER='true' ;; | |
r) REMOVE_OPTION='-r' ;; | |
a) ARCHIVE='true' ;; | |
?) usage ;; | |
esac | |
done | |
# removing options | |
shift "$(( OPTIND -1 ))" | |
# check params inserted | |
if [[ "${#}" -lt 1 ]] | |
then | |
usage | |
fi | |
# looping usernames | |
for USERNAME in "${@}" | |
do | |
echo "Processing user: ${USERNAME}" | |
USERID=$(id -u ${USERNAME}) | |
# evaluating < 1000 UID logic | |
if [[ "${USERID}" -lt 1000 ]] | |
then | |
echo "Unable to delete the ${USERNAME} account with UID ${USERID}" | |
exit 1 | |
fi | |
# creating archive logic | |
if [[ "${ARCHIVE}" = "true" ]] | |
then | |
if [[ ! -d "${ARCHIVE_DIR}" ]] # checking directory existance | |
then | |
echo "Creating ${ARCHIVE_DIR} directory" | |
mkdir -p ${ARCHIVE_DIR} | |
if [[ "${?}" -ne 0 ]] | |
then | |
echo "The archive directory ${ARCHIVE_DIR} could not be created" | |
exit 1 | |
fi | |
fi | |
# Archive the users's home directory and assign it to the ARCHIVE_DIR | |
HOME_DIR="/home/${USERNAME}" | |
ARCHIVE_FILE="${ARCHIVE_DIR}/${USERNAME}.tgz" | |
if [[ -d "${HOME_DIR}" ]] | |
then | |
echo "Archiving ${HOME_DIR} to ${ARCHIVE_FILE}" | |
tar -zcf ${ARCHIVE_FILE} ${HOME_DIR} &> /dev/null | |
if [[ "${?}" -ne 0 ]] | |
then | |
echo "Could not create ${ARCHIVE_FILE}" | |
exit 1 | |
fi | |
else | |
echo "${HOME_DIR} does not exist or is not a directory" | |
exit 1 | |
fi | |
fi | |
if [[ "${DELETE_USER}" = 'true' ]] | |
then | |
# delete the user | |
userdel ${REMOVE_OPTION} ${USERNAME} | |
# checking if succeded | |
if [[ "${?}" -ne 0 ]] | |
then | |
echo "The account was not deleted" | |
exit 1 | |
fi | |
echo "The account ${USERNAME} was deleted" | |
else | |
chage -E 0 ${USERNAME} | |
if [[ "${?}" -ne 0 ]] | |
then | |
echo "The account ${USERNAME} was not disabled" | |
exit 1 | |
fi | |
echo "The account ${USERNAME} was disabled" | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check "getopts" flags for more instructions, or check the content of the "usage" function placed there.