Skip to content

useradd: race conditions with --create-home #1649

@snejok

Description

@snejok

When creating user with --create-home, for example this way:

# useradd -m test1 ; useradd -m test1
useradd: user 'test1' already exists

# grep test1 /etc/passwd
test1:x:1097:1097::/home/test1:/bin/sh

# stat -c '%u %U' /home/test1
1097 test1

second useradd failed, ownership is OK

but if for some reason two processes try to create the same user simultanuosly, there is a race condition:

# useradd -m test2 & useradd -m test2
useradd: warning: the home directory /home/test2 already exists.
useradd: Not copying any file from skel directory into it.

# grep test2 /etc/passwd
test2:x:1099:1099::/home/test2:/bin/sh

# stat -c '%U' /home/test2
1098 UNKNOWN

ownership is bad, because uid is wrong

Tested on:
ubuntu 22.04
ubuntu 24.04
ubuntu 26.04

I have local only installation, no ldap or something like that

egrep '(passwd|group|shadow)' /etc/nsswitch.conf

passwd: files systemd
group: files systemd
shadow: files systemd
gshadow: files systemd

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions