How to add a group in Linux

How to add a group in Linux

Into this tutorial we will learn how to add group in Linux using command line mode with different options and commands.

Groups

  •             Users are assigned to groups with unique group ID numbers (the GID)
  •             The group name and GID are stored in /etc/group
  •             Each user is given their own private group
  •             They can also be added to their groups to gain additional access
  •             All users in a group can share files that belong to the group



Each and every user is a member of at least one group, called a primary group. In addition, a user can be a member of an unlimited number of secondary groups. Group membership can be used to control the files that a user can read and edit. For example, if two users are working on the same project you might put them in the same group so they can edit a particular file that other users cannot access.
A users primary group is defined in the /etc/passwd file and Secondary groups are defined in the /etc/group file.

The primary group is important because files created by this user will inherit that group affiliation.

Create a Group with default options:
To  create a group the sysntax is

# groupadd < name for the group>
#groupadd linuxelearn

And verify it in /etc/group file with tail /etc/group command.


[root@linuxelearn ~]# groupadd linuxelearn
[root@linuxelearn ~]# tail /etc/group
user:x:500:
nagios:x:501:
nagcmd:x:502:apache
rahul:x:503:
ram:x:504:pooja
rpgroup:x:505:rahulp,pooja
ktg:x:506:
admin:x:507:nagios
pooja:x:508:
linuxelearn:x:509:
[root@linuxelearn ~]#


Create a group with user specified group id (GID)

# groupadd <option> <name for the group>
# groupadd –g 595 linuxelearn

Verify it in /etc/group file


[root@linuxelearn ~]# groupadd -g 510 linuxelearn1
[root@linuxelearn ~]# tail /etc/group
nagios:x:501:
nagcmd:x:502:apache
rahul:x:503:
ram:x:504:pooja
rpgroup:x:505:rahulp,pooja
ktg:x:506:
admin:x:507:nagios
pooja:x:508:
linuxelearn:x:509:
linuxelearn1:x:510:
[root@linuxelearn ~]#


Modifying the properties of the group

To modify the group properties the syntax is

#groupmod <option> <arguments> <group name>

The options are
  •             -g to change the group id
  •             -o to override the previous assigned id, if it matches with the new one.
  •             -n to change the group name

Change the GID of the group
  •             #groupmod –g 600 linuxelearn
  •             Verify it in /etc/group


[root@linuxelearn ~]# groupmod -g 570 linuxelearn
[root@linuxelearn ~]# tail /etc/group
nagios:x:501:
nagcmd:x:502:apache
rahul:x:503:
ram:x:504:pooja
rpgroup:x:505:rahulp,pooja
ktg:x:506:
admin:x:507:nagios
pooja:x:508:
linuxelearn:x:570:
linuxelearn1:x:510:
[root@linuxelearn ~]#


Changing the name of the Group
            The syntax for changing the group name is

            #groupmod –n <new name> <existing name>
            #groupmod –n linuxelearn rjgroup


[root@linuxelearn ~]# groupmod -n rjgroup linuxelearn
[root@linuxelearn ~]# tail /etc/group
nagios:x:501:
nagcmd:x:502:apache
rahul:x:503:
ram:x:504:pooja
rpgroup:x:505:rahulp,pooja
ktg:x:506:
admin:x:507:nagios
pooja:x:508:
linuxelearn1:x:510:
rjgroup:x:570:
[root@linuxelearn ~]#



In this tutorial we cover all the options of adding a group in Linux. 




Post a Comment