Ubuntu is a popular distributions used by a lot of companies as Desktop and Server. As a system administrator one of the most recurring job is changing existing user group. In this tutorial we will look how to accomplish this task and some helper commands about this.
Primary group specifies the user first group which will be used in file creation and other operations. For example if a user creates a file the group of the file will be set to the user primary group.
Secondary group is generally used for permission related operations. For example if we want to a user to access network sniffing device or features we need to add required group to the user.
Print Given User Groups
Before doing any add or remove operation we generally prefer listing given user primary and secondary groups. Group information is stored in file
/etc/group and we will filter it with user name
ismail like below.
$ grep ismail /etc/group
List User Primary Group Information with id Command
id command which can be used to print user related information. This will list userid, group id (guid) and secondary groups of the user.
Change User Primary Group
We can change user primary group with
usermod command and
-g option. As there is only single primary group for each user. We will change user ismail primary group to the
root with the following command.
$ sudo usermod -g root ismail
Add User To The Secondary Group
We can change this group with
usermod command by using
-a option. In this example we will add primary group of user
$ sudo usermod -a -G root ismail