Linux Add User To Group Using Command-Line

See all GNU/Linux related FAQ
How can I add a user to a group under Linux operating system using command line options? How to add an existing user into a group in Linux using command line options? How to add a user to a group in Linux server systems?

You can use the useradd or usermod commands to add a user to a group on Linux. This page explains how to add user to group in Linux using the CLI. The useradd command creates a new user or updates default new user information. The usermod command modifies a user account, and it is useful to add a user to existing groups. There are two types of groups on Linux operating systems:
Advertisement

  1. Primary user group. – It is the group that applied to you when login. Typically it is same as your login name. All of your process and files (including directories/folders) would have your primary group as the group membership. The primary group allows private group membership and security features. Your files or process cannot access by other group members or users on the Linux system.
  2. Secondary or supplementary user group – Users can be a member of other groups on the Linux system. It is useful for file sharing and other purposes. A sysadmin can fine-tune security too. For example, if you are a member of a secondary group called cdrom, you can mount and unmout cd-rom drive.
Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements Linux terminal
Category User Management
OS compatibility AlmaLinux Alpine Arch Debian Fedora Mint openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu WSL
Est. reading time 6 minutes

How to add user to group in Linux

Please note that all user accounts related information are stored in the following files:

  • /etc/passwd – Contains one line for each user account.
  • /etc/shadow – Contains the password information in encrypted formatfor the system’s accounts and optional account aging information.
  • /etc/group – Defines the groups on the system.
  • /etc/default/useradd – This file contains a value for the default group, if none is specified by the useradd command.
  • /etc/login.defs – This file defines the site-specific configuration for the shadow password suite stored in /etc/shadow file.

We do not modify these files by hand. Instead, we add a user to a group in Linux using various commands.

Linux command to add user to group

Open the terminal and then type:

  1. Add a new user called jerry to secondary group named cartoons on Linux:
    $ sudo useradd -G cartoons jerry
  2. Want to add a new user called tom to primary group called cartoons? Run:
    $ sudo useradd -g cartoons tom
  3. We can add a existing user named spike to existing group named cartoons in Linux:
    $ sudo useradd -g cartoons spike

How to become a root user

You must run all command as root user. To become a root user run:
$ su -
Alternatively, use the sudo command:
$ sudo -i

Add a new user to secondary group using useradd

You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:
# useradd -G {group-name} username
In this example, create a new user called vivek and add it to group called developers. First, make sure group developers exists using the grep command as follows:
# grep "^developers" /etc/group
Sample outputs:

developers:x:1124:

If you do not see any output then you need to add group developers using the groupadd command:
# sudo groupadd developers
Verify that user vivek does not exists:
# grep "^vivek" /etc/passwd
You should not see any outputs from above command. Finally, add a new user called vivek to group developers:
# useradd -G developers vivek
Setup password for user vivek:
# passwd vivek
Ensure that user added properly to group developers using the id command:
# id vivek
Sample outputs:

uid=1122(vivek) gid=1125(vivek) groups=1125(vivek),1124(developers)

Adding a user to multiple groups

Please note that capital G (-G) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry

How to add a new user to primary group using useradd

To add a user tony to group developers use the following command:
# useradd -g developers tony
# id tony

Linux Add User To Group
Please note that small g (-g) option add user to initial login group (primary group). The group name must exist. A group number must refer to an already existing group.

How to add a existing user to existing group using usermod

Add existing user tony to ftp supplementary/secondary group with the usermod command using the -a option ~ i.e. add the user to the supplemental group(s). Use only with -G option:
# usermod -a -G ftp tony
In this example, change tony user’s primary group to www, enter:
# usermod -g www tony

usermod command options summary

Option Purpose
-a--append Add the user to the supplementary group(s). Use only with the -G option.
-g GROUP
--gid GROUP
Use this GROUP as the default group.
-G GRP1,GRP2--groups GRP1,GRP2 Add the user to GRP1,GRP2 secondary group.

Creating a group

You can create a new group using the groupadd command as follows:
$ sudo groupadd {Group_Name_Here}
$ sudo groupadd sftpusers

Use the getent command or grep command on /etc/group to verify new group name:
$ getent group sftpusers
$ grep '^sftpusers' /etc/group

Deleting a group

You can delete an existing group, using the groupdel command:
$ sudo groupdel {Group_Name_Here}
$ sudo groupdel wwwusers

Again use the getent command or grep command on /etc/group to verify new group name was removed from the Linux system:
$ getent group wwwusers
$ grep '^wwwusers' /etc/group

Viewing all groups on the Linux system

Use the cat/more/less command as follows:
$ more /etc/group
## or use the getent command ##
$ getent group

How do I view the groups a Linux user account is assigned to?

Try the groups command or id command to find out the groups a Linux user named ‘vivek’ assigned to using the following two commands:
$ groups vivek
$ id vivek

A note about security

If you add or delete user to existing group, you must change the owner of any crontab files or at jobs manually. You must make any changes involving NIS on the NIS server too.

A note about GUI tool

You will probably find the use of the GUI tool easy. KDE user can use KUser tool and the GNOME user can use users-admin:

users-admin - Linux Users Administration Tool

users-admin is part of the GNOME system tools, a set of tools to easily access and manage system configuration

One can easily add users as follows from the gnome 3 settings on a Debian/Ubuntu Linux:
Fedora/RHEL/CentOS user can use system-config-users command as follows
# system-config-users
$ ssh -X -t vivek@server1.cyberciti.biz sudo system-config-users

Linux add new users with user manager
Click on the Groups tab to add or view groups:
Linux Add User To Group Using GUI Tools

Summing up

You learned how to add a new or existing user to group on Linux operating systems. For more information type the following command at the shell prompt to read man pages using the man command or help command:
$ man usermod
$ man passwd
$ man useradd

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

136 comments… add one
  • Jana Dec 17, 2022 @ 8:26

    Wow, there is so much information in here. Thank you for sharing your knowledge.

  • Chris Aug 6, 2023 @ 18:33

    Good evening! I find that all of the step-by-step guides on cyberciti.biz are excellent, much better than those of OpenAI which seem to be mere copies.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.