Master Linux User Group Management: Create, Modify, Delete, and Assign Users
This guide explains how to create, modify, delete, and query Linux user groups, assign users to single or multiple groups, change group passwords, switch groups with newgrp, and adjust file ownership with chown, providing concrete command examples and practical scenarios for effective system administration.
创建用户组
1 创建用户组
Use the groupadd command followed by the desired group name.
sudo groupadd mygroup2 指定组ID
Option -g allows you to explicitly set the Group ID (GID); otherwise the system assigns the next available GID.
sudo groupadd -g 1001 mygroup3 验证用户组
Check the new group by inspecting /etc/group or using getent.
getent group mygroup修改用户组
1 修改用户组名称
Rename a group with groupmod -n followed by the new and old names.
sudo groupmod -n newgroupname oldgroupname2 修改用户组GID
Change a group's GID using groupmod -g.
sudo groupmod -g newGID groupname删除用户组
Remove a group with groupdel and the group name.
sudo groupdel groupname将用户添加到组
1 将用户添加到组
Add a user to a group using usermod -aG with the group name and username.
sudo usermod -aG groupname username2 从组中删除用户
Remove a user from a group with gpasswd -d.
sudo gpasswd -d username groupname查看用户组信息
List group members via getent or by grepping /etc/group.
getent group groupname cat /etc/group | grep groupname将用户添加到多个组
Assign a user to several groups at once by separating group names with commas.
sudo usermod -aG developers,designers john使用 newgrp 切换组
Switch the current session to another group without logging out using newgrp.
newgrp developers查看用户所属的所有组
Show all groups a specific user belongs to with groups.
groups username组密码
Set a password for a group with gpasswd; only users who know the password can add others to the group.
sudo gpasswd groupname使用 chown 更改文件所有者和组
Change both file owner and group with chown using the owner:group syntax.
sudo chown john:developers file.txt示例场景:创建 Web 开发团队用户组
Create a webdev group and add developers Alice and Bob.
sudo groupadd webdev
sudo usermod -aG webdev alice
sudo usermod -aG webdev bob示例场景:创建文件共享组
Create a fileshare group and add Alice, Bob, and Charlie.
sudo groupadd fileshare
sudo usermod -aG fileshare alice
sudo usermod -aG fileshare bob
sudo usermod -aG fileshare charlie总结
Managing user groups enables better organization, security, and resource access on Linux systems, and mastering these commands is essential for both personal and enterprise environments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
