Valeriy Artamonov

User accounts on Linux can be assigned to one or more groups, allowing you to configure file permissions and other privileges based on groups. For instance, on Ubuntu, only users in the “sudo” group can utilize the “sudo” command for elevated permissions.

To add an existing user account to a group, employ the “usermod” command with the “-a -G” options. You should write firstly group name and then the user name you wish to add.

How to add an existing user to a group

In Linux, you have the ability to add an existing user account to a group. This allows you to assign additional permissions and privileges to the user, granting them access to specific resources and functionalities.

To add an existing user to a group, you can use the “usermod” command.

usermod -a -G exgroup exuser

Replace “exgroup” with the name of the group you want to add the user to, and ” exuser” with the actual username of the existing user account.

For example, to add the user “testuser” to the “mynewgroup” group, you would run the following command:

usermod -a -G  mynewgroup  testuser 

The “-a” flag ensures that the user is appended to the group without removing them from any existing groups, while the “-G” flag specifies the group to which the user is added.

Once the command is executed, the user will be added to the specified group and inherit the permissions and privileges associated with that group.

Adding an existing user to a group is beneficial when you need to grant additional privileges or access to specific resources. For example, adding users to the “sudo” group allows them to use the “sudo” command to perform administrative tasks.

Remember to use the “sudo” command before executing the “usermod” command to ensure you have the necessary administrative privileges to modify user accounts.

How to add a New Group

In Linux, you can create new groups to organize and manage user accounts with shared permissions and privileges. Creating a new group allows you to define specific access levels and control permissions for a set of users.

To add a new group to your Linux system, you can use the “groupadd” command.

Open a terminal on your Linux system.

Use the following syntax to create a new group:

sudo groupadd groupname

Replace “groupname” with the desired name for the new group.

For example, to create a new group called “mynewgroup”, you would run the following command:

sudo groupadd mynewtestgroup

Using the “sudo” command ensures you have administrative privileges to create the group.

Creating a new group allows you to group users together and define common permissions and access controls. You can later assign individual users to this group using the “usermod” command described in the previous sections.

How to change a user’s primary group

While a user account can belong to multiple groups, one group is designated as the primary group, and others are secondary groups. The primary group is responsible for the user’s login process and ownership of files and folders created by the user.

Use the following syntax to change the primary group of a user:

sudo usermod -g groupname username

Replace “groupname” with the name of the group you want to set as the primary group, and “username” with the actual username of the user account.

For example, to change the primary group of the user “exampleuser” to “examplegroup,” you would run the following command:

sudo usermod -g examplegroup exampleuser

You should notice that the lowercase “g” is used to designate the primary group, and the uppercase “-G” is used to designate the secondary groups.

How to view group assignments for a user account

To view the groups associated with a specific user account on Linux, you can use the “groups” command. This command provides you with a list of groups to which the user belongs.

Simply open a terminal and enter the following command:

groups

This will display a list of groups separated by spaces. The first group in the list is the user’s primary group, while the rest are secondary groups.

If you want to see the numerical IDs associated with each group, you can use the “id” command instead:

id

The output will include the user’s UID (user ID) and GID (group ID), along with the list of groups they are assigned to.

In case you want to view the group assignments for a different user account, you can specify the username with the “groups” command:

groups username

Similarly, you can use the “id” command along with the username to see the numerical IDs associated with each group:

id username

By examining the output, you can easily identify the primary and secondary groups assigned to the user account. The primary group is denoted by the first group in the list or the group displayed after “gid=” in the “id” command output.

How to create a new user and assign a group simultaneously

When working with Linux, there may be occasions when you need to create a new user account and assign them to a specific group at the same time. This can be easily accomplished using the “useradd” command with the appropriate options.

To create a new user and assign them to a group in one command, you need open a terminal and use the following syntax to create a new user account and specify the group:

sudo useradd -G groupname username

Replace “groupname” with the name of the group you want to assign the user to and “username” with the desired username for the new account.

For example, to create a new user named “tom” and assign them to the group “guard”, you would run the following command:

sudo useradd -G mynewgroup  testuser1

Once you’ve created the user account, you will need to set a password for the new user. You can use the “passwd” command to do this:

sudo passwd username

Replace “username” with the actual username you specified during user creation.

For example, to set a password for the user “tom,” you would run:

sudo passwd testuser1

By combining the “useradd” command with the “-G” option, you can create a new user account and assign them to a specific group in a single step. This approach is particularly useful when you want to quickly create user accounts with predefined group assignments, such as granting FTP access to specific users.

How to Add User to Multiple Groups

In Linux, you have the flexibility to assign a user account to multiple groups simultaneously. This allows you to grant users access to various resources, privileges, and permissions associated with each group. To add a user to multiple groups, you can use the “usermod” command with the appropriate options.

To add a user to multiple groups, open a terminal on your Linux system. Use the following syntax to add a user to multiple groups:

sudo usermod -a -G group1,group2,group3 username

Replace “group1,group2,group3” with the names of the groups you want to add the user to, separated by commas. Also, replace “username” with the actual username of the user account you wish to modify.

For example, to add the user named “testuser1” to the groups “mygroup1”, “mygroup2”, and “mygroup3”, you would run the following command:

sudo usermod -a -G mygroup1,mygroup2,mygroup3 testuser1
  1. You can specify as many groups as needed; just separate them with commas.
  2. Once the command is executed, the user will be added to all the specified groups. They will inherit the permissions and privileges associated with each group.

Adding a user to multiple groups is useful when you want to grant the user access to various resources or provide different levels of permissions across different group memberships. For example, users may need access to shared files in one group and administrative privileges in another.

Remember to use the “sudo” command before executing the “usermod” command to ensure you have the necessary administrative privileges to modify user accounts.

LEAVE A REPLY

Please enter your comment!
Please enter your name here