Add A User To A Group
Add A User To A Group
Add A User To A Group
Changing the group a user is associated to is a fairly easy task, but not everybody
knows the commands, especially to add a user to a secondary group. We’ll walk
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 the sudo command to gain elevated permissions.
For example, to add the user geek to the group sudo , use the following command:
Note the -g here. When you use a lowercase g, you assign a primary group. When you
use an uppercase -G , as above, you assign a new secondary group.
View the Groups a User Account is Assigned To
To view the groups the current user account is assigned to, run the groups command.
You’ll see a list of groups.
groups
To view the numerical IDs associated with each group, run the id command instead:
id
To view the groups another user account is assigned to, run the groups command and
specfy the name of the user account.
groups exampleusername
You can also view the numerical IDs associated with each group by running
the id command and specifying a username.
id exampleusername
The first group in the groups list or the group shown after “gid=” in the id list is the user
account’s primary group. The other groups are the secondary groups. So, in the
screenshot below, the user account’s primary group is example.
For example, to create a new user account named jsmith and assign that account to the
ftp group, you’d run:
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.
For example, to add the user named geek to the ftp, sudo, and example groups, you’d
run:
You can specify as many groups as you like—just separate them all with a comma.
getent group
This output will also show you which user accounts are members of which groups. So,
in the screenshot below, we can see that the user accounts syslog and chris are
members of the adm group.