How To Add Linux User To Group

Tags: , , , , ,
advertisement

All Linux users have a user ID and a group ID and a unique numerical identification number called a userid (UID) and a groupid (GID) respectively. Groups can be assigned for a single user or a group or users together for a common security, privilege and access purpose.
/etc/group is an ASCII file which defines the groups to which users belong. There is one entry per line, and each line has the format:

group_name:passwd:GID:user_list

The field descriptions are: group_name - the name of the group. password - the (encrypted) group password. If this field is empty, no password is needed. GID - the numerical group ID. user_list - all the group member’s user names, separated by commas. There are two types of group, first is primary user group and other is supplementary group. How to create Linux ‘Admin’ group? Type:

#groupadd admin

How to add a new Linux user ‘eric’  to primary group ‘admin’? Type:

#useradd -g admin eric

#id eric
uid=1000(eric) gid=1000(admin) groups=1000(admin)

How to add a new Linux user to supplementary group1,group2 and group3? Type:

#useradd -g admin -G group1,group2,group3 user

OR

#id user
uid=1289(user) gid=1000(admin) groups=1000(group1),1001(group2),1002(group3)

Edit /etc/default/useradd file and add

GROUPS=group1,group2,group3

Save

#useradd user -g admin

#id user
uid=1289(user) gid=1000(admin) groups=1001(group1),1002(group2),1003(group3)

How to add a existing Linux user to existing group? Type:

#id username2
uid=1023(username2) gid=100(users) groups=100(users)

#usermod -G hr,sales username2

#id username2
uid=1023(username2) gid=100(users) groups=100(users),1003(hr),1004(sales)

How to remove existing Linux user from existing group? Type:

#id username3
uid=1023(username3) gid=100(users) groups=1001(users),1003(admin),1004(sales)

#usermod -g users -G admin username3

#id username3
uid=1023(username3) gid=100(users) groups=1001(users),1003(admin)

How to show Linux Group User lists

#getent groupname

#cat /etc/group |grep groupname |cut -d: -f4

Probably this is a simple tutorial for you but not for Linux beginner and I hope this is helpful. Enjoy reading!

Possibly Related Posts:


Leave a Reply