Linux Add User To Group

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?

Tutorial details
Difficulty Intermediate (rss)
Root privileges Yes
Requirements usermod/useradd
Time 5 minutes
You can use the useradd or usermod commands to add a user to a group. 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:

  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.

All user account related information are stored in the following files:

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

Linux command to add user to group

  1. Add a new user called jerry to secondary group named cartoons on Linux:
    sudo useradd -G cartoons jerry
  2. Add a new user called tom to primary group called cartoons:
    useradd -g cartoons tom
  3. Linux add a existing user named spike to existing group named cartoons:
    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 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 grep command:
# 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:
# id vivek
Sample outputs:

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

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


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.

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 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

Sample outputs:

Click on the Groups tab to add or view groups:

See also

For more information type the following command at the shell prompt:
$ man usermod
$ man useradd

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
133 comments… add one
  • bb Nov 7, 2013 @ 13:52

    adduser not useradd

  • Jose Dec 31, 2013 @ 22:24

    Excellent bro !, I’m using fedora and this is very usefull for HTTPD directories !

  • Golandaj S Feb 5, 2014 @ 6:45

    adduser account

    • Golandaj Saddam Feb 5, 2014 @ 6:47

      groupadd

  • Naveen Sharma Apr 25, 2014 @ 12:45

    I got this question lately in an interview.
    One User ID can belong to maximum how many secondary groups.?

    Is there a definite no?

  • JET Apr 25, 2014 @ 23:45

    The format of this post took out an important part of the commands. PLEASE DISREGARD ABOVE. Here is an ammended post:

    A simple “man usermod” will provide the options available. The -A option does not remove any groups, it only adds. For adding a user to a group, MY preferred method is:
    usermod username -A group

    If you wish to add user to a list of groups, you may use a comma as delimiter, as in:
    usermod username -A group1,group2,group3

    The -G option is not preferred in my opinion, it is used to specify all groups to which the user will belong to, and has the ability to REMOVE a user from unspecified groups. As many have pointed out, the ability to remove a user from groups can have nasty results if not carefully used.

    So, just use the -A option to add your existing users to existing groups, and you should be all set. Good luck!

    • Extra Dec 18, 2014 @ 10:51

      Good point Jet! Thank You!

  • raja May 19, 2014 @ 2:45

    i am create the create to the roup bdba an user add but he was not add to the usermod command.
    usermod -u 1000 -g dba -d |d01|oracle

  • lliseil Jan 1, 2015 @ 17:26

    # Post’s original command (does not work):

    useradd -G GROUP USER
    useradd: user 'USER' already exists

    # Working command:

    gpasswd -a USER GROUP
    Adding user USER to group GROUP

    # Help:

    gpasswd -h
    Usage: gpasswd [option] GROUP
    
    Options:
      -a, --add USER                add USER to GROUP
      -d, --delete USER             remove USER from GROUP[/code]
    

    Arch linux here.

  • pandiyan Jan 3, 2015 @ 12:10

    thanks friends…..

  • Craig R Morton Jan 13, 2015 @ 15:33

    Excellent, thank you.

  • Harry Hinson likes erica Feb 23, 2015 @ 12:00

    complete noob with linux here…

    what are the actual groups that you can add users to?

    i know there’s the ‘users’ group for standard users… but what else is there? can i add a user as a sysadmin with full control over the system? or is that the same as logging in as root?

    i also understand it’s apperently a “bad idea” to login as root into KDE… but why?

    sorry, like i said, complete noob. just installed yesterday.

    oh, and also, even though i realize this isn’t the right place to ask this, i have no idea where else to ask and i’ve been seraching for this for hours… how do you mount a new hard drive? there’s plenty of info telling how to mount a cdrom or a floppy drive, or even a usb thumb drive, but NOWHERE does anything or anyone say how to mount a simple hard drive…. is it not possible to add an additional hard drive? i can’t believe that that would be impossible, so there’s got to be a way.

    • harry hindson Feb 24, 2015 @ 14:25

      I promise I do fancy her no joke

  • pradeep Jul 16, 2015 @ 3:34

    Good command explanations

  • Nina Jul 16, 2015 @ 13:18

    Hi, I want to know how to create a group in redhad but to be sure that this group is created in the local machine and not in the grid where I am connected at. I saw man and found option groupadd -r that seesm to add the user to the system account. How could I be sure that the new group is created in the local system? Thanks

  • Reddy Aug 19, 2015 @ 15:55

    This just saved my day. Thank you.

  • angiekarix Sep 29, 2015 @ 5:11

    Hi
    I wanted to know how I can add multiple users at once and not necessarily in one group.

  • ROhit Feb 18, 2016 @ 17:56

    hi All

    How to create Group in Centos 6.7
    Group name “Project Manager”

    what is the cli commands
    Please share asap.

  • Vasanth Muthu Feb 25, 2016 @ 21:40

    Fantastic!!! Your blog is full of information. I finally added a user to a group in linux….. Thanks,

  • Peter Mar 29, 2016 @ 20:06

    Great thank you, tired of looking on google every time lol hitting back to school this summer!

  • Poldovico Oct 25, 2016 @ 21:04

    Does “usermod -G group” remove me from every other group? If so, the guide should mention.
    Because in doing “usermod -G my_username www-data”) I seem to have removed myself from the sudo group on my remote server on which the root account is locked. Which is fun.

    • 🐧 Vivek Gite Oct 26, 2016 @ 19:23

      Why you skipped the -a option while using usermod command? The guide clearly use the example as follows:
      usermod -a -G ftp tony

      • Poldovico Oct 27, 2016 @ 10:37

        So I did. Crap.
        My apologies.
        At least I was able to fix my mistake without needing a reinstall or getting support involved, so I got that going for me.

  • Kevin Feb 4, 2017 @ 7:04

    Hi Please help me this assignment
    Q: Assign permission to access folder /share only member of IT, Sale, and Finance

    Please answer!

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.