Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Add User to Group in Linux Systems - DesignLinux

Aug 08 2020
designlinux 0 Comments
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.

Related

Tags: group, terminal, user

How to Install Sublime Text 3 on Ubuntu 20.04

Prev Post

How to Run Sudo Command Without Password

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 462
  • 606,212

DesignLinux.com © All rights reserved

Go to mobile version