How to Create a New User Group in Linux

In a Linux environment, managing permissions for individual users can become a nightmare. If you have five developers who all need read and write access to the /var/www/html web directory, manually modifying the permissions for each specific user every time a new file is created is highly inefficient.

The standard Linux solution is to create a User Group. You assign the permissions to the group itself, and then simply add the developers to that group. If a developer leaves the company, you just remove them from the group, and their access is instantly revoked.

Step 1: Create a New Group

To create a new group, you must use the groupadd command. Because creating groups alters the system configuration, you must execute this command with sudo (root privileges).

Open your terminal and type:

sudo groupadd developers

This command creates a new group named “developers”. It will run silently; if it works successfully, it will simply drop you back to the next prompt without outputting any text.

Step 2: Verify the Group Was Created

Linux stores all group information in a simple text file located at /etc/group. You can verify your new group exists by searching that file using the grep command.

grep developers /etc/group

The terminal should output something like: developers:x:1005:, which confirms the group is active and has been assigned a unique Group ID (GID).

Step 3: Add Existing Users to the Group

Now that the group exists, you need to put users inside it. You use the usermod (user modification) command for this.

It is critical that you use two specific flags: -a (append) and -G (groups). If you forget the -a flag, Linux will violently remove the user from all of their other existing groups, which will break their account.

To add a user named “sarah” to the “developers” group, run:

sudo usermod -aG developers sarah

Step 4: Apply Group Permissions to a Folder

Now you have a group full of developers. You need to give that group ownership of the web directory so they can actually do their work.

Use the chown (change owner) command. By placing a colon (:) before the group name, you tell Linux to change the group ownership, not the user ownership.

sudo chown :developers /var/www/html

Finally, ensure the group actually has write permissions to that folder using chmod:

sudo chmod 775 /var/www/html

Now, any user placed into the “developers” group can freely edit files in the web directory.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.