27

I'm building a Docker container, and I need to add my user to a group. usermod is not available in Alpine Linux by default. Apparently, you can add shadow from apk to install usermod, but I would prefer to not install additional packages.

Is there an alternative way to add a user to a group, or an equivalent tool to usermod available in Alpine?

Zak
  • 371
  • 1
  • 3
  • 5
  • 1
    [Have you seen this](https://github.com/chrootLogin/docker-nextcloud/issues/3#issuecomment-251181545)? Basically adding environment variables for UID and GID when initializing. – Giacomo1968 Jan 17 '19 at 17:20
  • 3
    Why not simply modify the /etc/group file directly? – davidgo Jan 17 '19 at 18:12
  • @davidgo Some people don’t know you can just edit it like that but it’s a good solution. Would need to be scriptable via `sed` or something like that. – Giacomo1968 Jan 17 '19 at 21:09
  • 1
    `shadow` (`apk --no-cache add shadow`) is a very viable alternative if you need to retain exact host uid/gid and have no chance to the default commands adduser and addgrup do not support the `--non-unique` (allow to create users with duplicate (non-unique) UID) flag. Only weights less than 10MiB for those concerned about space consumption. – n1nsa1d00 Jul 01 '21 at 11:20

1 Answers1

25

You should be able to use the built-in addgroup command to add the user to a given group:

$ addgroup --help
BusyBox v1.29.3 (2019-01-24 07:45:07 UTC) multi-call binary.

Usage: addgroup [-g GID] [-S] [USER] GROUP

Add a group or add a user to a group

        -g GID  Group id
        -S      Create a system group

So running addgroup ${USER} ${GROUP} should update /etc/groups without needing to edit the file directly.

tzrlk
  • 351
  • 3
  • 6