Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

groupadd

Create a group (groupadd), change it (groupmod) or delete it (groupdel).

Level
RHCSA exam
Group
Users and groups
Needs root
Yes
Package
shadow-utils
Also called
groupmod, groupdel

What it does

Groups give the same rights to many users, for example write access to a shared folder. The groups are in /etc/group and /etc/gshadow. The GID comes from the range in /etc/login.defs (GID_MIN … GID_MAX) unless you give one.

groupmod renames a group or changes its GID. groupdel removes a group, but not if it is the primary group of a user.

Add members with usermod -aG GROUP USER or gpasswd -a USER GROUP.

How to write it

groupadd [OPTION]... GROUP
groupmod [OPTION]... GROUP
groupdel [OPTION]... GROUP

Options 17

Most used

groupadd

-g, --gid GID
Use this GID.
-r, --system
Create a system group (GID under 1000).
-f, --force
Exit with success if the group already exists (and pick another GID if -g is used).
-o, --non-unique
Allow a GID that is used already (with -g).
-U, --users USER1[,USER2...]
Add these users as members right away.
-K, --key KEY=VALUE
Override a value of /etc/login.defs (like GID_MIN).
-p, --password HASH
Set a hashed group password (not recommended).
-R, --root DIR
Work in this chroot folder.
-P, --prefix DIR
Use the files under this prefix folder.
-h, --help
Show help.

groupmod

-n, --new-name NEW_GROUP
Rename the group.
-g, --gid GID
Change the GID (files keep the old number; fix them with find).
-a, --append
With -U: add the users to the members (do not replace).
-U, --users USER1[,USER2...]
Set the member list.
-o, --non-unique
Allow a GID that is used already.
-p, --password HASH
Change the hashed group password.

groupdel

-f, --force
Delete the group even if it is the primary group of a user.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Create a group

    groupadd webadmins

    Gets the next free GID.

  2. 2Create a group with a fixed GID

    groupadd -g 3000 sales

    Same GID on all servers for NFS shares.

  3. 3Create a system group for a service

    groupadd -r appsvc

    GID under 1000.

  4. 4Create a group with members

    groupadd -U anna,bob projectx

    Newer versions only; else use gpasswd -a.

  5. 5Rename a group

    groupmod -n web-admins webadmins

    Members and files keep working: they use the GID.

  6. 6Change the GID and fix the files

    groupmod -g 3100 sales
    find / -xdev -gid 3000 -exec chgrp -h sales {} +

    Files still have the old number until you change them.

  7. 7Delete a group

    groupdel projectx

    Fails if it is the primary group of a user.

  8. 8Set up a shared team folder

    groupadd sales
    mkdir /srv/sales
    chgrp sales /srv/sales; chmod 2770 /srv/sales

    2 = setgid: new files get the group sales (see the permissions chapter).

  9. 9Do not fail when the group exists (scripts)

    groupadd -f sales

    Safe to run twice, for example in Ansible-like scripts.

  10. 10Check the group

    getent group sales
    sales:x:3000:anna,bob

    Name, password placeholder, GID, members.

  11. 11Find groups with no members (cleanup)

    awk -F: '$4 == "" && $3 >= 1000 {print $1}' /etc/group

    Private groups of users are listed too; they are normal.

Watch out

  • groupdel does not change files: they then show the old GID number.
  • A user gets new groups only after a new login.

Files

/etc/group, /etc/gshadow
The group data.
/etc/login.defs
GID_MIN, GID_MAX, SYS_GID_MIN, SYS_GID_MAX.

Exit codes

0 :: Done. 2 :: Wrong syntax. 3 :: Wrong value for an option. 4 :: The GID is used already. 6 :: The group does not exist (groupmod, groupdel). 8 :: Cannot delete the primary group of a user (groupdel). 9 :: The group name is used already. 10 :: Could not update the group file.