How to Add User to Group on Ubuntu Linux (Usermod)

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).

What is Group Account in Linux?

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.

  • -h: Display help information.
  • -g: Change user’s primary group.
  • -G: Change user’s secondary group.
  • -a: Add a user to new group.

How to Create a Group Account

The basic syntax to create a group account is shown below.

				
					groupadd group-name
				
			

For example, to create a new group named account and hr, run the following command.

				
					groupadd account
groupadd hr
				
			

Add an Existing User Account to a Group

You can use the -a and -G options with usermod command to add an existing user to a new group.

For example, to add john to a group account, run the following command.

				
					usermod -a -G account john
				
			

It is a good idea to use -a option when you add an existing one to a new group. Otherwise, the user is remove automatically from the previous group.

To verify the user’s group, run the following command:

				
					id john
				
			

You should see the user’s group information on the following screen.

Change User’s Primary Group

A user account has two types of groups, primary and secondary. If you want to change the primary group of users, use the -g option.

				
					usermod -g group user
				
			

Create a New User and Add It to a Group

Also create a new user and assign it to the primary and secondary groups using a single command. This is achieved using the useradd command.

Let’s create a account named fred with primary group users and secondary groups sudo and www-data.

				
					useradd -g users -G sudo,www-data fred
				
			

Now, verify the user’s group with the following command:

				
					id fred
				
			

You should see the user group information in the following output.

In the above output, you should see that the primary group of the fred is users and the secondary groups are sudo and www-data.

Add a User to Multiple Groups

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 gpasswd command to remove a user from the group account. For example, to remove an account named john from the account group, run the following command.

				
					gpasswd -d john account
				
			

View All Groups in Your System

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.

Also see all groups using the following command.

				
					cat /etc/group
				
			

This shows you a list of all groups.

To see the group information of the current login user, run the following command.

				
					groups
				
			

You should see the following output.

				
					john account hr
				
			

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 named hr 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

Avatar for Hitesh Jethva
Hitesh Jethva

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x