Ugacomp

How to add a user to a group in Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Adding a user to a group allows you to control access to files, directories, and resources. In this article, we will explore how to add a user to a group in Linux, along with examples demonstrating various permissions.

Checking Existing Groups

Before adding a user to a group, it’s essential to know which groups already exist on the system. You can list all groups using the following command:

$ cat /etc/group

This command displays a list of groups along with their associated information.

Adding a User to an Existing Group

To add a user to an existing group, you can use the usermod command. For example, to add a user named “john” to the “developers” group, use the following command:

$ sudo usermod -aG developers john

Here, the -aG options stand for append and group, respectively. This ensures that the user “john” is added to the “developers” group without removing them from their existing groups.

Creating a New Group

If the group to which you want to add a user doesn’t exist, you can create a new group using the groupadd command. For instance, to create a group called “admins,” use the following command:

$ sudo groupadd admins

Assigning Specific Permissions

Linux groups can be associated with specific permissions using file system access control mechanisms. Let’s explore how to grant read, write, and execute permissions to a group.

Granting Read Access

To give the “developers” group read access to a file or directory, you can use the chmod command. For example:

$ chmod g+r file.txt

This command grants read access to the group associated with “file.txt.”

Granting Write Access

Similarly, to grant write access, use the following command:

$ chmod g+w directory/

This command gives the group write permissions for the specified directory.

Granting Execute Access

For execute permissions, use the chmod command with the execute flag:

$ chmod g+x script.sh

This example allows the group to execute the specified script.

Verifying Group Membership and Permissions

To confirm the user’s group membership and associated permissions, you can use the id command. For instance:

$ id john

This command displays information about the user “john,” including their group memberships.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.