Deleting User Accounts

How to delete user and system accounts

The command to use 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 leaving 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 testuser
id: testuser: No such user

In the above example, we can see that the user account "testuser" has been removed, however, the home directory for the user still remains.

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" has been removed along with the account.