There are two ways to add user into existing group which are useradd and usermod. The useradd command is to create a new user or update default new user information. Meanwhile the usermod command is to modify a user account i.e. it is useful to add existing user to existing group.
In Linux, there are two types of group that we can specify which are primary and secondary. Primary group is the default group of the user and the group name should equal as the username. If the username is demoroot, then the primary group should be demoroot as well
[root@server ~]# useradd demoroot [root@server ~]# grep demoroot /etc/group demoroot:x:503:
And the secondary group is the second group of the user that we can add either using useradd or usermod . All user account related information is stored in /etc/passwd, /etc/shadow and /etc/group files to store user information.
1. Add existing user (demoroot) into root group. As state above, the group for demoroot user is only as below :
[root@server ~]# usermod -G root demoroot [root@server ~]# grep demoroot /etc/group root:x:0:root,demoroot demoroot:x:503:
This usermod command with -G option modify a demoroot account, and add demoroot into existing root group.
2. Add new user into Existing group.
[root@server ~]# useradd -G root testroot [root@server ~]# grep testroot /etc/group root:x:0:root,demoroot,testroot testroot:x:504:
This useradd command with -G option has created a new user, then add into existing root group
[root@server ~]# grep root /etc/group root:x:0:root,demoroot,testroot bin:x:1:root,bin,daemon daemon:x:2:root,bin,daemon sys:x:3:root,bin,adm adm:x:4:root,adm,daemon disk:x:6:root wheel:x:10:root demoroot:x:503: testroot:x:504: