Linux adding existing user to existing group

How to Add User to Group in Linux

In this tutorial, we will explain how to add a user to a group in Linux systems. We will also show you how to remove a user from a group and how to create, delete, and list groups.

Linux Groups #

Linux groups are organization units that are used to organize and administer user accounts in Linux. The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.

There are two types of groups in Linux operating systems:

  • The Primary group – When a user creates a file, the file’s group is set to the user’s primary group. Usually, the name of the group is the same as the name of the user. The information about the user’s primary group is stored in the /etc/passwd file.
  • Secondary or supplementary group — Useful when you want to grant certain file permissions to a set of users who are members of the group. For example, if you add a specific user to the docker group, the user will inherit the group’s access rights and be able to run docker commands.

Each user can belong to exactly one primary group and zero or more secondary groups.

Only root or users with sudo access can add a user to a group.

How to Add an Existing User to a Group #

To add an existing user to a secondary group, use the usermod -a -G command followed the name of the group and the user:

sudo usermod -a -G groupname username

For example, to add the user linuxize to the sudo group, you would run the following command:

sudo usermod -a -G sudo linuxize

Always use the -a (append) option when adding a user to a new group. If you omit the -a option, the user will be removed from any groups not listed after the -G option.

Читайте также:  Linux cli find files

On success, the usermod command does not display any output. It warns you only if the user or group doesn’t exist.

How to Add an Existing User to Multiple Groups in One Command #

If you want to add an existing user to multiple secondary groups in one command, use the usermod command followed by the -G option name of the group separated by , (commas):

sudo usermod -a -G group1,group2 username

How to Remove a User From a Group #

To remove a user from a group, use the gpasswd command wit the -d option.

In the following example, we are removing the user username from the group groupname :

sudo gpasswd -d username groupname

How to Create a Group #

To create a new group , use the groupadd command followed by the group name:

How to Delete a Group #

To delete an existing group, use the groupdel command followed by the group name:

How to Change a User’s Primary Group #

To change a user primary group, use the usermod command followed by the -g option:

sudo usermod -g groupname username

In the following example, we are changing the primary group of the user linuxize to developers :

sudo usermod -g developers linuxize

How to Create a New User and Assign Groups in One Command #

The following useradd command creates a new user named nathan with primary group users and secondary groups wheel and developers.

sudo useradd -g users -G wheel,developers nathan

Display User Groups #

To display complete user information, including all the groups of which a user is a member of, use the id command followed by the username:

If you omit the username, the command will print the information about the currently logged-in user. Let’s check the user linuxize :

uid=1000(linuxize) gid=100(users) groups=100(users),10(wheel),95(storage),98(power),990(libvirt),993(docker),999(kvm) 

From the output above, we see that the primary group of the user is users and it belongs to wheel , storage , libvirt , docker , and kvm supplementary groups.

Use the groups command to display the user’s supplementary groups:

wheel storage power users libvirt docker kvm 

If no username is passed to the groups command, it will print the currently logged in user’s groups.

Conclusion #

In this tutorial, we have shown you how to add a user to a group.

The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.

Feel free to leave a comment if you have any questions.

Источник

10 practical examples to add or remove user from group in Linux

Linux add user to group or remove user from group

1. Create a new user and add to existing primary group

  • By default when we create a new user, a new primary group is created by the same name as of the user.
  • But we can also use useradd to create a user and add this user to any existing group
  • So this group will not become the primary group of your new user
Читайте также:  Linux find file status

In this example I will create a new group » admin «

# getent group admin admin:x:1003:

Next I will create a new user » user1 » and add this user to » admin » group using useradd -g

Verify the primary group of user1

# id user1 uid=1003(user1) gid=1003(admin) groups=1003(admin)

2. Create a new user and add to existing supplementary group

We want to create a new user and add him/her to supplementary group (please NOTE, we will add the user to supplementary group and not the primary group here)

I have below list of groups on my Linux server

# egrep 'admin|devops|qa_team' /etc/group admin:x:1003: devops:x:1004: qa_team:x:1005:

