In Linux, the usermod -aG command is used to add a user to supplementary (secondary) groups. Here is a breakdown of what each part does:
-G(Groups): This flag tells the system you want to specify a list of supplementary groups. Crucially, if you use-Gby itself, it will overwrite the user's current group memberships with only the ones you list.-a(Append): This flag is used only with the-Goption. It tells the system to add the new groups to the user's current list of groups, rather than replacing the existing ones.
Example in your Lab:
When you ran:
sudo usermod -aG developers userA
- Before:
userAwas only in theuserAgroup. - After:
userAremained in theuserAgroup AND was added to thedevelopersgroup.
Why the -a is so important:
Imagine a user named "Alice" is already in the finance and hr groups.
- If you run
sudo usermod -G IT Alice, she will be removed fromfinanceandhrand put only inIT. This can break her access to files! - If you run
sudo usermod -aG IT Alice, she will stay infinanceandhrand gain access toIT.
Summary: Always remember to use the -a (append) flag when adding a user to a new group unless you specifically want to remove them from all their other supplementary groups