useradd example – Add a new user to primary group
To add a user tony to group developers use the following command:
# useradd -g developers tony
# id tony
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.
usermod example – Add a existing user to existing group
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.