Full width home advertisement

Post Page Advertisement [Top]

 Add a user to a group (or other group) on Linux


Changing the group a user is associated with is a fairly simple task, but not everyone knows the commands, especially adding a user to a secondary group. We go through all the scenarios for you.

User accounts can be assigned to one or more groups on Linux. You can configure file permissions and other privileges by group. For example, on Ubuntu, only users in the sudo group can use sudocommands to obtain elevated permissions.

Add a new group

GENEALOGY: What's the difference between Sudo and Su in Linux?

To create a new group on your system, use groupadd command after command, and replace new_group with the name of the group you want to create. You must also use sudo with this command (or on non-use Linux distributions sudo, you must run the sucommand alone to obtain elevated permissions before running the command).

sudo groupadd mynewgroup

Add an existing user account to a group

To add an existing user account to a group on your system, use usermod command, replace examplegroupwith the name of the group you want to add the user exampleusername with the name of the user you want to add.

usermod -a -G examplegroup exampleusername

For example, to add the user geekto the group sudo, use the following command:

usermod -a -G sudo geek

Change the primary group of a user

While a user account can be part of multiple groups, one of the groups is always the "primary group" and the others are "secondary groups". The user's login process and files and folders the user creates will be assigned to the primary group.

To change the primary group a user is assigned, run usermod command, replace examplegroup with the name of the group you want to be primary and exampleusernamewith the name of the user account.

usermod -g groupname username

Notice -ghere. When using lowercase letters g, assign a primary group. When using uppercase letters -G, as above, assign a new secondary group.

View the groups a user account is assigned to

To see the groups the current user account is assigned to, run groups command. You will see a list of groups.

groups

To see the numeric IDs associated with each group, run id command instead:

id

To see the groups another user account is assigned to, run the groupscommand and specify the name of the user account.

groups exampleusername

You can also see the numeric IDs associated with each group by running the id command and specifying a username.

id exampleusername

The first group in the groupslist or the group that appears after “gid =” in the idlist is the primary account of the user account. The other groups are the secondary groups. So in the screenshot below is the primary group of the user account example.

Create a new user and assign a group in a command

Sometimes you may want to create a new user account that has access to a specific resource or directory, such as a new FTP user. You can specify the groups a user account is to be assigned while creating the user account with a useradd command, such as:

useradd -G examplegroup exampleusername

For example, if you want to create a new user account named jsmith and assign that account to the ftp group, you will run:

useradd -G ftp jsmith

You will assign a password for that user afterwards, of course:

passwd jsmith

Add a user to multiple groups

While assigning the secondary groups to a user account, you can easily assign multiple groups at once by separating the list with a comma.

usermod -a -G group1,group2,group3 exampleusername

For example, if you want to add the user named geek to the ftp, sudo, and sample groups, you will run:

usermod -a -G ftp,sudo,example geek

You can specify as many groups as you want - just separate them all with commas.

View all groups on the system

To see a list of all the groups on your system, you can use the getent command:

getent group

This output also shows you which user accounts are members of which groups. So on the screenshot below we can see that the user accounts syslog and chris are members of the admin group.

It should cover everything you need to know about adding users to groups on Linux. 

No comments:

Post a Comment

Bottom Ad [Post Page]