How to delete users in Linux with the generical userdel command

We already know how to create users in Linux, now let’s learn how to delete them: userdel

The generical low level tool for deleting users is userdel:
Syntax:userdel [options] user

$ tail -3 /etc/passwd | cut -d ':' -f 1,6
xavier:/home/profx
storm:/home/preetystorm
george:/home/george

Note: When you delete a user, be carefull not to delete the user’s files unless you want to!!!

1. Delete only the user, but ignore the files he owns: userdel without any option:

$ sudo userdel george
$ id george
id: george: No such user
$ ls -1 /home/george
georgecv

So, george’s homedir still exists.

2. Delete user, homedir and mail spool with userdel -r:

The user’s dir and the files inside will be deleted, also the mail spool will be thrown away, only the files owned by the user, that are not located in his ~ will remain undeleted.

Storm owns /home/profx/file2, located in xavier’s homedir:
$ sudo find /home/profx/ -user storm
/home/profx/file2

Now I will delete storm and her homedir and show you what /home/profx/file2 will remain in the system.
$ sudo userdel -r storm
$ ls -l /home/storm
ls: cannot access /home/storm: No such file or directory
$ ls -l /home/profix/file2
-rw-r--r-- 1 1051 1051 0 2012-05-28 04:41 /home/profx/file2

The file still exists, but it does not have on owner, now. Instead of an owner and group, there are displayed to random ids.

3. Deleting users forced: userdel -f

userdel -f will delete the user, the user’s ~, the user’s mail spool and all it’s files in the system, forced. It will perform the removing even if the user is logged on, or his files are in use.

$ sudo userdel -f xavier

Tagged with: , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!