Users, Groups and sudoLesson 3 of 4
Groups and shared access
Create groups, add users correctly, understand primary versus supplementary membership, and build a shared project directory.
groupaddgroupmodgroupdelusermodgpasswdidgroupsgetentnewgrp
After this lesson you can
- Create, rename and delete groups with a chosen GID.
- Add and remove members without removing the groups they already have.
- Build a directory that a team can share, using group ownership and the setgid bit.
The idea
Groups let you give the same permission to several people at once. Permissions on a file are checked against three things: the owner, the group and everybody else. Putting users in a group and giving the group access is how teams share a directory.
Each user has:
- one primary group, stored in
/etc/passwdand used as the group of newly created files, - any number of supplementary groups, listed in
/etc/group.
id shows both. The most common mistake is usermod -G without -a, which replaces the supplementary list and silently removes the user from every other group, including wheel.
| Task | Command |
|---|---|
| Create a group | groupadd [-g GID] name |
| Rename / change GID | groupmod -n new old, groupmod -g GID name |
| Delete a group | groupdel name (fails if it is anyone’s primary group) |
| Add a member | usermod -aG group user or gpasswd -a user group |
| Remove a member | gpasswd -d user group |
| Set the primary group | usermod -g group user |
| Show membership | id user, groups user, getent group name |
Group changes apply at the next login. A running shell keeps its old group list until the user logs in again (or runs newgrp group for a one-off shell).
Try it
sudo groupadd -g 3000 devs
sudo usermod -aG devs,wheel dana
getent group devs
id dana
devs:x:3000:dana
uid=1002(dana) gid=1002(dana) groups=1002(dana),10(wheel),3000(devs)
sudo usermod -G devs dana
id dana
uid=1002(dana) gid=1002(dana) groups=1002(dana),3000(devs)
wheel is gone: without -a the supplementary list was replaced by devs. Put it back with sudo usermod -aG wheel dana.
A shared directory
For files to stay shareable you need two things: group ownership and the setgid bit on the directory. With setgid (2 in the mode, s in the group execute position), new files inherit the directory’s group instead of the creator’s primary group.
sudo mkdir -p /srv/projects
sudo chown root:devs /srv/projects
sudo chmod 2770 /srv/projects
ls -ld /srv/projects
drwxrws---. 2 root devs 6 Nov 3 11:05 /srv/projects
sudo -u dana bash -c 'echo hello > /srv/projects/plan.txt; chmod g+w /srv/projects/plan.txt'
sudo -u erin bash -c 'echo more >> /srv/projects/plan.txt'
ls -l /srv/projects
-rw-rw-r--. 1 dana devs 11 Nov 3 11:06 plan.txt
Going further
getent group reads every source (files, LDAP, IdM), while grep /etc/group sees only local files. gpasswd -M a,b,c group sets the exact member list. groupmod -g changes a GID, but files keep the old number, so fix them with find / -gid OLD -exec chgrp NEW {} +. Groups with a password (gpasswd group) exist but are rarely used.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Create a group and look it up
Create a group
devswith GID3000, then show its line from the group database usinggetent.Check your result
getent group devsprintsdevs:x:3000:.Hint
groupadd -g 3000 devs.Solution
sudo groupadd -g 3000 devs getent group devs - Practice
Add a user without losing other groups
Make
danaa member ofdevsandwheel. She must keep any groups she already had. Show her groups before and after.Check your result
id danalistsdevsandwheelas supplementary groups together with her primary group.Hint
usermod -aG: the-a(append) is essential. Without it the supplementary list is replaced.Solution
id dana sudo usermod -aG devs,wheel dana id dana - Practice
Change the primary group
Make
devsthe primary group oferin. Create a file aserinand show that it is owned by groupdevs.Check your result
id erinshowsgid=3000(devs). A file created by erin listserin devsas owner and group.Hint
usermod -g devs erinchanges the primary group. Create the file withsudo -u erin touch /tmp/erin-file.Solution
sudo usermod -g devs erin id erin sudo -u erin touch /tmp/erin-file ls -l /tmp/erin-file - Practice
A directory the team shares
Create
/srv/projectsowned byroot:devs. Members ofdevsmust be able to create and edit files; everybody else must have no access; new files must automatically belong to groupdevs. Test withdanacreating a file anderinediting it.Check your result
ls -ld /srv/projectsshowsdrwxrws---,root devs. A file created by dana is groupdevsand can be changed by erin.Hint
chgrp,chmod 2770. The2is the setgid bit: new files inherit the directory's group.Solution
sudo mkdir -p /srv/projects sudo chown root:devs /srv/projects sudo chmod 2770 /srv/projects ls -ld /srv/projects sudo -u dana bash -c 'echo hello > /srv/projects/plan.txt; chmod g+w /srv/projects/plan.txt' sudo -u erin bash -c 'echo more >> /srv/projects/plan.txt' ls -l /srv/projects - Challenge
Remove a member and a group cleanly
Remove
erinfromdevswithout touching any other group she has (change her primary group back to a private grouperinfirst). Then delete the groupdevsonce nobody has it as primary, and show what happens to the files that were owned by it.Check your result
id erinno longer listsdevs. Aftergroupdel devs,ls -l /srv/projects/plan.txtshows the numeric GID3000instead of a name.Hint
groupdelrefuses to delete a group that is anyone's primary group.gpasswd -d user groupremoves one member.Solution
sudo groupadd erin sudo usermod -g erin erin sudo gpasswd -d dana devs sudo groupdel devs ls -l /srv/projects/plan.txt