Operations 7 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux User Group Management: Create, Modify, Delete, and Assign Users

创建用户组

1 创建用户组

Use the groupadd command followed by the desired group name.

sudo groupadd mygroup

2 指定组ID

Option -g allows you to explicitly set the Group ID (GID); otherwise the system assigns the next available GID.

sudo groupadd -g 1001 mygroup

3 验证用户组

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 oldgroupname

2 修改用户组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 username

2 从组中删除用户

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

sysadminusermodgroupadduser-groups
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.