add user to group code example

Example 1: add user to group

usermod -a -G examplegroup exampleusername

Example 2: linux add user to group

# Change the primary group a user is assigned to, note the -g here.
# When you use a lowercase g, you assign a primary group. When you 
# use an uppercase -G you assign a new secondary group.
usermod -g groupname username

Example 3: linux add user to group

# add user to additional group

usermod -G groupname username

Example 4: how to create a user and add it to a group in linux

useradd -G examplegroup exampleusername

Example 5: how to set default user group in django

from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        instance.groups.add(Group.objects.get(name='group_name'))

Example 6: Adding users to groups

sudo usermod -a -G <groupname> <username>
e.g.
sudo usermod -a -G wonderland alice