How to Add User into Group in Linux

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.

READ  How to Creates the /etc/httpd/passwords File and Configures a Password for Apache User?

1. Add existing user (demoroot) into root group. As state above, the group for demoroot user is only as below :

    demoroot:x:503:
    
    [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:
    
Leave a Reply

Your email address will not be published. Required fields are marked *

SiteGround.com

A world leading hosting company that provides fully-managed innovative and secure solutions, suitable for hosting small to medium-sized websites

Built on the best available technologies combined with Google Cloud for strong redundancy and application availability. Backed by skilled experts to address web security threats, a devops team to create advanced custom security solutions, and 24/7 sysadmins to watch over the platform. This powerful, hands-on approach makes your sites faster, safer, and easier to manage. Starting from only $3.95/mo.

TRY FREE

* up to 30 days money back guarantee