{"id":4559,"date":"2021-02-17T15:28:29","date_gmt":"2021-02-17T15:28:29","guid":{"rendered":"https:\/\/linuxways.net\/?p=4559"},"modified":"2021-02-17T15:28:40","modified_gmt":"2021-02-17T15:28:40","slug":"how-to-add-user-to-group-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-add-user-to-group-in-linux\/","title":{"rendered":"How to Add User to Group in Linux"},"content":{"rendered":"<p>Groups make it easy to give users the same security and access privileges for modifying a file or directory. A single user can be associated with several groups. In Linux, groups can be classified into two types that are primary and secondary groups. Primary groups are created during a user account creation while the secondary group can be one or more groups a user belongs to.<\/p>\n<p>In this post, we are going to describe how to add a user to a group in Linux OS. This post will also cover adding a user to multiple groups, removing a user from a group, changing a user&#8217;s primary group, creating, removing, and listing groups.<\/p>\n<p><strong>Note<\/strong>: The procedure explained here has been tested on <strong>Ubuntu 20.04 LTS<\/strong>. However, the same procedure is also valid for other Linux distributions.<\/p>\n<h2>Prerequisite<\/h2>\n<p>You will need sudo privileges or root access in order to add or remove a user from a group in Linux.<\/p>\n<h2>How to Add an Existing User to a Group<\/h2>\n<p>When a new user is created, it is automatically added to a group of the same name. For instance, after creating a user named \u201ctin\u201d, the system will automatically create a new group named \u201ctin\u201d. Then this new user \u201ctin\u201d will be added to this new group named \u201ctin\u201d.<\/p>\n<p>If you want add a user to some other groups, the command is as follows:<\/p>\n<pre>$ sudo usermod -a -G &lt;group_name&gt; &lt;user_name&gt;<\/pre>\n<p>For instance, the below command will add an already existing user named \u201ctin\u201d to a secondary group named \u201csamba\u201d.<\/p>\n<pre>$ sudo usermod -a -G samba tin<\/pre>\n<p>Enter the sudo password, after which a new user will be created and assigned a group.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"563\" height=\"50\" class=\"wp-image-4560\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-group.png\" alt=\"add user to group\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-group.png 563w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-group-300x27.png 300w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/p>\n<h2>How to Add an Existing User to Multiple Groups<\/h2>\n<p>If you want to add an existing user to multiple secondary groups, you can do so using a single command as follows:<\/p>\n<pre>$ sudo usermod -a -G &lt;group_name1,group_name2,group_name3&gt; &lt;user_name&gt;<\/pre>\n<p>For instance, the below command will add an existing user named \u201ctin\u201d to three groups named \u201csudo\u201d, \u201csamba\u201d, and \u201cadmin\u201d.<\/p>\n<pre>$ sudo usermod -a -G sudo,samba,admin tin<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"53\" class=\"wp-image-4561\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-multiple-groups.png\" alt=\"add user to multiple groups\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-multiple-groups.png 693w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/add-user-to-multiple-groups-300x23.png 300w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/p>\n<h2>Create a New User and Add in a Group<\/h2>\n<p>If you need to create a new user and add it in a secondary group, here is the command to do so:<\/p>\n<pre>$ sudo useradd -G &lt;group_name&gt; &lt;user_name&gt;<\/pre>\n<p>For instance, the below command will create a new user named \u201ctitin\u201d and add it to the already existing group named \u201csamba\u201d.<\/p>\n<pre>$ sudo useradd -G samba tintin<\/pre>\n<p>Enter the sudo password, after which a new user will be created and assigned a group.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"49\" class=\"wp-image-4562\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/create-and-add-user-in-a-group.png\" alt=\"create and add user in a group\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/create-and-add-user-in-a-group.png 574w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/create-and-add-user-in-a-group-300x26.png 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<h3>How to Change the Primary Group of a User<\/h3>\n<p>To change the primary group of a user, the command is as follows:<\/p>\n<pre>$ sudo usermod -g &lt;group_name&gt; &lt;user_name&gt;<\/pre>\n<p>For instance, to change a user\u2019s named \u201ctin\u201d primary group to \u201csamba\u201d, the command would be:<\/p>\n<pre>$ sudo usermod -g samba tin<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"49\" class=\"wp-image-4563\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/change-the-users-primary-group.png\" alt=\"change the user's primary group\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/change-the-users-primary-group.png 543w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/change-the-users-primary-group-300x27.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/p>\n<h2>View the Groups a User is added To<\/h2>\n<p>After adding a user to a specific group, you can confirm it as follows:<\/p>\n<pre>$ groups &lt;user_name&gt;<\/pre>\n<p>It will show you the groups to which the user specified as &lt;user_name&gt; is added to. For instance, the below command shows that a user \u201ctin\u201d is added to two groups \u201ctin\u201d and \u201csamba\u201d. <img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"61\" class=\"wp-image-4564\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-the-groups-a-user-belongs-to.png\" alt=\"View the Groups a User belongs to\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-the-groups-a-user-belongs-to.png 372w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-the-groups-a-user-belongs-to-300x49.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/p>\n<h2>How to Remove a User from a Group<\/h2>\n<p>For removing a user from a specific group, the syntax is as follows:<\/p>\n<pre>$ sudo gpasswd -d &lt;user_name&gt; &lt;group_name&gt;<\/pre>\n<p>For removing a user named \u201ctin\u201d from the group named \u201csamba\u201d, command would be:<\/p>\n<pre>$ sudo gpasswd -d tin samba<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"53\" class=\"wp-image-4565\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/remove-a-user-from-a-group.png\" alt=\"remove a user from a group\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/remove-a-user-from-a-group.png 537w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/remove-a-user-from-a-group-300x30.png 300w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/p>\n<h2>How to Create a New Group<\/h2>\n<p>If you need to create a new group in Linux, you can do so by using the below command in Terminal:<\/p>\n<pre>$ sudo groupadd group_name<\/pre>\n<p>Make sure to replace <strong>group_name<\/strong> with the group name you want to create.<\/p>\n<h2>How to Remove a Group<\/h2>\n<p>If you need to remove a group from the Linux system, the syntax is as follows:<\/p>\n<pre>$ sudo groupdel &lt;group_name&gt;<\/pre>\n<p>For instance, to remove a group named \u201csamba\u201d, the command would be:<\/p>\n<pre>$ sudo groupdel samba<\/pre>\n<h2>View All Groups on the System<\/h2>\n<p>If you want to view all the groups on the Linux system, the command is as follows:<\/p>\n<pre>$ getent group<\/pre>\n<p>This command will list all the groups on your system along with the user accounts which are member of these groups.<\/p>\n<p>For instance, the following output shows the list of groups along with their members like you can see users \u201csyslog\u201d and \u201ckbuzdar\u201d are members of group \u201cadm\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"169\" class=\"wp-image-4566\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-all-groups-in-linux.png\" alt=\"view all groups in Linux\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-all-groups-in-linux.png 383w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/view-all-groups-in-linux-300x132.png 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/p>\n<p>This was all about how to add a user to a group in Linux. If you want to learn about adding or removing a user on Linux OS, you can follow our guide on <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-add-and-remove-users-on-ubuntu-20-04\/\">How to Add and Remove Users on Linux.<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Groups make it easy to give users the same security and access privileges for modifying a file or directory. A single user can be associated with several groups.&hellip;<\/p>","protected":false},"author":4,"featured_media":4567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[35],"class_list":["post-4559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-linux"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4559"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4567"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}