FreeBSD add a user to group nixCraft Updated Tutorials/Posts

How do I add a user to group under FreeBSD operating system? How can I add a user named top to existing group on a FreeBSD? How do I add a user to a group in FreeBSD?

You need to use the pw command. The pw command line utility used for the system user and group files, allowing the superuser (root user) an easy to use and standardized way of adding, modifying and removing users and groups. First login as the root using either the su command or sudo command command:
su -
sudo -i

Add existing FreeBSD user to a group and replace existing membership

First, print tom users current group membership with the help of id command:
# id tom
Say, you would like to add existing user tom to a secondary group called ftpusers. Type the command as follows to replaces tom user’s group membership:
# pw usermod tom -G ftpusers
You can add tom to secondary group ftpuser and wwwusers:
# pw usermod tom -G ftpusers,wwwusers
FreeBSD add a user to group with pw command
The -G option Set the default groups in which new users are granted membership. This is a separate set of groups from the primary group, and you should avoid nominating the same group as both primary and extra groups.

FreeBSD add a user to group and keep existing group membership

When you run above commands user is dropped from existing group membership. To avoid that use the following syntax:
pw group mod {groupNameHere} -m {userNameHere}
pw group mod {groupNameHere} -m {userNameHere1,userNameHere2,...}

Again, let us see current group membership of jerry user:
# id jerry
Sample outputs:

uid=1002(jerry) gid=1002(jerry) groups=1002(jerry),1004(wwwusers)

Next, add jerry as group members for ftpusers group while keeping original group membership as it is:
# pw group mod ftpusers -m jerry
Verify it with the id command:
# id jerry
How to add a user to a group in FreeBSD

Task: Add a new user to group

Add a new user named wendy to system and to secondary group called sales:
# pw useradd wendy -G sales
# passwd wendy
# id wendy

Add a new user to secondary group on a FreeBSD
First command adds user wendy to the system with secondary group called sales. Second commands set a password for wendy.

Task: List memebers (users) of a given group name

To list all members of group named sales, run:
# pw group show sales
Or use the grep command with /etc/group file
# grep ^sales /etc/group
Show group memberships of a user named wendy, use the groups command:
# groups wendy
# id wendy
# id -G -n wendy

FreeBSD show user group memebership commands


You just learned how to add a user to a group in FreeBSD. The commands summary is as follows:

  • Add existing user called foo to the sales group: pw group mod sales -m foo
  • To add new user called bar while creating a new account and to the secondary sales group: pw user add bar -G sales && passwd bar
  • Verify new group membership: id userName or pw groupshow groupName

Further readings:

Read pw command man page here:
$ man pw

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

Removing the WordPress admin file editor How many times have you handed over a project to a client, only to receive a phone call two weeks later saying “I think I broke the site…”? Maybe your...
Simple Shell Commands Simple Shell commands for filesystemFilesystem cd ?Change directory - :Change to the previous directory you were inchmod ?Change permissio...
Issues with Rails on CENTOS5 and cPanel ? I was trying to get rails to work through cPanel on Centos 5 and as many of you, I have encountered problems too.Here's what made it work for me ...
Why containers are the best way to test software performance Software performance and scalability are frequent topics when we talk about application development. A big reason for that is an application's perform...
Spread the love

Posted by News Monkey