How to Delete User Account in Linux

This post will guide you how to delete or remove a User account from the command line in your Linux operating systems. How do I delete a user account and its related files in your current Linux system using userdel command.

delete user account linux1

Userdel Command


userdel is a low level utility for removing users. On Debian, administrators should usually use deluser instead. The userdel command modifies the system account files, deleting all entries that refer to the user name LOGIN. The named user must exist.

The syntax for the userdel command is as followss:

userdel  [options]  UserAccount

Note: if you want to remove a user account, and you need to be logged in as root or the current logged user need to have sudo permission.

Delete a User Account using Userdel Command


If you only want to delete a user account called mytest from your Linux system, just type the following command:

$ sudo userdel mytest

This command will remove the user entries from the /etc/passwd and /etc/shadow files.

Delete a User Account with Home Directory


If you only use “userdel” command without any options, and it will only delete that user account, and its home directory still exist in your Home directory. And if you want to completly remove all files in the user’s home directory along with the home directory itself and the user’s mail sppol, and you need to pass the “-r” option to the userdel command, type the following command:

$ userdel -r mytest

Force Delete a User Account


You are not able to use the userdel command to delete a user who is still logged in. And you will get a warning message as followss:

userdel: user mytest is currently used by process 4224

You can pass the -f option to the userdel command to forces the removal of the user account. It also forces to remove the user’s home directory and mail spool, even if another user uses the same home directory or if the mail spool is not owned by the specified user. type:

$ userdel -f mytest

Outputs:

devops@devops:~$ sudo userdel -f mytest
userdel: user mytest is currently used by process 6599
devops@devops:~$ id mytest
id: ‘mytest’: no such user
devops@devops:~$

Conclusion


You should know that how to use userdel command to delete or remove a user account in CentOS or RHEL or Ubuntu Linux server.

You might also like:

Sidebar



back to top