How To Add User To Group In Linux

by lifeLinux on May 19, 2011

There are two types of group. First is primary user group and other is secondary group. Group infomation is stored in /etc/group file contains basic group attributes. This is an ASCII file that contains records for system groups.

useradd command

You need to add new users to existing group. The syntax is as follows:

# useradd -g <groupname> <username>

Example, create a new user called user1 and add it to group called admin. First login as a root user (make sure group admin exists), type the following command

# useradd -g admin user1

Ensure that user added properly to group admin, enter

# id user1

Output

uid=503(user1) gid=502(admin) groups=502(admin)

usermod command

If you need to add a existing user to existing group, type the command with syntax is as follows

# usermod -G <groupname> <username>

To add user to a list of supplementary groups, enter

# usermod -G group1, group2, group3 username

How to remove existing Linux user from existing group? See example bellow

#id test

Output

uid=503(test) gid=501(www) groups=501(www),4(adm),50(ftp)

To remove adm group from user test, type the following command

# usermod -G www,ftp test

Ensure that adm group removed from user test, enter

# id test

Output

uid=503(test) gid=501(www) groups=501(www),50(ftp)

Change existing user test primary group to ftp, enter

usermod -g ftp test

Output

uid=503(test) gid=50(ftp) groups=50(ftp),501(www)

Please note that “-g” option add user to initial login group (primary group). The group name must exist. And “-G” option add user to a list of supplementary groups

Related Posts:

{ 1 comment… read it below or add one }

dipanjan mukherjee November 4, 2014 at 10:37 am

You have mentioned that to remove a group from a user type like below :
# usermod -G www,ftp test
But suppose we have hundreds of groups then this command is very painful to type. We could do this easily by deleting the mentioned group only by this command :
# gpasswd -d username groupname
# gpasswd -d test adm

Reply

Previous post:

Next post: