Skip to content

usermod: Add option to automatically find subordinate IDs#1549

Merged
hallyn merged 1 commit intoshadow-maint:masterfrom
richardweinberger:rw/usermod_find_subids
Feb 26, 2026
Merged

usermod: Add option to automatically find subordinate IDs#1549
hallyn merged 1 commit intoshadow-maint:masterfrom
richardweinberger:rw/usermod_find_subids

Conversation

@richardweinberger
Copy link
Contributor

Tools such as useradd(8) automatically select subordinate UID and GID ranges based on settings in login.defs.
But when one wants to add subordinate IDs to an existing user, these ranges have to be specified manually using the -w and -v options of usermod(8).

Add a new -S / --add-subids option to usermod(8) which will, just like useradd(8), find a range based on the settings in login.defs.

@hallyn
Copy link
Member

hallyn commented Feb 24, 2026

Need to look at the patch, but definitely +1 to the feature.

Copy link
Member

@hallyn hallyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch to id_t does need to be explained, thanks.

@alejandro-colomar
Copy link
Collaborator

Could you please show a shell session testing this? It looks good.

@richardweinberger
Copy link
Contributor Author

Could you please show a shell session testing this? It looks good.

I'm glad that you asked, just noticed a bug while inspecting the shell session. :-/

@richardweinberger
Copy link
Contributor Author

Could you please show a shell session testing this? It looks good.

I'm glad that you asked, just noticed a bug while inspecting the shell session. :-/

The bug is, sub_uid_open() and sub_gid_open() have to get called before find_new_sub_uids() or find_new_sub_gids() can get used. Otherwise the result is undefined.

Tools such as useradd(8) automatically select subordinate UID and GID
ranges based on settings in login.defs.
But when one wants to add subordinate IDs to an existing user, these
ranges have to be specified manually using the -w and -v options
of usermod(8).

Add a new -S / --add-subids option to usermod(8) which will, just like
useradd(8), find a range based on the settings in login.defs.

Signed-off-by: Richard Weinberger <richard@nod.at>
@richardweinberger
Copy link
Contributor Author

Could you please show a shell session testing this? It looks good.

Here you go:

test0r:~/shadow # cat /etc/subgid
rw:100000:65536
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
test0r:~/shadow # ./src/usermod -S sepp
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # ./src/usermod -S franz -v 300000-300100
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
franz:231073:65537
franz:300000:101
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
franz:231073:65537

@richardweinberger
Copy link
Contributor Author

Could you please show a shell session testing this? It looks good.

I'm glad that you asked, just noticed a bug while inspecting the shell session. :-/

The bug is, sub_uid_open() and sub_gid_open() have to get called before find_new_sub_uids() or find_new_sub_gids() can get used. Otherwise the result is undefined.

Bug fixed and PR updated.

@alejandro-colomar
Copy link
Collaborator

Could you please show a shell session testing this? It looks good.

I'm glad that you asked, just noticed a bug while inspecting the shell session. :-/

Thanks!

@alejandro-colomar
Copy link
Collaborator

alejandro-colomar commented Feb 25, 2026

Could you please show a shell session testing this? It looks good.

Here you go:

test0r:~/shadow # cat /etc/subgid
rw:100000:65536
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
test0r:~/shadow # ./src/usermod -S sepp
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # ./src/usermod -S franz -v 300000-300100
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
franz:231073:65537
franz:300000:101
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
franz:231073:65537

It would be interesting to grep(1) the login.defs(5) relevant configs in the shell session.

Also, I think it could be good to include this in the commit message.

@richardweinberger
Copy link
Contributor Author

Could you please show a shell session testing this? It looks good.

Here you go:

test0r:~/shadow # cat /etc/subgid
rw:100000:65536
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
test0r:~/shadow # ./src/usermod -S sepp
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
test0r:~/shadow # ./src/usermod -S franz -v 300000-300100
test0r:~/shadow # cat /etc/subuid
rw:100000:65536
sepp:165536:65537
franz:231073:65537
franz:300000:101
test0r:~/shadow # cat /etc/subgid
rw:100000:65536
sepp:165536:65537
franz:231073:65537

It would be interesting to grep(1) the login.defs(5) relevant configs in the shell session.

Also, I think it could be good to include this in the commit message.

This is a freshly installed openSUSE Tumbleweed.

$ grep SUB /usr/etc/login.defs
SUB_UID_MIN                100000
SUB_UID_MAX             600100000
SUB_UID_COUNT               65536
SUB_GID_MIN                100000
SUB_GID_MAX             600100000
SUB_GID_COUNT               65536
#GRANT_AUX_GROUP_SUBIDS yes

Copy link
Collaborator

@alejandro-colomar alejandro-colomar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

From a behavior point of view, it looks good.

While there are a few stylistic things I'd like to change, the most important parts are covered, so it's also good to me. And since it's my fault for not having documented the style before, I'll not complain about those minor details.

Reviewed-by: Alejandro Colomar <alx@kernel.org>

You may want to address @hallyn's comment about id_t in the commit message. I personally don't need it, but you may need it for his approval (I don't know). :)

You may also want to include the shell session in the commit message, since it's relatively small; but also feel free to do it or not (having it in the PR can be sufficient).

Also, I'd like someone else to review the patch before merging. I'm never confident enough with new features.

@hallyn
Copy link
Member

hallyn commented Feb 25, 2026 via email

@hallyn
Copy link
Member

hallyn commented Feb 25, 2026 via email

@hallyn
Copy link
Member

hallyn commented Feb 25, 2026 via email

@hallyn hallyn merged commit 1ed06fe into shadow-maint:master Feb 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants