How to add/change a user to a group in Linux operating system ?

How to add/change a user to a group in Linux operating system ?

Changing the group of a user is quite easy and simple task, but it is not known to every user. In this topic we will tell you the commands through which not only you can add new user to a group but also change existing user's group too.
The basic commands used are "useradd" and "usermod". useradd command is used to create a new user or change user information. Whereas, usermod command modifies an user account and add user to existing groups.
There are two types of groups listed under Linux operating systems.
1). Primary user group.
2). Secondary user group.

While reading this a question can be arised in your mind that where is user account related informations are stored ?
>> Here is the answer, User related informations are stored under various files which are listed under :
/etc/passwd – Contains one line for each user account.
/etc/shadow – Contains the password information in encrypted format for the system’s accounts and optional account aging information.
/etc/group – Contains the information which defines the groups on the system.
/etc/default/useradd – Contains a value for the default group, if none is specified by the useradd command.
/etc/login.defs – Defines the site-specific configuration for the shadow password suite stored in /etc/shadow file.

Now lets start with adding a New Group to system :

To add a new group, you can use the groupadd command:
-----
# groupadd <groupname> -- Syntax
# groupadd developer -- Example

-----

To add an Existing User to a Group we can use usermod command :
-----
# usermod -a -g <groupname> username --Synatx
# usermod -a -g developer rajan --Example

-----

If we need to change a User’s Primary Group :
Sometimes we need to change the primary group that a user is assigned to, we can do with this command:
-----
# usermod -g <groupname> username --Synatx
# usermod -g designers akhilesh --Example

-----

To View a User’s Group information :
If you’re trying to figure out a permissions issue, you’ll want to use the id command to see what groups the user is assigned to:
-----
# id <username> --Synatx
# id bhawani --Example

-----

This will display output which look something like this:
-----
uid=300(bhawani) gid=300(bhawani) groups=300(bhawani), 1356(writers)
-----
You can also use the groups command instead of id command:
-----
# groups <username> --Synatx
# groups bhawani --Example

-----

To view a List of All Groups on the system:
-----
# groups
-----

To Add a New User and Assign a Group in One Command
-----
# useradd -g <groupname> username --Syntax
# useradd -g designers kumkum --Example

-----

To assign a password for that user:
-----
# passwd username --Synatx
# passwd hunter --Example

-----

To Add a User to Multiple Groups:
You can add user to more than one group by separating them by commas in line.
-----
# usermod -a -g group1,group2,group3 <username> --Synatx
# usermod -a -g admins,ftp,developer hoststud --Example

-----
Author
bhawanisingh
Views
2,748
First release
Last update
Rating
0.00 star(s) 0 ratings
Top