On a Linux system you can easily delete a user without having to delete any files owned by that user. Such a file will stay in place and the file owner's user ID (which is stored as an attribute of the inode) remains unchanged. This way a file can become effectively ownerless.
If you then later create a user with the same ID, the user will automatically become the owner of the previously orphaned files. That's how a new user may unintentionally (or with malicious intent) become the owner of files they shouldn't own, which is obviously bad for security.
Note that userdel
has an extra switch to remove users along with their files, but it will only remove the files from some fixed locations, such as the home directory:
-r, --remove
Files in the user's home directory will be removed along with
the home directory itself and the user's mail spool. Files
located in other file systems will have to be searched for
and deleted manually.
You may want to use find
with the -nouser
switch to discover files with a non-existent owner:
-nouser
No user corresponds to file's numeric user ID.
Here is a quick demo on Arch Linux.
Let's add a user alice
and make her the owner of a file foo
.
[root@box /]# useradd alice
[root@box /]# touch foo
[root@box /]# chown alice foo
[root@box /]# ls -l foo
-rw-r--r-- 1 alice root 0 Jan 5 02:59 foo
Now let's delete alice
.
[root@box /]# userdel alice
[root@box /]# ls -l foo
-rw-r--r-- 1 1001 root 0 Jan 5 02:59 foo
ls
can't determine the owner name and just displays the numeric owner ID 1001
. Now let's add a new user bob
.
[root@box /]# useradd bob
[root@box /]# ls -l foo
-rw-r--r-- 1 bob root 0 Jan 5 02:59 foo
Since bob
has been assigned the next free user ID, he's now automatically the owner of the file.