Linux How To: Delete A User Account

In Linux we can delete local user accounts with the ‘userdel’ command. Here we will cover some examples of using the various syntax options with userdel in order to remove a local user account in Linux.

If you need test accounts to practice deleting, see our post on creating new user accounts.

Remove a User Account

We can delete local user accounts in Linux with the ‘userdel’ command, essentially all we really need to do is specify a username that exists after the command that we wish to remove.

[root@server ~]# userdel testaccount

This will delete the testaccount user from the /etc/passwd, /etc/group and /etc/shadow configuration files, however by default it will not delete the user’s home directory or contents.

If you also wish to clean up the home directory when deleting a user, specify the -r flag.

[root@server ~]# userdel -r testaccount

The -r flag will also delete the user’s mail spool.

Note: By default the ‘userdel’ command will not successfully remove a logged in user that has running processes, to remove the user you would usually need to stop these processes and logout first. With the -f option, we can force userdel to delete the account regardless of any running processes. This option also essentially performs the functionality of the -r flag, in that the user’s home directory and mail will also be removed.

Summary

As shown it is quite straightforward to delete a local user account in Linux with the ‘userdel’ command, simply specify the username you wish to delete and specify whether or not you want to also remove the home directory and mail contents.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>