Categories: Ubuntu

Ubuntu Add User to Group

In Linux, groups are the entities used for the organization and management of numerous user accounts simultaneously. Groups offer easy and quick administration of user accounts. Various Linux users have many different task assignments and roles. The key point of groups is the identification of a set of rights like write, read or execute access for a specified resource that can be mutually shared among the group users.

In this guide, we will discuss how many types of groups exist in the Ubuntu Linux system. Moreover, we will also elaborate on how to add a Ubuntu user into the groups.

Groups
Sponsored
in Ubuntu Linux

Ubuntu and many other Linux distributions have two different types of groups:

  • Primary group
  • Secondary group

Primary Group

As soon as the user creates the file, it is automatically added to the primary group. The primary group is similar to the user name. All primary group details about the users are saved in a file that is located at ‘/etc/passwd’.

Secondary Group

Secondary groups are significant for granting particular file access to the group members. They are made to administer individual files along with software applications. Group members inherit write, read and execute rights for that group.

Every user on a Linux system is a member of one primary group and in addition, this user can be included in none or more secondary groups. Users can be added as members of the group by merely root or users with sudo privilege.

Create a Group in Ubuntu

You can create a new group on the Ubuntu system by running the following command:

$ sudo groupadd test_group

In the above command, the ‘test_group’ is the name of the new group.

Adding an Existing User to a Group in Ubuntu

To add a user that is already created on your Ubuntu machine and you want to assign a group to this existing user, run the following command to do this:

$ sudo usermod -a -G groupname username

For example, the ‘sam-pc’ is existing on our system and we want to add this user to a group ‘test_group’. To do so, change the above command into the following form:

$ sudo usermod -a -G test_group sam-pc

When you execute the above ‘usermod’ command, it does not show you any output on the terminal screen on successful command execution. That means the user is added to a group successfully. When you add a user into the group, you need to append the ‘-a’ option. If you do not add the ‘-a’ option, the user will be removed or eliminated from any groups not listed after the option ‘-G’.

Create a New User and Add in Multiple Groups in Ubuntu

If you want to create a new user and add into a group by using just single command, use the ‘useradd’ command to add a new user and then add this user into the primary and secondary group by using the following syntax:

$ sudo useradd -g group -G group1,group2 username

In the following example, we have created a new user with the name ‘Samrina’ and assigned multiple groups. The primary group is ‘users’ and the secondary groups are ‘sudo, sam-pc’.

$ sudo useradd -g users -G sudo,sam-pc Samrina

Add an Existing Ubuntu User into Multiple Groups

To add an existing user into the multiple groups in the Ubuntu system, you can use the ‘usermod’ command followed by option ‘-G’, enter the name o the groups which will be separated by commas as follows:

$ sudo usermod -a -G first_group, second_group username

Let’s take an example, to add an existing user ‘Samrina’ into multiple groups such as ‘sam-pc, sudo, sambashare’, run the below-mentioned command:

Sponsored
$ sudo usermod -a -G sam-pc, sudo, sambashare Samrina

Change User’s Primary Group in Ubuntu

By using the `usermode` command followed by the option `-g`, you can change the user’s primary group.

$ sudo usermod -g groupname username

In the following example, we changed the primary group of a user ‘Samrina’ to ‘test_group’ by running the below-mentioned command:

$ sudo usermod -g test_group Samrina

Display User’s Groups Details in Ubuntu

You can display a complete user’s group information, such as a user belonging to which group. To perform this task, the following id command is used followed by the username:

$ id username

For example, we want to display all group information of a user ‘sam-pc’.

$ id Samrina

As you can observe in the above output, the user belongs to different groups. Here, the user’s primary group is ‘users’ and it is also a member of other supplementary groups that are ‘sudo, sam-pc, and sambashare’ which are shown in the above screenshot.

When you use the ‘group’ command followed by the username, it displays the user’s supplementary groups.

$ groups sam-pc

If you will not pass a username to the ‘group’ command, in this case, it will display the current logged in user’s group information as follows:

$ groups

Display All Ubuntu System Groups

To view all system groups that are running on your Ubuntu system, use the following ‘getent’ command:

$ getent group

The above command displays all system groups on the terminal screen and also shows you the user’s account belongs to which group.

Remove an Existing User from a Group in Ubuntu

To remove or eliminate a user from a group, execute the command ‘gpasswd’ followed by the option ‘-d’ on the terminal.

$ sudo gpasswd -d username groupname

For example, we want to remove a user ‘Samrina’ from the ‘sambashare’ group, change the above command into the following form to do this task:

$ sudo gpasswd -d Samrina sambashare

Delete a Group in Ubuntu

Use the following command ‘groupdel’ along with ‘groupname’, to delete an existing user’s group.

$ sudo groupdel groupname

Conclusion

We demonstrated in this guide how to add a user to a group in Ubuntu 20.04 system. We have seen how groups facilitate us to manage all system accounts and users. We implemented various commands for a better understanding of the users and groups. The above command can also execute on other Linux distributions. Let us know in case of any confusion related to this article. Thanks!

Ubuntu Server Admin

Recent Posts

Ubuntu Weekly Newsletter 866

Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…

11 hours ago

Ubuntu vs Debian: Linux Distributions Compared Deep Dive

Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…

20 hours ago

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

3 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

3 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

4 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

5 days ago