How to add multiple users to a group at once in linux?

Configurare noua (How To)

Situatie

Managing user groups in Linux is an essential part of system administration. Often, you’ll find the need to add multiple users to a specific group simultaneously.

Solutie

Pasi de urmat

Using the `usermod` Command

The `usermod` command is a powerful utility for modifying user accounts, including adding users to groups. To add multiple users to a group using usermod, follow these steps:

sudo usermod -aG group_name user1 user2 user3
  • `sudo`: This command is typically executed with superuser privileges, as modifying user accounts requires administrative access.
  • `usermod`: This is the command for modifying user accounts.
  • `-aG`: These options specify that we are adding users to groups. `-a` stands for “append,” and `-G` indicates the group name.
  • `group_name`: Replace this with the name of the group you want to add users to.
  • `user1 user2 user3`: List the usernames of the users you want to add to the group, separated by spaces.

Let’s say you have a group named “developers,” and you want to add users “alice,” “bob,” and “charlie” to this group:

sudo usermod -aG developers alice bob charlie

This command appends (“-a”) the users “alice,” “bob,” and “charlie” to the “developers” group.

Editing the /etc/group File

The `/etc/group` file contains information about all user groups on the system. You can manually edit this file to add users to a group. However, this method is less recommended than using `usermod` because it requires more precision and can lead to errors if not done carefully.

To add users manually to a group using the `/etc/group` file:

Open the `/etc/group` file with a text editor using root privileges, such as:

sudo nano /etc/group

Locate the group you want to modify (e.g., “developers”).

Append the usernames of the users you want to add to the group, separated by commas, after the group name. For example:

developers:x:1001:alice,bob,charlie

Save and exit the file.

Tip solutie

Permanent

Voteaza

(3 din 4 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?