How to Add User to Group on Ubuntu Linux (Usermod). In this post, we introduce a group accounts and explain how to add a user to a group with examples.
Linux is a Unix like computer operating system assembled under the model of free and open source software development and distribution. The operating system provides a lot of flexibility and freedom.
A group account is a collection of users that helps the system administrator to manage users. The main purpose of a group is to define a set of privileges, such as read, write, or execute privileges, for a given resource, that is shared between users in a group. In Linux, a group is a unit where privileges for multiple users are managed at once. It is installed on different kinds of hardware and can be customized to suit the needs of the user.
You can use the useradd -g command if the user that you wish to add to the group does not yet exist. If you want to add him to more than one group, you use the usermod command. The usermod command uses -append and -group options to append a user to a specific group.
For example, create the group, add users, then change the ownership of a file or directory to the new group. You also make the default group something else with -g, but keep in mind that on a multi-user system, using the default of just one group is fine (e.g. You could also add the user to multiple groups simultaneously, by using commas to separate group names.)
Let’s continue with How to Add User to Group on Ubuntu Linux (Usermod).
There are two types of groups in Linux, Primary groups (default group that belongs to a user) and Secondary groups (added after the creation of a user account).
Group Account is a Linux feature that allows you to set up a group of users and control their access to the computer. This is done by editing the account’s password file, /etc/passwd, and adding an entry for the new user in it. The user can then be added to the appropriate groups using either the command line or GUI tools.
Users may be assigned to groups that are created for an account with shared privileges, security, and access. Groups help to determine what privileges and access a Linux user account has to files, folders, settings, and more. Being a member of a group gives him/her specific access to system resources, like files, directories, or processes (programs) running on the Linux system.
A parent group is created that has the user’s name, and any files created by him/her are included in this group. When creating a user using the adduser command, it is automatically assigned to the primary group with the same name as the user. The useradd command allows him/her to be created, and using the -g switch, adds the user to a group.
If you add new software to a typical Linux system, it creates more groups, since software vendors make use of both user and group accounts to allow controlled access to files that are part of the software. Using groups, you define which users are allow to read, write, or execute specific resources on the Linux machine.
We have reached the main part of this article How to Add User to Group on Ubuntu Linux (Usermod).
How to Add User to Group on Ubuntu Linux (Usermod)
In this section, we show you how to add an existing user to a group. We will also explain how to create a new one and add it to a specific group in Linux.
Basic Syntax of usermod Command
usermod is a Linux command line tool that allows you to add a user to a group account in Linux. The basic syntax of the usermod command is shown below.
usermod [options] [group-name] [user-name]
A brief explanation of each option is shown below.
You also add an existing user to multiple secondary group accounts. For example, to add an account john to multiple groups named account and hr, run the following command.
usermod -a -G account,hr john
Remove a User From a Group
You can use the gpasswdcommand to remove a user from the group account. For example, to remove an account named john from the account group, run the following command.
By default, there are multiple built in groups in Linux operating system. You get a list of all groups available in your system using the following command.
getent group
This command shows you all groups in the following output.
If you want to display groups for a different account, run the following command.
groups fred
You should see the freds group information in the following output.
fred : users sudo www-data
How to Remove a Group Account
If you want to delete a group account in Linux, you can use the groupdel command. For example, to delete a group namedhr from your system, run the following command.
groupdel hr
Thank you for reading How to Add User to Group on Ubuntu Linux (Usermod). We shall conclude.
How to Add User to Group on Ubuntu Linux (Usermod) Conclusion
In this post, we explained how to add an account to a group in Linux. Now, you follow the above guide and get your hands dirty to get enough understanding of groups in Linux. You also refer to the man page of each command for more information on all commands we use in this guide. The above commands work on many Linux distributions, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
To read more of Ubuntu/ Linux content, please navigate to our blog over here.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.