kazmax - Home Server on Linux

Delete Linux user account - userdel

To delete Linux user accounts, use the userdel command.

It is also possible to delete the user's home directory and mail spool at the same time. This section explains the options, formats, and usage examples of the userdel command.

Also, the userdel command changes /etc/passwd, /etc/shadow, /etc/group file.

Last Update : December 16, 2018

Delete Linux user account - userdel Contents

  1. Usage and options of "userdel" command
  2. Example of using "userdel" command

1. Usage and options of "userdel" command

Usage

userdel [ -r ]  login 

login specifies the user name.

Executing the userdel command deletes the line with the user name specified in login from /etc/passwd, /etc/shadow, /etc/group file.

OPTION

-r Delete the file in the user's home directory along with the home directory itself. Also erase the user's mail spool at the same time. Files on other file systems must be found manually and removed.

Notes

If you do not specify the -r option, the home directory and mail spool will remain on the file system. Files that existed in the home directory will also remain.

The owner and group of the remaining files are displayed as uid, gid of the deleted user.

[root@localhost ~]# id -a foo
uid=514(foo) gid=514(foo) groups=514(foo)

Check home directory of user "foo"
[root@localhost ~]# ls -ld /home/foo
drwx------  4 foo foo 4096  Dec  2 20:20 /home/foo

Run the userdel command without "-r" and delete the user.
[root@localhost ~]# userdel foo
[root@localhost ~]# ls -ld /home/foo
drwx------  4 514 514 4096  Dec  2 20:20 /home/foo
↑ Owner / group is displayed as uid, gid.

Notes

If you create a user with the useradd command, a group with the same name as the user name will be created.

If that group only belongs to that user, the group will also be deleted at the same time. If another account belongs to that group, the group will not be deleted.

2. Example of using "userdel" command

If you want to delete the user "foo", do it like this.

[root@localhost ~]# userdel foo

↓ When executing with "-r" option
[root@localhost ~]# userdel -r foo

Notes

Do not use "-r" if you want to preserve files created by users.

If you forgot to add the "-r" option to the userdel command and want to delete the target user's file later, delete the target user's home directory with rm command.

Notes

If you want to completely delete the file created by that user, it will completely disappear if you do the following.

↓ Check uid (user id) of user "foo"
[root@localhost ~]# ls -ld /home/foo
drwx------  4 514 514 4096  Dec  2 20:20 /home/foo
You can see that the uid of user "foo" is "514".

↓ Display a list of files whose uid is "514" and check whether it is a file that can be deleted.
[root@localhost ~]# find / -uid 514
/home/foo
/home/foo/.bash_logout
/home/foo/.qmail
/home/foo/.gtkrc
/home/foo/.bash_profile
・・・
[root@localhost ~]# find / -user foo -exec rm -rf {} \;
↑ Execute the "rm" command for the displayed file.

This will permanently delete the file whose owner is "foo".