In this instructional tutorial, I will make yours through steps you can use to delete a user’s account along with his/her home directory on a Linux system.
To figure out how to create user accounts and manage them on Linux systems, read the accompanying articles from the links below:
As a System Administrator in Linux, you may need to remove users’ accounts after at some point when a user account may get torpid for such a long time, or the user may leave the association or organization or some other reasons.
- How to Create and Manage Users Using “Useradd” Linux Command
- Best Linux “Usermod” Command With Examples
- Managing Users And Groups In Linux
As a System Administrator in Linux, you may need to remove users’ accounts after at some point when a user account may get torpid for such a long time, or the user may leave the association or organization or some other reasons.
While removing user accounts on a Linux system, it is likewise essential to remove their home directory to let free space on the storage devices for new system users or different services.
Follow the below Article for Delete User Accounts with Home Directory in Linux:
1. For exhibit reason, first I will begin by making two user accounts on my system that is user msdhulap and user itsmarttricks with their home directories /home/msdhulap and /home/itsmarttricks individually utilizing adduser command.
# adduser msdhulap # passwd msdhulap # adduser itsmarttricks # passwd itsmarttricks
From the screen capture above, I have utilized the adduser
command to make user accounts on Linux. You can likewise utilize useradd
command, both are the same and do likewise work.
2. We should now move further to perceive how to erase or remove user accounts in Linux utilizing deluser
(For Debian and its subsidiaries) and userdel
(For RedHat/CentOS based systems) command.
The mandates inside the design document for deluser
and userdel
commands decide how this will handle with all user files and directory when you run the command.
Let us look at the configuration file for the deluser command which is /etc/deluser.conf
on Debian derivatives such as Ubuntu, Kali, Mint, and for RHEL/CentOS/Fedora users, you can view the /etc/login.defs
files.
The values in this configuration are default and can be changed according to your requirements.
# vi /etc/deluser.conf [On Debian and its derivatives] # vi /etc/login.defs [On RedHat/CentOS based systems]
3. To delete a user with a home directory, you can utilize the high-level path by following these means on your Linux server machine. At the point when users are logged on to the server, they use services and run various processes. Note that user be deleted successfully when they are not logged on to the server.
Lock User Accounts in Linux:
Start by locking the user account password so that there is no access for the user to the system. This will keep a user from running processes on the system.
The passwd
command including the –lock
option can help you achieve this:
# passwd --lock msdhulap Locking password for user msdhulap. passwd: Success
Find and Kill All Running Processes of User:
Next, discover all running processes of user account and kill them by deciding the PIDs (Process IDs) of processes owned by the user utilizing:
# pgrep -u msdhulap 1947 1959 2091 2094 2095 2168 2175 2179 2183 2188 2190 2202 2207 2212 2214
At that point you can list the processes in terms of username, PIDs, PPIDs (Parent Process IDs), terminal utilized, process state, command way in a full formatting style with the assistance of the following command as appeared:
# ps -f --pid $(pgrep -u msdhulap) UID PID PPID C STIME TTY STAT TIME CMD msdhulap 1947 1 0 10:49 ? SLl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login msdhulap 1959 1280 0 10:49 ? Ssl 0:00 mate-session msdhulap 2091 1959 0 10:49 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session msdhulap 2094 1 0 10:49 ? S 0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session msdhulap 2095 1 0 10:49 ? Ss 0:00 //bin/dbus-daemon --fork --print-pid 6 --print-address 9 --session msdhulap 2168 1 0 10:49 ? Sl 0:00 /usr/lib/dconf/dconf-service msdhulap 2175 1959 0 10:49 ? Sl 0:02 /usr/bin/mate-settings-daemon msdhulap 2179 1959 0 10:49 ? Sl 0:47 marco msdhulap 2183 1 0 10:49 ? Sl 0:00 /usr/lib/gvfs/gvfsd msdhulap 2188 1959 0 10:49 ? Sl 0:00 mate-panel msdhulap 2190 1 0 10:49 ? Sl 0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes msdhulap 2202 1 0 10:49 ? S<l 0:20 /usr/bin/pulseaudio --start --log-target=syslog msdhulap 2207 1959 0 10:49 ? S 0:00 /bin/sh /usr/bin/startcaja msdhulap 2212 1 0 10:49 ? Sl 0:03 /usr/bin/python /usr/lib/linuxmint/mintMenu/mintMenu.py msdhulap 2214 1 0 10:49 ? Sl 0:11 /usr/lib/mate-panel/wnck-applet ....
When you locate all the running processes of the user, you can utilize the killall command to execute those running processes as appeared.
# killall -9 -u msdhulap
The – 9 is the sign number for the SIGKILL sign or use – KILL rather than – 9 and – u defines username.
Note: In ongoing arrivals of RedHat/CentOS 7.x variants and Fedora 21+, you will get the error message as:
-bash: killall: command not found
To fix such error, you need to install psmisc bundle as appeared:
# yum install psmisc [On RedHat/CentOS 7.x] # dnf install psmisc [On Fedora 21+ versions]
Backup User Data Before Deleting:
Next, you can backup users’ files, this can be discretionary yet it is suggested for sometime later when the need emerges to review user account details and files.
I have utilized the tar utilities to create a backup of users home directory as follows:
# tar jcvf /user-backups/msdhulap-home-directory-backup.tar.bz2 /home/msdhulap
Delete/Remove User Account and Files:
Presently you can securely remove the user along with his/her home directory, to remove all user records/files on the system utilize the – remove-all-files option in the command below:
# deluser --remove-home msdhulap [On Debian and its derivatives] # userdel --remove msdhulap [On RedHat/CentOS based systems]
That’s all, In this article, we have explained the How to Delete User Accounts with Home Directory in Linux. I hope you enjoy this article. If you like this article, then just share it and then do subscribe to email alerts for Linux, Windows,macOS tutorials. If you have any questions or doubts about this article, please comment.