Properly delete user in RHEL Linux
Simple example with 7 steps to have a user deleted properly from a RHEL Linux server.
1. Lock the User:
#passwd -l username
you can set an Expiry also on which the user account will be disabled
(syntax is usermod –expiredate YYYY-MM-DD username):
#usermod --expiredate 1 username
User will get below message :
Your account has expired; please contact your system administrator
Authentication failure
2. backup files from /home/username to /tmp/
#tar -zcvf /tmp/account/deleted/username.$uid.$now.tar.gz /home/username/
We can change the $uid, $now with actual UID and date/time. The userdel command will not allow you to remove an account if the user is currently logged in. Need to kill any running processes which belong to an account Use:
#pgrep -u username
#ps -fp $(pgrep -u username)
#killall -KILL -u username
3. Delete at jobs :
#find /var/spool/at/ -name "[^.]*" -type f -user username -delete
4. Remove any cron jobs:
#crontab -r -u username
5. Delete any print jobs:
#lprm username
6. Fine all files owned by a user :
#find / -user username -print
If any files is found with the ownership of that user, change the ownership:
# find / -user username -exec chown newUserName:newGroupName {} \;
#find / -user username -exec chown newUserName:newGroupName {} \;
7. Delete the user :
#userdel -r username
or
#userdel -r -f username
This command will delete all the files, including home directory and users mail spool.
Categories: Linux