A Guide to Adding Groups and Users in Linux

Linux logo

Managing groups is a core part of controlling who can do what on a Linux system, since groups let you hand a set of permissions to several users at once rather than one by one. This guide covers the everyday tasks: viewing group membership, creating a group, adding and removing users, and giving a group access to a directory.

Viewing Group Membership

To see which groups a specific user belongs to, pass their username to the groups command, replacing exampleuser with the name you want to check:

groups exampleuser

Run it with no argument to see the groups for your own current user:

groups

Creating a Group

Create a new group with groupadd, replacing sharedgroup with whatever you’d like to call it:

sudo groupadd sharedgroup

Adding a User to a Group

Add a user to a group with usermod. The -a is important, since it appends the group rather than replacing all the user’s other groups, and forgetting it is a common way to accidentally remove someone from everything else:

sudo usermod -a -G examplegroup exampleusername

Giving a Group Access to a Directory

To let a group share a directory, set the directory’s group ownership with chown. The -R applies it recursively to everything inside:

sudo chown -R :examplegroup sharedfolder/

Removing a User from a Group

To take a user back out of a group, use gpasswd with the -d flag:

sudo gpasswd -d testuser testgroup

With these few commands you can organise users into groups and manage shared access cleanly, which is far easier to maintain than setting permissions on individual users one at a time.

Related guides


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *