Contents...
As Linux Administrator you may have to remove users account when a user leave organization or a company for any reason. If you remove user’s account it is also important to remove their home directory to free up some space on the storage device.
In this article I will show you how to delete user’s account together with his/her home directory in Linux/Unix system.
Delete User Account with Home Directory
To complete this tutorial I am going to create two user account the my Linux system called “sagar” and “linuxuser” with their home directory /home/sagar and /home/linuxuser using adduser command.
# adduser sagar # passwd sagar # adduser linuxuser # passwd linuxuser
Above you can see I have created two user with adduser command you can also use useradd command to create user account both are same and does the same job.
Before delete users with their home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that user can only be deleted effectively when they are not logged on to the server.
Lock User Account
First of all lock the user account password that you want to delete, so that there is no access for the user to the system. This will also prevent a user from running process on the system.
# passwd --lock sagar Locking password for user sagar. passwd: Success
Find and Kill all Running Process of a Particular User
Now find the all running process of a particular user and kill them by determine the PIDs of process owned by the user using below command:
# pgrep -u sagar 1847 1859 1991 1994 1995
You can also list full details using below command such as username, PIDs, PPIDs, Terminal used, Process state, command path etc.
# ps -f --pid $(pgrep -u sagar) UID PID PPID C STIME TTY STAT TIME CMD sagar 1847 1 0 10:49 ? SLl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login sagar 1859 1280 0 10:49 ? Ssl 0:00 mate-session sagar 1991 1959 0 10:49 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session sagar 1994 1 0 10:49 ? S 0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session sagar 1995 1 0 10:49 ? Ss 0:00 //bin/dbus-daemon --fork --print-pid 6 --print-address 9 --session
Once finding the all running process of a particular user now you can use killall command to kill those process like below:
# killall -9 -u sagar
Where -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines username.
Backup User Data
You can keep user data backup before deleting it. It is important for future purpose if a new user join a company you can copy all user directory and files to new user’s home directory.
Use tar utilities to create a backup of users home directory like below:
# tar jcvf /opt/user-bkp/sagar-home-directory-backup.tar.bz2 /home/sagar
Delete User Account and their Data
Now you can remove user account and his/her home directory using –remove-home option on Debian system, you can also try –remove-all-files. For CentOS/RedHat user –remove in the command line.
# deluser --remove-home sagar [ For Debian System] # userdel --remove sagar [ For CentOS/RedHat System]
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Leave a Comment