I will create a new user » user2 » and add this user to all these supplementary groups using useradd -G ,,..

# useradd -G admin,devops,qa_team user2

Verify the supplementary groups.

Please NOTE that since we did not specified primary group using -g , a new group user2 is created and assigned as primary group to the user

# id user2 uid=1003(user2) gid=1006(user2) groups=1006(user2),1003(admin),1004(devops),1005(qa_team)

3. Create a new user and add to existing primary and supplementary group

Now we will combine both the above arguments i.e. -g to add primary_group and -G to add supplementary_group
In this example i will create user3 with primary_group as admin and with supplementary_group of devops and qa_team

# useradd -g admin -G devops,qa_team user3

Verify the new user group details

# id user3 uid=1003(user3) gid=1003(admin) groups=1003(admin),1004(devops),1005(qa_team)

4. Change primary group of existing user

I have a user who is currently added to his own primary group

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4)

I will change the primary group of this user to admin using usermod

Verify the new primary group for user4

# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin)

5. Add user to Group (Supplementary or Secondary) using usermod

  • To add user to group we can use use usermod or gpasswd command
  • We can add user to supplementary groups only
  • In this example I will add user4 to devops supplementary group

Syntax to add user to group: usermod -G

# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin),1004(devops)

6. Add user to multiple groups (Supplementary or Secondary) using usermod

We can also add user to multiple supplementary groups using syntax usermod -G ,,..
In this example I will add user4 to multiple supplementary groups ( devops and qa_team )

# usermod -G devops,qa_team user4
# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin),1004(devops),1005(qa_team)

7. Add user to Group (Supplementary or Secondary) using gpasswd

  • Similar to usermod we can also user gpasswd to add user to group
  • The syntax to add user to group is gpasswd -M

In this example I will add user4 to devops as supplementary group

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1004(devops)

Alternatively you can also user gpasswd -a

In this example I will add user4 to qa_team as supplementary group

# gpasswd -a user4 qa_team Adding user user4 to group qa_team
# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1005(qa_team)

8. Add multiple users to same group

We need to user gpasswd to add multiple users to same group
Currently I already have admin group which does not contain any users at the moment

# egrep admin /etc/group admin:x:1003:

The syntax to add multiple users to single group would be gpasswd -M ,,..

In this example I will add my existing users i.e. user1 , user2 and user3 to admin as supplementary group

# gpasswd -M user1,user2,user3 admin

Verify the admin group details

# egrep admin /etc/group admin:x:1003:user1,user2,user3

9. Remove user from Group (Supplementary or Secondary)

Currently my user4 is part of three different supplementary groups

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1003(admin),1004(devops),1005(qa_team)

gpasswd is the best tool to remove user4 from qa_team group

# gpasswd -d user4 qa_team Removing user user4 from group qa_team

We can also use usermod command to remove user from group. The problem with usermod is that you must define the complete list of supplementary group which the user is part of and only remove the group which you wat to remove the user from.

For example, my user4 is part of devops , admin and qa_team . So to remove user4 from qa_team we will re-add user to group devops and admin (not to qa_team )

# usermod -G devops,admin user4
# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1003(admin),1004(devops)

I would recommend using gpasswd to remove user from group

10. Remove multiple users from supplementary group

There is no single command to remove multiple users from single group but we can use a small script
Currently I have admin which has three users

# egrep admin /etc/group admin:x:1003:user1,user2,user3

I will write a small script to remove all the 3 users from admin group

remove user from group

11. Remove user from all Groups (Supplementary or Secondary)

  • We can use gpasswd to remove user from group
  • But if a user is part of multiple groups then you need to execute gpasswd multiple times
  • Or write a script to remove user from all the supplementary groups
  • Alternatively we can use usermod -G «»

Currently my user4 is part of multiple supplementary groups

# groups user4 user4 : user4 admin devops qa_team

To remove user from all supplementary groups, use:

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4)
# groups user4 user4 : user4

Lastly I hope the steps from the article to add user to group, remove user from group and difference between primary group and supplementary group on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью
Adblock
detector