Do you need to add users to groups on your Linux PC or Linux server? Don’t understand how the group system works and need some guidance? We can help! Follow along with this guide as we show you how to add users to groups with the “usermod” tool on Linux!
Need to view all users on a Linux system? Follow this guide.
Adding users to existing groups with usermod
If you need to add a user to an existing group on your Linux PC, the first step is to list the groups on the system. You can find out if the group you expect to add the user to is on the system and how it is spelled by listing groups.
To view a list of existing groups on your Linux PC, execute the cut command. This command will print out every group you have into the terminal, and you can use it to locate the group you want to add users to with it.
cut -d: -f1 /etc/group
If there are a whole lot of groups and you want an easier way to sort through them, try running it with the less command.
cut -d: -f1 /etc/group | less
When you’ve found the existing group, you wish to add your user to using the usermod -a -G command. This command will allow the specified user account to be bound to any group you want to.
For example, to add the user addictivetips to the group “tech,” you could enter the usermod -a -G command with the “tech” group to add the user to the group.
sudo usermod -a -G tech addictivetips
The usermod command isn’t limited to a single group. Users can add a single user to many groups at a time in the same command. For example, if you wish to add the “addictivetips” user to the groups “tech,” “video,” and “sound,” you can by simply listing all groups before the username. There isn’t a limit to how many groups can be added to a user at a time.
sudo usermod -a -G tech video sound addictivetips
To add your own groups to a user, take the usermod -a -G command above and change the examples to fit your use case. Be sure to do this for each user you wish to add to existing groups on Linux!
Adding users to new groups with usermod
Along with adding users to existing groups, adding users to new groups using the usermod command is also possible. To start, you’ll need to create a new group. To create a new group, you will need to use the groupadd command.
To create a new group, open up a terminal window. To open up a terminal window on the Linux desktop, press Ctrl + Alt + T on the keyboard. Or, search for “Terminal” in the app menu and use it that way.
With the terminal app open and ready to use, run the groupadd command to create your new group. The group can be named anything you like. However, be sure you know why you’re making the group and that it has a function. Otherwise, you may accidentally delete your group at a later date without understanding why.
sudo groupadd mynewgroup
Upon entering the groupadd command, your Linux system will likely ask you for your password. You will need to enter your password. Using the keyboard, enter your user account password and press the Enter key to continue.
Once you’ve created your new group, run the cut command to view your new group. It’s very important to confirm that the new group you’ve just created exists on the system. If it doesn’t show up, you won’t be able to add a user to it, and the group will need to be made again.
To view all groups on your Linux system, run the following cut command, and add on the grep command to filter through the long list of existing groups for the one you created.
sudo cut -d: -f1 /etc/group | grep mynewgroup
The cut command will return your group name if it has been made successfully. If it hasn’t, nothing will happen with the command run.
Once you’ve confirmed that your new group is on your Linux system, you can assign a user to it. For example, add the “addictivetips” user to the “mynewgroup” we just created and execute the following usermod command below.
sudo usermod -a -G mynewgroup addictivetips
Remove users from groups
If at any time you need to remove a Linux user from a group, you can make use of the gpasswd command. For example, to remove the “addictivetips” user from the “mynewgroup” group, you’d execute the command below.
sudo gpasswd -d addictivetips mynewgroup
Enter the command above any time you need to remove a Linux user from a group on the system.