Linux Add User To Group | How to Tutorial


The following tutorial Linux add user to group explains how to add a new or existing user to a new or existing group under Linux operating system using command line interface / SSH. First let me tell you that there are two categories of users in Linux OS i.e. primary and secondary. So I will explain add new and existing users to both primary and secondary group side by side. Here we go:-

  1. Linux add user to group (New User)
  2. Adding an existing user to any group

add user to group

LINUX ADD USER TO GROUP (NEW USER) TUTORIAL

linux add user to group centos

Under linux OS “useradd” or “usermod” command is used to add a user, so following syntax can be used to add a user to any group in linux.


usermod -a -G (groupname) username

For example if we need to add a new user arshadnew to group admin then following code will be used :-


usermod -a -G  admin arshadnew
passwd arshadnew
 Note:- passwd command is to assign password for user arshadnew.

Now ensure that the user arshadnew is properly added to the group admin by following command :-


id arshadnew
or
group arshadnew

HOW TO LINUX: ADD AN EXISTING USER TO GROUP

If we need to add an existing user to a group then we just need the first line of above code because we do not need to setup password for the user as the user is already existed.
For example : to add an existing user arshadold to group admin then following command will be used :-


usermod -a -G admin arshadold
 note:- the user is already existed so we do not need to setup password for that.

ADDING GROUP IN LINUX

Ensure that the group to which you are adding a user should be created first or already existing.
If you need to add a group then you can use following command to add a group in linux.


groupadd (groupname)

Hope this tutorial linux add user to group helps you in the best way, don’t forget to share, like, comment and follow me if you like the above tutorial.

SHARE