Add/Remove A User To/From A Group

How do I add a user to a group under blackPanther Operating System from a shell prompt or command line?

You can useradd command to add a new user login on the system. The blackPanther OS uses /etc/passwd, /etc/shadow and /etc/group files to store user information.

Add A New User To A Group

Add a new user called sampleuser to a group called ftp, enter:
# useradd -G ftp -d /raid1/home/sampleuser -m sampleuser
# passwd sampleuser

Where,

-G ftp : Add sampleuser to secondary group called ftp. This group must exists on the system.
-d /raid1/home/sampleuser : Path to the sampleuser’s home directory
-m : Create the new user’s home directory if it does not already exist.
sampleuser : Username / login nane

Add Existing User To A Secondary Group

Use usermod command to modify a user’s login definition on the system including group membership. For example add user sampleuser to sales group, enter:

# usermod -G sales sampleuser

Add user tony to sales but also keep his existing membership of ftpusers, enter:

# usermod -G ftpusers,sales tony

Add Existing User To A Primary Group

Add tothist to sales primary group and change its existing group membership:

# usermod -g sales tothist

For more information read following man pages:
$ man usermod
$ man useradd
$ man passwd

Remove Linux User From a Secondary Group ( Supplementary Groups )

User tom is a member of a group called sales and printer. I’d like to remove tom from a group called printer without editing any user configuration text files stored at /etc/ directory?

A. /etc/groups file defines group membership for each user. usermod command has -G option to set a list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. If the user is currently a member of a group which is not listed, the user will be removed from the group.

Step # 1: Find out user group identity

Use id command:
# id -nG {user-name}
# id -nG tom

Output:

sales printer

Step # 2: Remove user from printer group

Use the following syntax:

# usermod -G {groupname1,groupname2,...} {username}
To keep membership for sales only group (remove user tom from printer group), enter:
# usermod -G sales tom
# id -nG tom

Output:

sales

The following example remove user vivek from all groups except admin, audio, video and powerdev group:

# id -nG vivek

Output:

vivek adm dialout cdrom floppy audio dip video plugdev scanner netdev lpadmin powerdev admin
Modify group membership, enter:
# usermod -G admin, audio, video, powerdev vivek
# id -nG tom

Sample output:

vivek audio video powerdev admin

For more information, read usermod(8) command man page:
$ man usermod

admin

About admin