Showing posts with label remove group of a user. Show all posts
Showing posts with label remove group of a user. Show all posts

Thursday, May 16, 2013

Add / Remove user to an existing Group in Linux

Users & Groups in Linux : ( Add & Remove )

All Groups of unix system are listed in the file /etc/group
To see the list of all groups of system
$ cut -d: -f1 /etc/group

See all Groups of a user :
$ id -nG username
Suppose I want to see all Groups of user Thomas whose username is thomas :
$ id -nG thomas
The output is name of groups of thomas.
user1 root admin sambashare vboxusers usbuser mysql apache2
Here user1 is the Primary Group (First Group) & others are secondary Groups.
You can use following command too to see groups of a user.
$ groups username
The first group after colon is Primary Group.



Add an existing Group to user (Other groups should not be removed) :
$ usermod -a -G groupname username
Suppose I want to add a new group to user thomas, other groups should not be removed.
$ usermod -a -G newgroup thomas
Now an existing group newgroup has been added to user thomas. To see the changes, Run
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup



Add an existing Group to user (other groups should be removed, only Primary Group and currently added Group should be available) :
$ usermod -G groupname username
Suppose I want to add new group to user thomas, other groups should be removed. Only Primary Group and currently added group should be assigned to user.
$ usermod -G grp thomas
Now an existing group grp has been added to user thomas. All other secondary groups have been removed from thomas
To find current groups of user thomas
$ id -nG thomas
The output is
user1 grp
All other secondary groups are replaced with grp. Now there is only one secondary group for thomas.




Remove Group of a user :
You can remove only secondary groups of a user. Primary Group can't be removed by this command. If you want to remove primary group, you have to assign a new primary group for user. A user can't be modified without primary group.
$ gpasswd -d username groupname
Suppose I want to remove a secondary group apache2 from user thomas.
Right now there are following Primary Group & Secondary Groups of user thomas.
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I delete apache2
$ gpasswd -d thomas apache2
See the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql newgroup
apache2 has been removed for thomas.



Change Primary Group of a user :
$ usermod -g groupname username
It removes the current Primary Group of the user and replaces with new added primary group. Secondary groups are as it is. There is no change in Secondary Groups.
Suppose I want to change primary group of user thomas.
To see the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I run following command to change Primary Group
$ usermod -g newgroup2 thomas
Now Primary Group user1 has been removed from thomas and it is replaced with newgroup2. Now newgroup2 is primary group of thomas.
To verify the changes, I see the groups list.
$ id -nG thomas
The output is
newgroup2 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now the Primary Group is newgroup2.