Remove User Home Directory Linux after Delete User

Posted on

This is an article made to show how to remove home user directory in Linux operating system although the user itself has already been removed.

User which has been created before can be erased along with the user’s home directory with the right command executed in bash prompt. But sometime, there is a certain condition and situation where the user has already been erased but certain attributes, belongings or any other things related with the erased user still exist in the server.

Those things need to be checked are :

1. Erasing home directory of removed or deleted user.

Executing command to remove user normally shown as follows :

userdel myuser

This is when the command executed in the following bash prompt considered that the user which is going to remove is app-admin :

[root@hostname ~]# userdel app-admin
[root@hostname ~]# cd /home
[root@hostname home]#

After successfully deleting the user, there are actually certain things which is needed to be adjusted. One of them is definitely the folder of the deleted user correlated which is called as home user directory. And in this case, the user named ‘app-admin’ has its own home user directory located in /home/app-admin. If it is too late to execute the command to delete user simultaneously removing its home directory, the process of removing its home directory itself must be done manually. Below is how to do it :

rm -rf username

Description : 
rm : It is a command which is used to remove folder, files generally exist in UNIX/Linux operating system variant
-rf : It is another additional parameter which can be assumed as (r)ecursive and (f)orce. The recursive parameter is used to remove the directories and also the contents recursively. The other parameter, the force parameter is used remove with force ignoring nonexistent files, arguments even prompting for asking to remove it or not. 
username : It is in the context the name of the folder which exist in /home as its own home directory.

This is the executed command in a bash prompt :

[root@hostname home]# cd /home/
[root@hostname home]# rm -rf app-admin

2. Removing all attributes which has nouser and nogroup since deleting user in the previous action would cause folders or files created by the deleted user will still remain.

[root@hostname home]# find / -nouser -o nogroup 2> /dev/null

Description : 
find : It is a command used to find folder or files 
/ : It is defined as the start location of the searching process, in the above context it starts from the root partition (/)
-nouser : It is an additional parameter used 
-nogroup : It is an additional parameter 
2 : It is a file descriptor for standard error or STDERR.  
> : Redirect output which is generated before the ">" sign. 
/dev/null : It is a special device that discards everything which is written to it.

In the above executed command, find any files or folders exist which are not belong to any user or any groups. If in the searching process it stumbles on error, just redirect it to /dev/null or discard it.

Based on the findings, remove all files or folders which aren’t belong to any user so it will prevent any malicious actions which may be┬ádone by exploiting those files or folders.

One thought on “Remove User Home Directory Linux after Delete User

Leave a Reply