Deleting User Accounts

Howto delete user and system accounts

The command to user for removing a user or system account in Linux is the "userdel" command. The userdel command when issued will remove any entries for the specified account from "/etc/passwd", "/etc/shadow"/ and "/etc/group".




Deleting a user account - userdel command examples



Basic Syntax : userdel userid



Remove account, but Leave Home Directory



userdel userid


root@john-desktop:/home# ls -l
total 28
drwxr-xr-x 31 family      family       4096 Jan 30 19:22 family
drwxr-xr-x 72 john        john        16384 Mar  2 09:35 john
drwxr-xr-x 21 landoflinux landoflinux  4096 Jan 23 14:13 landoflinux
drwxr-xr-x  2 testuser    testuser     4096 Mar  2 10:39 testuser

root@john-desktop:/home# userdel testuser

root@john-desktop:/home# ls -l
total 28
drwxr-xr-x 31 family      family       4096 Jan 30 19:22 family
drwxr-xr-x 72 john        john        16384 Mar  2 09:35 john
drwxr-xr-x 21 landoflinux landoflinux  4096 Jan 23 14:13 landoflinux
drwxr-xr-x  2        1003        1003  4096 Mar  2 10:39 testuser

root@john-desktop:/home# id userdel
id: userdel: No such user

In the above example the "userdel" command was issued against the user account "testuser". After the "userdel" command was issued, a "ls" command was used to display the current files/directories under the "/home" area. We can see that home directory for the user "testuser" still remains as we did not specify to remove the directory. This option is useful if you believe that the user directory may contain files you may wish to keep. The "id" command was issued to verify that the account "testuser" has been deleted.



Remove account and Home Directory



userdel -r userid



root@john-desktop:/home# ls -l
total 28
drwxr-xr-x 31 family      family       4096 Jan 30 19:22 family
drwxr-xr-x 72 john        john        16384 Mar  2 09:35 john
drwxr-xr-x 21 landoflinux landoflinux  4096 Jan 23 14:13 landoflinux
drwxr-xr-x  2 testuser    testuser     4096 Mar  2 10:43 testuser

root@john-desktop:/home# userdel -r testuser

root@john-desktop:/home# ls -l
total 24
drwxr-xr-x 31 family      family       4096 Jan 30 19:22 family
drwxr-xr-x 72 john        john        16384 Mar  2 09:35 john
drwxr-xr-x 21 landoflinux landoflinux  4096 Jan 23 14:13 landoflinux
root@john-desktop:/home# id testuser

id: testuser: No such user

This time we have used the "-r" option on the "userdel" command. As you can see from the above output the directory "/home/testuser" is removed along with the associated account.