Skip to content

Instantly share code, notes, and snippets.

@Serverfrog
Last active September 21, 2016 04:38
Show Gist options
  • Save Serverfrog/6a221b972fee3e5bfd11f02630c157ea to your computer and use it in GitHub Desktop.
Save Serverfrog/6a221b972fee3e5bfd11f02630c157ea to your computer and use it in GitHub Desktop.
A bit more Secure way to delete a User based on http://www.cyberciti.biz/faq/linux-remove-user-command/
#!/bin/sh
#A more Secure deletion of a user.
# Need Username and location where the backup should be
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Usage: secure-deluser USERNAME BACKUPLOCATION"
exit 1
fi
#
# Parameters
#
user="$1"
location="$2"
uid=$(id -u "$user")
now=$(date +"%H:%M_%d-%m-%Y")
title="Secure Deletion of a User"
#
# Parameter Validation
#
if [ ! -d $location ] || [ ! -w $location ]; then
echo "$location is not a directory or writeable"
exit 1
fi
if ! id "$user" >/dev/null 2>&1; then
echo "User $user does not exist"
exit 1
fi
#
# Last Question
#
if ! whiptail --title "$title" --yesno "Are you sure you want delete\n\n$user\n\nwith all his data and store them in \n\n$location" 20 60 ; then
exit 1
fi
#
# Operation
#
{
passwd -l $user
echo XXX
echo "10"
echo "Locking User"
echo XXX
}whiptail --gauge "Locking User" 10 70 0
sleep 1
tar -zcvf $location/$user.$uid.$now.tar.gz /home/$user/
echo "20"
> > whiptail --gauge "Backing up Home" 10 70 02
sleep 1
killall -KILL -u $user
echo "30"
> > whiptail --gauge "Killing Processes" 10 70 0
sleep 1
find /var/spool/at/ -name "[^.]*" -type f -user $user -delete
echo "40"
> > whiptail --gauge "Removing Jobs" 10 70 0
sleep 1
crontab -r -u $user
echo "50"
> > whiptail --gauge "Removing Cronjobs" 10 70 0
sleep 1
lprm $user
echo "60"
> > whiptail --gauge "Removing Printjobs" 10 70 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment