How to delete user with home directory in Linux

How to delete user with home directory in Linux

Linux system administrators have to manage multiple user accounts on their system. Often each of these users has their own home directory or folder to store their files and run their own applications. Sometimes system administrators need to remove user accounts from their system, in case an employee leaves the organization or an account remains unused for a long time. In this article, we will learn how to delete user accounts with home directory in Linux.

Here are the steps to delete user accounts with home directory in Linux.

1. Create User Accounts

First, we will create user account test1 for our purpose, using the following adduser commands. You can also use useradd command for this purpose. Its home directory will be located at /home/test1.

# adduser test1 # passwd test1

You can use deluser (for Debian/Ubuntu) and userdel (for RHEL/Fedora/CentOS) for deleting user accounts. But the thing is that you can delete users from Linux only when they are not logged in. So if they are logged in and using different applications and services, you will need to terminate them first and forcefully logout the users before deleting their account.

2. Lock User Accounts in Linux

First of all, you need to lock the user account that you want to delete from your system, so that they are unable to run any processes or even log into your system. You can do this by using passwd command with –lock option, followed by username. Here is the command to lock user test1.

# passwd --lock test1 Locking password for user test1. passwd: Success

3. Find & Kill All Processes for User

Next, find out all running processes for the said user using pgrep command. It will give you the PIDs of all processes being run by the user. Here is the command to find all processes run by user test1.

# pgrep -u test1 2343 2323 3443 ...

You can further use the above command’s output in ps command, to get more details about these processes, such as their username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, command path, etc.

# ps -f --pid $(pgrep -u tecmint) UID PID PPID C STIME TTY STAT TIME CMD test1 2343 1 0 10:49 ? SLl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login test1 2323 1280 0 10:49 ? Ssl 0:00 mate-session test1 3443 1959 0 10:49 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session

Next, you can use kill command to kill these processes. Use -9 option to kill the processes, followed by the PIDs of all processes that you want to kill.

# kill -9 2343 2323 3443 ...

Alternatively, you can use the -u option with killall command to kill all the processes of a given user.

# killall -9 -u test1

In the above command, we use -9 option to send SIGKILL signal to processes. We use -u option to specify the username whose processes we want to kill.

Sometimes killall command is not present in RHEL/CentOS/Fedora system. If killall command is not present on your system, you can use the following command to install it.

# yum install psmisc [On RedHat/CentOS 7.x] # dnf install psmisc [On Fedora 21+ versions]

4. Backup User Data

It is advisable to backup all files & directories in your user’s home directory for future use. /home/test1 is the folder of user test1. Here is the command to backup the contents of test1 user.

# tar jcvf /home/test1-backup.tar.bz2 /home/test1

5. Delete User Accounts

Now you can use deluser or userdel command, depending on your Linux distribution, to delete user account. Although this command deletes user credentials from the system, it will not delete the content of user’s home directory. For this purpose, you can use –remove-home option.

# deluser --remove-home test [On Debian and its derivatives] # userdel --remove test [On RedHat/CentOS based systems]

In this article, we have learnt how to delete user accounts in Linux. As mentioned earlier, the key is to lock the user account first, and kill all processes being run by the user, before you delete the user’s account. Otherwise, you will get an error.

Also read:

How to Update or Change System Locale in Linux
Linux Grep Binary Files for Strings
Linux Prevent File From Being Modified
Linux Rename File With Special Characters
Linux Rename File Starting With Dash

The passwd file entry for a user associated with a service (squeezelite) indicates that user has a home directory (/home/squeezelite). This folder does not exist (and I suspect it never did).

The service is running fine, so my inclination is to modify the passwd file to indicate the user has no home directory. Is there some way to accomplish this with usermod or should I edit the passwd file directly? If the answer is to edit the passwd file, what change should I make? Replace /home/squeezelite with /

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.

How to delete user with home directory in Linux

How to Delete User Accounts with Home Directory in Linux

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.

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.