1

I have created a user called paul using the following command:

sudo adduser paul

adduser also created a new group called paul and made it the primary group for the user paul.

I have created a file using the paul user, and displayed its information using ls-l:

-rw-r--r-- 1 paul paul 25 2017-05-14 15:30 1.txt

Then I have deleted the paul user using the following command:

sudo userdel paul

And then I have displayed the previously created file's information using ls -l:

-rw-r--r-- 1 1001 1001 25 2017-05-14 15:30 1.txt

The paul user has been replaced by its ID (which is 1001) since I have just deleted this user. But why the group ID is also displayed instead of the group name, was the group also deleted?

1 Answer 1

1

Summary

Here is the short answer: When a user is deleted its primary group is also delete, unless that group also contains other users. In this latter case the user is deleted but the group is not.

We can easily verify this ourselves.

Case 1: No other users in primary group of deleted user

First we consider the case where the primary group contains no other users.

Create the paul user:

root@host:~# useradd paul

Check for the paul group using getent:

root@host:~# getent group paul
paul:x:1001:

Delete the paul user:

root@host:~# userdel paul

Check for the paul group using getent:

root@host:~# getent group paul

root@host:~#

Notice that there is no output from this command. We can also try to delete the paul group ourselves:

root@host:~# groupdel paul
groupdel: group 'paul' does not exist

This confirms that the paul group no longer exists.

Case 2: Additional users in primary group of deleted user

Now we will check to see what happens if we try to delete a user whose group contains other users:

root@host:~# useradd user1

root@host:~# useradd user2

root@host:~# usermod -a -G user1 user2

root@host:~# groups user2
user2 : user2 user1

root@host:~# userdel user1
userdel: group user1 not removed because it has other members.

root@host:~# groups user2
user2 : user2 user1

root@host:~# getent group user1
user1:x:1002:user2

root@host:~# getent passwd user1

root@host:~# deluser user1
/usr/sbin/deluser: The user `user1' does not exist.

In this case the user1 user is deleted but the user1 group remains.

5
  • 1
    But what if the primary group have multiple users, will deleting all of the users of this group cause the primary group to be deleted? Commented Dec 10, 2017 at 3:17
  • 1
    @user7681202 One sec - I'll update my solution.
    – igal
    Commented Dec 10, 2017 at 3:19
  • 1
    Did the deletion of the user actually fail, or did it remove the user and keep the group?
    – Jeff Schaller
    Commented Dec 10, 2017 at 4:06
  • @JeffSchaller I'm a jerk. You're absolutely right. I updated the solution. Thank you for the correction.
    – igal
    Commented Dec 10, 2017 at 4:11
  • @igal Also, based on my testing, the primary group will not be deleted if it has a different name than the user. Commented Dec 10, 2017 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .