Professional Documents
Culture Documents
Menu
Tutorials
BASH Shell
Troubleshooting
Nginx
Networking
MySQL
Google Cloud Platform
Amazon Cloud Computing
Rackspace Cloud Computing
Linux
CentOS
Debian / Ubuntu
Ubuntu Linux
Suse
RedHat and Friends
Slackware Linux
UNIX
AIX
Mac OS X
FreeBSD
FreeBSD Jails (VPS)
Openbsd
Solaris
See all tutorial topics
Contact us
Linux Scripting Guide
RSS/FEED
Shop
nixCraft
Linux and Unix tutorials for new and seasoned sysadmin.
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?
You can use the useraddor usermodcommands to add a user to a group. The useradd command creates a new user or update default new user information. The
usermod command modifies a user account and it is useful to add user to existing groups. There are two types of groups under Linux operating systems:
1. Primary user group.
2. Secondary or supplementary user group.
Tutorial details
Difficulty
Intermediate (rss)
Root privileges
Yes
All user account related information are stored in the following files:
Requirements
usermod/useradd
Estimated completion time 5 minutes
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.
In this example, create a new user called vivek and add it to group called developers. First login as a root user (make sure group developers exists), enter:
# grep developers /etc/group
Output:
developers:x:1124:
If you do not see any output then you need to add group developers using the groupadd command:
# groupadd developers
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
1/15
24/6/2015
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
Sample outputs:
uid=1123(tony) gid=1124(developers) groups=1124(developers)
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.
Purpose
Add the user to the supplementary group(s). Use only with the -Goption.
Use this GROUP as the default group.
Add the user to GRP1,GRP2 secondary group.
--groups GRP1,GRP2
Sample outputs:
For more information type the following command at the shell prompt:
$ man usermod
$ man useradd
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
2/15
24/6/2015
Reply Link
lefty.crupps October 15, 2014, 5:39 pm
Youre suggesting wed need to know each group first? Perhaps telling us that above would be good, also including the command to find the current
oldgroup1 oldgroup1 stuff:
groups lefty
Also, whats the difference between useradd and adduser?
Whats the differences between distros?
The article could use some reorganization as well to make the concept flow better, but the article has good info, just not laid out super-clearly. IMHO.
Reply Link
vim July 14, 2007, 2:41 am
Actually, its 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 dont know when this feature was introduced, you should check for it on the man pages before using it.
Reply Link
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
3/15
24/6/2015
4/15
24/6/2015
Is there anyway I get access back to all the groups I had before?
I am using Ubuntu so I dont have a password for the root user.
Reply Link
nixCraft March 11, 2008, 7:31 pm
See if you have backup in /etc/group- or restore it from your tape backup.
HTH
Reply Link
chris March 11, 2008, 7:50 pm
no tape backup and unless the etc/group file is auto backedup I dont have one of those either.
Reply Link
nixCraft March 12, 2008, 5:28 am
May be this will help
Reply Link
LiLo April 30, 2008, 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)
Reply Link
nixCraft April 30, 2008, 12:48 pm
You need to write a shell script; there is no built in option to create mysql db.
Reply Link
LiLo April 30, 2008, 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
Thats not making my life any easier :)
Reply Link
toti May 2, 2008, 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?
Reply Link
toti May 2, 2008, 4:20 am
sorry just read the disclaimer on leave reply disregard my question :|
Reply Link
RobM June 6, 2008, 8:27 pm
If you modified your own user account while logged in with it, the changes will not have an effect until you next login.
If you dont want to logout, you can replace your current BASH shell with a new one to achieve the same thing by running the follow:
su preserve-environment command $(which $SHELL) login -i $(whoami)
This will switch-user to yourself, replacing your current shell with a new instance of itself. You will be prompted for your password.
If you dont specify a command to run youll probably get an error to the effect of cannot execute bash: file not found because it wont be on the environment.
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
5/15
24/6/2015
You could use shell to point su at it, but your shell wouldnt be a login shell and therefore wont read your .bash_profile etc.
Reply Link
kishore June 19, 2008, 9:58 am
Hi friends,
I want to say thanks for u.Because of this post i created the users myself without anyones help.
Thanks and Regards
Kishore
Reply Link
helpneeded June 26, 2008, 11:30 am
why do we need to add new users in linux?
Reply Link
nixCraft June 26, 2008, 1:31 pm
Linux is multiuser operating system. User can maintain their privacy and security.
Reply Link
HAMEED June 27, 2008, 9:18 am
WHAT IS THE COMMAND TO CHECK THE EXISTING GROUPS IN THE SYSTEM AND ALSO COMMAND TO CHECK THE USERS ???
Reply Link
itsadok July 5, 2008, 6:16 am
There are simpler commands (at least on Ubuntu):
adduser user group
6/15
24/6/2015
7/15
24/6/2015
Suggestions::
plz insert search tool with in website.
GOD bless YOU
Reply Link
hyacinth February 4, 2009, 3:01 am
How to delete and add a group..
please help me..
Thanks
Reply Link
dunno February 13, 2009, 9:41 pm
complete noob with linux here
what are the actual groups that you can add users to?
i know theres 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 its 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 isnt the right place to ask this, i have no idea where else to ask and ive been seraching for this for hours how do you
mount a new hard drive? theres 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 cant believe that that would be impossible, so theres got to be a way.
Reply Link
wasim March 9, 2009, 12:14 pm
to add user in multiple group
usermod -G group1,group2 username
Reply Link
Patrick March 29, 2009, 6:04 pm
i have 3 sites. MainSite TestSite and FileSite. Main and Test need to be apart of the FileSite group to be able to read/write/delete files.
usermod -a -G MainSite,TestSite FileSite
is that right?
Reply Link
Aheebwa Edgar May 19, 2009, 6:16 pm
pliz thanx so much for your ideas but my question was how to add one user to the linux group.am waiting right now.
God bless you
Reply Link
Aheebwa Edgar May 19, 2009, 6:46 pm
i need just a command to add user to a group.thank you
Reply Link
Snap June 5, 2009, 2:44 am
If I have a user that is only used to run scripts (started by fetchmail), but I do not actually ever log in as such user in the console. I have added that user to a group to
get access to a folder, but it does not seem to work.. if it was a normal user, I would simply log out and log in again to activate my membership in this group.. but
since I never log in with this user, how can I make sure that it is actually a member of the group?
Reply Link
balu phani July 29, 2009, 6:18 am
hi
i want to create a user say testusr and i dont want to get home directory for him and his individual group (generally he will belong to his group testusr group i
dont want it) but i have already have a common group say cmngrp so he should belong to it only.
i think my point is clear
create testusr without home directory and without his own group but belongs to cmngrp.
Reply Link
balu phani July 29, 2009, 7:29 am
i got it
useradd -Ng -M
Reply Link
wasim July 29, 2009, 11:38 am
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
8/15
24/6/2015
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
9/15
24/6/2015
Reply Link
uzer July 4, 2010, 7:51 pm
Hey, Im getting
[user@localhost ~]$ sudo su
[sudo] password for user:
user is not in the sudoers file. This incident will be reported.
Reply Link
shane williamson July 7, 2010, 2:03 am
Okay these commands DO NOT WORK!!!!!!!!!!!!!!
Reply Link
indir July 9, 2010, 6:25 am
thanks, its works for me :)
Reply Link
selven September 15, 2010, 7:44 pm
seriously, this is so gay! in my days to add a user to a group, we just had to add it in /etc/group! i dont understand why there should be a tool to complicate it!
Reply Link
nic September 16, 2010, 2:49 pm
I like to use a for loop for this. For exampled
for i in group1 group2 group3 ...; do adduser $i; done
Reply Link
Tom O'Connell October 7, 2010, 3:22 pm
I am using Mint 9 and have no knowledge of either command line or archive manager.
Can you steer me to the easiest tutorial.
Thanks,
Reply Link
dx November 4, 2010, 10:06 am
how many users can be in a group? i want to add 500 users to group mailusers. wont that be slowing down my mailserver?
regards
Reply Link
syed December 9, 2010, 7:53 pm
i have need in one account and multiple sign
Reply Link
NURUL HUDA January 9, 2011, 4:04 pm
I have lost my all inbox mails unexpectedly.Is it possible to retrive again?please help me.
Reply Link
emiat February 1, 2011, 4:52 am
what is the 1st step on how to add group in active directory 2003? And 1st step of how to add group in active directory?
emiat.
Reply Link
Rodislav Moldovan April 3, 2011, 10:01 pm
#works in Debian 6
Add user to a new group, by keeping old groups
usermod -a -G AdditionalGroup User
-a append
-G group name
Reply Link
Rashid May 12, 2011, 10:50 am
thanks for sharing useful and knowledgeable topic
Reply Link
Extra June 27, 2011, 8:02 am
Oh yeah man! This is the best site i ever meet, on helping users to quick find the answer to concrete Question! Thank you so much for the FAQ archive! wish you
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
10/15
24/6/2015
11/15
24/6/2015
The correct way to add a user to a group is usermod -aG groupname username.
Reply Link
nitiratna nikalje April 25, 2012, 1:19 pm
its mind blowing solution for adding user to any group .we can add multiple existing users to an existing group by this method # gpasswd -M niti,gauri,lina,nidhi masti
=added user niti ,gauri ,lina and nidhi to group masti
Reply Link
Jeremy June 11, 2012, 5:03 pm
Thanks, this is helpful for people like myself; just starting out with RHEL.
Reply Link
dhani September 18, 2012, 8:21 am
Thank you, this really solve my problems in samba share.
Reply Link
Bijay Manandhar September 20, 2012, 6:24 am
really nice tutorial ..
thanx millions..
Reply Link
Valerio October 15, 2012, 6:41 pm
Hi I tryed your hint, but I have got some erros below:
root@ibeji:/etc# testparm
Load smb config files from /etc/samba/smb.conf
Unknown parameter encountered: client code page
Ignoring unknown parameter client code page
Unknown parameter encountered: character set
Ignoring unknown parameter character set
Unknown parameter encountered: domain admin group
Ignoring unknown parameter domain admin group
Unknown parameter encountered: domain admin users
Ignoring unknown parameter domain admin users
Processing section [netlogon]
Processing section [home]
Processing section [publico]
Processing section [contabil]
Processing section [suporte]
Processing section [tmp]
Loaded services file OK.
Server role: ROLE_DOMAIN_PDC
Press enter to see a dump of your service definitions
Reply Link
Sequoia December 18, 2012, 5:05 pm
You should really change this article to either
a) use the -a switch in the first example or
b) add a BIG RED WARNING that the command you give will REMOVE THE USER FROM ALL CURRENT GROUPS
This comes up #1 on google for linux add user to group and I suspect Im not the only one who completely screwed up their user by running the command you
have listed here to add a user to a group
Perhaps the article should be titled How to remove a user from all groups and add them to a new group
Reply Link
Subrat N January 10, 2013, 5:52 pm
Hi Geeks,
How can I add multiple users into a group at a time without removing the old users belongs to that particular group.
I have tried the following command
#gpasswd -M user1,user2,user3 group_name
But, the thing is that, after adding these users, the previous users belongs to this group being removed.
I want the previous users to be exist.
Please, help..
Thanks in advance ..
Reply Link
Luis Mompo Handen January 22, 2013, 1:35 pm
I built a bash script to add multiple users from a csv-file to an existing group without creating a home dir. The passwords in csv-file are clear text.
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
12/15
24/6/2015
#!/bin/bash
FILE=/root/test.csv
cat $FILE | while read line; do
USER=$(echo $line | awk -F\; '{print $1}')
PASS_TMP=$(echo $line | awk -F\; '{print $2}' | sed 's/\ //g')
useradd -M -G cvs_usr $USER
echo $USER:$PASS_TMP | chpasswd
done
Reply Link
Landis January 27, 2013, 7:30 pm
In SuSE linux, there is No -a (this article states that the -a retains existing groups and adds new group -G), but in openSuSE 12.2 at least, there is no -a
option to usermod, it keeps existing groups, by default i guess
# usermod -G GroupName UserName
eg,
# id lunar
uid=1002(lunar) gid=100(users) groups=100(users)
# usermod -G sshd lunar
# id lunar
uid=1002(lunar) gid=100(users) groups=100(users),102(sshd)
Landis.
Reply Link
Vaibhav Gupta January 31, 2013, 3:09 pm
Hi ,
I have built a x86 rootfs using buildroot with both ifuse , imobiledevice ,usbmuxd . i did chroot ( directory :/var/prj_ca/) to the generated filesystem and mounted the
following
mount bind /dev /var/prj_ca/dev
mount bind /dev/pts /var/prj_ca/dev/pts
mount bind /dev/fuse /var/prj_ca/dev/fuse
mount bind /proc /var/prj_ca/proc
mount bind /sys /var/prj_ca/sys
mount bind /media /var/prj_ca/media
when i run .
lsusb -v | grep -i iSerial i am getting the IPOD serial id but .
ifuse is failing with following error.
ifuse /tmp/apple_inc._ipod_b067d003ed1b22a9fffa47e3654a00e6386a6c9a/
usbmuxd_get_device_list: error opening socket!
No device found, is it connected?
If it is make sure that your user has permissions to access the raw usb device.
If youre still having issues try unplugging the device and reconnecting it.
I am stuck can u please help.
Reply Link
Muhammad Usman Majeed April 22, 2013, 7:46 am
# usermod -a -G ftp tony
what did the -a switch do?
Regards
Reply Link
prashanth July 12, 2013, 12:35 pm
is there any way to add multiple users to a existing group with disturbing or deleting existing users in that group
Reply Link
George Huebschman July 16, 2013, 5:49 pm
Thanks,
A very clear thorough answer to the question!
When you have a choice, spend your money where youd prefer to work if you had NO choice.
Reply Link
bb November 7, 2013, 1:52 pm
adduser not useradd
Reply Link
Jose December 31, 2013, 10:24 pm
Excellent bro !, Im using fedora and this is very usefull for HTTPD directories !
Reply Link
Golandaj S February 5, 2014, 6:45 am
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
13/15
24/6/2015
adduser account
Reply Link
Golandaj Saddam February 5, 2014, 6:47 am
groupadd
Reply Link
Naveen Sharma April 25, 2014, 12:45 pm
I got this question lately in an interview.
One User ID can belong to maximum how many secondary groups.?
Is there a definite no?
Reply Link
JET April 25, 2014, 11:45 pm
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!
Reply Link
Extra December 18, 2014, 10:51 am
Good point Jet! Thank You!
Reply Link
raja May 19, 2014, 2:45 am
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
Reply Link
lliseil January 1, 2015, 5:26 pm
# Posts 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
-d, --delete USER
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
14/15
24/6/2015
i know theres 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 its 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 isnt the right place to ask this, i have no idea where else to ask and ive been seraching for this for hours how do you
mount a new hard drive? theres 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 cant believe that that would be impossible, so theres got to be a way.
Reply Link
harry hindson February 24, 2015, 2:25 pm
I promise I do fancy her no joke
Reply Link
Leave a Comment
Name *
Email *
Website
Comment
Submit
Next post: Howto Compiling C program and creating executable file under Linux / UNIX / *BSD
Previous post: Linux Start Oracle As a Service
To search, type and hit enter
Featured Articles:
30 Cool Open Source Software I Discovered in 2013
30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
Top 30 Nmap Command Examples For Sys/Network Admins
25 PHP Security Best Practices For Sys Admins
20 Linux System Monitoring Tools Every SysAdmin Should Know
20 Linux Server Hardening Security Tips
Linux: 20 Iptables Examples For New SysAdmins
Top 20 OpenSSH Server Best Security Practices
Top 20 Nginx WebServer Best Security Practices
20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
15 Greatest Open Source Terminal Applications Of 2012
My 10 UNIX Command Line Mistakes
Top 10 Open Source Web-Based Project Management Software
Top 5 Email Client For Linux, Mac OS X, and Windows Users
The Novice Guide To Buying A Linux Laptop
2000-2015 nixCraft. All rights reserved. Privacy Policy - Terms of Service - Questions or Comments
The content is copyrighted to nixCraft and may not be reproduced on other websites.
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
15/15