Howto: Linux add user to group
You can use useradd or usermod commands to add user to group. useradd command creates a new user or update default new user information. usermod command modifies a user account i.e. it is useful to add user to existing group. There are two types of group. First is primary user group and other is secondary group.
useradd example - Add a new user to secondary group
Use 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. Syntax:
useradd -G {group-name} username
Create a new user called vivek and add it to group called developers. First login as a root user:
Make sure group developers exists
# grep developers /etc/groupOutput:
developers:x:1124:
If you do not see any output then you need to add group developers using groupadd command:
# groupadd developersAdd a user vivek to group developers
# useradd -G developers vivekSetup password for user vivek:
# passwd vivekEnsure that user added properly to group developers:
# id vivekOutput:
uid=1122(vivek) gid=1125(vivek) groups=1125(vivek),1124(developers)
Please note that small -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, developers:
# useradd -G admins,ftp,www,developers jerry
useradd example - Add a new user to primary group
To add a user tony to group developers use following command:
# useradd -g developers tony
# id tony
uid=1123(tony) gid=1124(developers) groups=1124(developers)
Please note that small -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.
usermod example - Add a existing user to existing group
Add existing user tony to ftp supplementary/secondary group with usermod command using -a option ~ i.e. add the user to the supplemental group(s). Use only with -G option :
# usermod -a -G ftp tonyChange existing user tony primary group to www:
# usermod -g www tony
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- Howto: Linux creating user account under group
- Linux add a user using command line tools
- FreeBSD add a user to group
- Remove Linux User From a Secondary Group ( Supplementary Groups )
- Linux setup shared directory
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
~ Last updated on: July 14, 2007


March 26th, 2006 at 4:05 pm
How can i run the passwd command
non-interactively in linux . I want to change the password through a script file….How can i
invoke the passwd command thru a script file non-interactively.
If thats not possible then suggest
some alternative…
March 26th, 2006 at 8:02 pm
See shell script example here
August 14th, 2006 at 3:41 pm
praj: see chpasswd(8)
December 6th, 2006 at 8:51 pm
thanks!
time saving tip for new admin. I can read man page but it is full of shit-load and no examples at all. keep it up good work!
June 19th, 2007 at 10:37 am
I find the easiest way to add a user to a group is using gpasswd like this:
gpasswd -a
June 19th, 2007 at 10:38 am
This editor bugs me
The command should be:
gpasswd -a user group
July 11th, 2007 at 6:52 am
This is wrong, usermod -G ftp tony will take you out of all your existing groups. Not add a new one.
July 12th, 2007 at 6:13 pm
Yes I’m adding a existing user to existing group, but to keep user in other old group you need to use:
July 14th, 2007 at 2:41 am
Actually, it’s easier to go like this:
usermod -a -G ftp tony
The -a causes your old groups to be kept. At least in the newer versions. I was looking into this and found an old manpage where the -a function is not documented.
Since I don’t know when this feature was introduced, you should check for it on the man pages before using it.
July 14th, 2007 at 10:15 pm
vim,
-a seems to a new option. This FAQ has been updated to include -a.
Appreciate your post!
August 7th, 2007 at 10:25 pm
I recommend that when you need to add people to an existing group to use
gpasswd -a USER GROUP
August 19th, 2007 at 6:24 pm
Using:
usermod (pwdutils) 3.1.2
there is no -a option available, so that options is a fairly new one, it also appears to be nonstandard across *NIX.
August 22nd, 2007 at 9:06 am
thanks for -a option, it saved my time on centos
January 7th, 2008 at 12:01 pm
why this command didnt work on sun solaris 10 ?
#useradd -u 100 -g other -d /export/home/newuser1 -m -s /bin/ksh -c “Regular User Account ” newuser1
Error :uid 100 already in use
January 25th, 2008 at 9:20 pm
Hello,
how can I give a usergroup root rights?
I would like to create a group “developer” but they need root access as well.
thanks,
Oliver
January 25th, 2008 at 9:23 pm
I also want to give another user root access.
That way you can do PermitRootLogin yes, which is more secure.
January 26th, 2008 at 3:09 pm
install sudo.
then edit /etc/sudoers (or other location)
and append the line-
%developer ALL=(ALL) NOPASSWD: ALL
This will give the users in group developer the root access but will need to type
sudo
The above setup will not demand root password
March 11th, 2008 at 6:47 pm
Now that I foolishly ran:
usermod -G ftp chrisIs there anyway I get access back to all the groups I had before?
I am using Ubuntu so I don’t have a password for the root user.
March 11th, 2008 at 7:31 pm
See if you have backup in /etc/group- or restore it from your tape backup.
HTH
March 11th, 2008 at 7:50 pm
no tape backup and unless the etc/group file is auto backedup I don’t have one of those either.
March 12th, 2008 at 5:28 am
May be this will help
April 30th, 2008 (3 weeks ago) at 8:34 am
Hi,
Is there a way to automatically create a MySQL database when creating the user?
(the new database would be named after the user and with the same login / password)
April 30th, 2008 (3 weeks ago) at 12:48 pm
You need to write a shell script; there is no built in option to create mysql db.
April 30th, 2008 (3 weeks ago) at 2:04 pm
OK, so for instance, with webmin which allows the creation of users from a text using useradd, it would not be possible to automatically add a database for each call of useradd without modifying webmin…
That’s not making my life any easier
May 2nd, 2008 (2 weeks ago) at 4:19 am
Hi… newb here
I have a quick question: in CentOS 5, when I type usermod -a -G ftp tony I always get the error “invalid numeric argument ‘ftp’ why is that?
May 2nd, 2008 (2 weeks ago) at 4:20 am
sorry just read the disclaimer on “leave reply” disregard my question