Home / Knowledge Base / How to / How to Add a User to a Linux Group

How to Add a User to a Linux Group

Table of Contents

Managing user permissions in Linux can be simplified by using groups. By adding users to specific groups, you grant them predefined permissions, which can help streamline your system administration tasks. In this comprehensive guide, we’ll walk you through how to add a user to a Linux group using only the terminal.

1. Introduction to User Groups in Linux

In Linux, groups are used to manage user permissions. Instead of assigning permissions to each user individually, users can be added to groups that have certain permissions. This method simplifies the management of user privileges.

2. Prerequisites

Before you start, make sure you have:

  • A Linux system
  • A user account with sudo or root privileges
  • Access to a terminal window (Ctrl-Alt-T or Ctrl-Alt-F2)

3. Understanding Primary and Secondary Groups

Primary Group

A primary group is the main group associated with a user. Any files created by the user are automatically associated with this group. A user can belong to only one primary group at a time.

Secondary Groups

Secondary groups provide additional permissions. A user can belong to multiple secondary groups, which allows for more flexible permission management.

4. Creating a New User Group

To create a new group, use the `groupadd` command followed by the name of the group. For example:

sudo groupadd developers

This command creates a new group called “developers”.

5. Adding Users to a Group

You can add an existing user to an existing group using several commands.

Using the `usermod` Command

The `usermod` command can add a user to a group without removing them from other groups. Use the `-aG` options for append and group:

sudo usermod -aG developers username

Replace `developers` with the group name and `username` with the user’s name.

Example:

sudo usermod -aG developers alice

6. Adding Users to Multiple Groups

To add a user to multiple groups at once, list all the groups separated by commas:

sudo usermod -aG group1,group2,group3 username

Example:

sudo usermod -aG developers,admins,designers alice

7. Creating a New User and Adding to a Group

You can create a new user and immediately add them to a group using the `useradd` command:

sudo useradd -m -G groupname username

Example:

sudo useradd -m -G developers bob

Then set a password for the new user:

sudo passwd bob

8. Changing a User’s Primary Group

To change a user’s primary group, use the `usermod` command with the `-g` option:

sudo usermod -g new_primary_group username

Example:

sudo usermod -g developers alice

9. Removing a User from a Group

To remove a user from a group, use the `gpasswd` command:

sudo gpasswd -d username groupname

Example:

sudo gpasswd -d alice developers

10. Listing Groups in Linux

To view all groups on your system, display the `/etc/group` file:

cat /etc/group

To display groups a specific user belongs to:

groups username

Example:

groups alice

Another method to display the groups a user belongs to, including user ID (uid) and group ID (gid), is to use the `id` command:

id username

Example:

id alice

11. Common Linux Groups

  • sudo – Grants sudo (superuser) privileges.
  • wheel – Another method for granting sudo-like privileges.
  • cdrom – Allows mounting of optical drives.
  • adm – Allows reading of certain system log files.
  • lpadmin – Allows configuration of printers.
  • plugdev – Allows access to external storage devices.

12. Conclusion

By understanding and effectively using user groups in Linux, you can simplify user permission management and improve system security. Whether you’re adding users to existing groups or managing multiple group memberships, these commands and best practices will help you maintain an organized and efficient Linux environment.

G7 Acceleration Network

The G7 Acceleration Network boosts your website’s speed, security, and performance. With advanced full page caching, dynamic image optimization, and built-in PCI compliance, your site will load faster, handle more traffic, and stay secure. 

WordPress Hosting

Trusted by some of the worlds largest WooCommerce and WordPress sites, there’s a reason thousands of businesses are switching to G7