Why do you want to remove a user in Linux? It’s up to you, but you can use userdel command to delete a user in Linux with or without a home directory. userdel command belongs to RHEL, CentOS, Fedora, etc. but it is a low-level utility in Debian based operating systems such as Ubuntu, Debian, etc. You can use the deluser command there as well.
Basically I want to explain that userdel command in the Linux system is used to remove a user account and related files.
This command basically modifies the system account files, deleting all the entries which refer to the username LOGIN.
As you know Linux is a multi-user system, which means more than one user can use the same Linux system at the same time.
If you are working in an organization as a system administrator, then you have the responsibility to manage the system’s users and groups.
I have described in another article how to create a user in the Linux system and how to list users in Linux and how to add a user in the sudo group etc. This article only related how to delete a user in the Linux system with userdel command.
The basic syntax of userdel command
Always remember the user login, It may differ from the user name, For example, my User Name “Vijay Kumar” but this is not user login. My user login is vijay. It’s the only vijay.
You can use command whoami to see the current login, I have described in details on how to list user in Linux.
You can find the basic syntax of userdel command as follows:
userdel [OPTIONS] USER LOGIN
For more details on options used with userdel command, you can use following syntax:
[[email protected] ~]# userdel -h Usage: userdel [options] LOGIN Options: -f, --force force some actions that would fail otherwise e.g. removal of user still logged in or files, even if not owned by the user -h, --help display this help message and exit -r, --remove remove home directory and mail spool -R, --root CHROOT_DIR directory to chroot into -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files -Z, --selinux-user remove any SELinux user mapping for the user [[email protected] ~]#
You can also use the man command to display the manual page of userdel command.
How to remove a user in Linux using userdel command
First I am going to have a look on users in my Linux system. It’s really very easy to know how many users are exist on your system.
When you create a user, the home directory with username has been created in /home folder automatic.
The first and simplest way for you to know existing user in Linux machine. So run the following command to get list of users:
[[email protected] ~]# ls /home/ student student1 student2 vijay [[email protected] ~]#
In the above example there are 4 users in my system. First, I want to show you that student user is active or not. I will use ‘su command’ and ‘whoami’ command to know the logged in user.
[[email protected] ~]# su student [[email protected] root]$ whoami student [[email protected] root]$ exit exit [[email protected] ~]# whoami root [[email protected] ~]#
I am willing to remove user login name “student”. So I will run the following command
[[email protected] ~]# userdel student [[email protected] ~]# su student su: user student does not exist [[email protected] ~]# ls /home/ student student1 student2 vijay [[email protected] ~]#
See close to the above example, I have deleted user ‘student’ then try to switch to student and get error “su: user student does not exist”.
But you can see home directory of user ‘student’ still exist in /home directory. So If you just want to remove user account with its home directory go to the next section.
Remove user in Linux with its home directory
Whenever You are deleting a user using this option then the files in the user’s home directory will be removed along with the home directory itself and the user’s mail spool.
All the files located in other file systems will have to be searched for and deleted manually.
If you want to remove user account with its home directory then you will use userdel command followed by -r option:
[[email protected] ~]# userdel -r student1 [[email protected]t ~]# su student1 su: user student1 does not exist [[email protected] ~]# ls /home/ student student2 vijay [[email protected] ~]#
In the above example, I have deleted user ‘student1’ along with home directory. I have tried su command to switch student1 got error.
Again I had a look on home directory of student1, but it did not exist after deleting user.
Remove user in Linux, if multiple user using same home directory.
In the some cases, we assigned single home directory for multiple users. Both users are not only access home directory even mail spool as well.
So you must use to remove userdel command followed by -f option.
This option forces the removal of the specified user account. It doesn’t matter that the user is still logged in.
It also forces the userdel to remove the user’s home directory and mail spool, even if another user is using the same home directory or even if the mail spool is not owned by the specified user.
#user -f [username]
userdel command with -R Option
This option apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
#userdel -R [username]
userdel command with -Z :
This option remove any SELinux(Security-Enhanced Linux) user mapping for the user’s login.
#userdel -Z [username]
You have learned about userdel command in Linux, which is used to remeve user in Linux with or without its home directory.
If I have missed something please leave comment below.
For more information about the userdel command, check the man page.