• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Add User to Group in Linux Systems

Written by Admin, Updated On August 8, 2020
group, terminal, user
How to Add User to Group in Linux

In Linux, we can set different users to different roles and responsibilities. Also we can set restriction for specific users and groups. In this tutorial we will show you how to add a user to a group in Linux systems. We will also explain you how to remove a user from a group and how to create, delete, and list groups.

User Groups in Linux#

Linux groups are categories of users with pre-defined permissions. The basic purpose of group is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.

There are two types of groups in Linux operating systems:

  • Primary Group – Primary group name is same as user logged-in. Any file created by that user, the file’s group is set to the user’s primary group. The /etc/passwd file contains the information about the user’s primary group.
  • Secondary Groups – Secondary groups are useful when you want to manage particular files and software applications permissions. Members of the group inherit the read, write, and execute privileges for that group.

Each user can belong to exactly one primary group and zero or more secondary groups.

Only root or users with sudo access can add a user to a group.

How to Add an Existing User to a Group#

To add an existing user to a secondary group, you should use the usermod -a -G command followed the name of the group and the user:

sudo usermod -a -G groupname username

For example, to add the user tecnstuff to the sudo group you would run the following command:

sudo usermod -a -G sudo tecnstuff

Ensure that you should use -a (append) option when adding a user to a new group. If the -a option not added, the user will be removed from any groups not listed after the -G option.

The command will not show any output on success. You will get message if user or group does not exists.

Add a User to Multiple Groups in One Command#

When you have requirement to add an existing user to multiple secondary groups using one command, you should use usermod command followed by the -G option name of the group separated by , (commas):

sudo usermod -a -G group1,group2 username

How to Remove a User From a Group#

If you want to remove any user from group, use the gpasswd command with the -d option.

For example, to remove the tecnstuff user from the sudo group, you would run:

sudo gpasswd -d tecnstuff sudo

Here, you have to replace tecnstuff with your username and sudo with your group.

How to Create a User Group#

To create a new group, run the following command:

sudo groupadd GROUP_NAME

You should replace the GROUP_NAME with the name you want for your new group.

How to Delete a Group#

You can delete an existing group, using the groupdel command followed by the group name:

sudo groupdel GROUP_NAME

How to Change a User’s Primary Group#

To change a user primary group, use the usermod command followed by the -g option:

sudo usermod -g GROUP_NAME USER_NAME

For example, to change the primary group of the user tecnstuff to devops, you would type:

sudo usermod -g devops tecnstuff

Create a New User and Assign Groups in One Command#

The following example the useradd command will create a new user with name pintu with primary group users and secondary groups wheel and devops.

sudo useradd -g users -G wheel,devops pintu

List User Groups#

To view the user information including all the groups of which user is a member of. You should use id command followed by the username:

id USER_NAME

If you will not pass specific username the system will show the information about the current logged-in user. To check the user tecnstuff:

id tecnstuff
uid=1000(tecnstuff) gid=100(users) groups=100(users),10(wheel),95(storage)

To list only secondary groups of a user, type:

groups tecnstuff
wheel storage users

Conclusion#

In this tutorial, we have shown you how to add a user to a group in Linux systems.

If you have any question or suggestion, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash: Append to File
Next Article   How to Run Sudo Command Without Password

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook