How To Add A User To A Group?
In Linux users may have different groups registered. By default adding a user to the Linux system will create new user. There are different commands to change user group one of them is
useradd which actually create new user and sets given group to the newly created user. As always we need root privileges to change user group.
Before starting to add user into a group we will list currently existing user accounts. There are a lot of different ways to list user accounts but I like to use following command.
$ cat /etc/passwd | cut -d ':' -f 1
We will also list currently existing groups which is stored in a file
$ cat /etc/group | cut -d ':' -f 1
Change Primary Group
Linux users may have more than one group. We call them primary and other groups. We will change user test primary group to mysql. Primary group is used to assign group to the files and directories user created.
In this example we will change user test primary group to the test
$ usermod -g mysql test
- usermod command will mdify usergroup
- -g mysql provides group parameter named mysql
- test is the user the group will be changed.
Add Other Groups
We can add user to the other groups. In this example we will add user test to the group root. Group root is not primary group of the test . We will use
-G option in order to specify other group.
$ usermod -G root test
- usermod is command that will change other group memebership
- -G root provides change for test user other group
- test is username
Add To Multiple Groups
Some times we may need to add single user to the multiple groups in a single command. We can use
-G options with multiple group names like below. the group names are delimited with command. In this example we will add
to the groups namedroot
$ usermod -G root,mysql test
- usermod is command
- -G root,mysql will change test user group memberships