FreeBSD add a user to group

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

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

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


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Tim May 29, 2009 @ 13:29

    On the FreeBSD systems I work with, I have to use the “-n” (name) switch before the username, or the command doesn’t work.

  • thommy Dec 13, 2009 @ 23:02

    It used for me BUT it kicked my main user out of
    the group wheel. Seems like you have to append all groups, the user is in, or else he is not in the groups anymore, he used to be!

    As for me, I now have no chance to remote administrate my server as root-remot-login is deactivated and this was the only-wheel-user :-(

  • thommy Dec 13, 2009 @ 23:02

    used = worked! plz change in above post and delete this one ;-) thx

  • Will Jan 17, 2010 @ 20:38

    Thommy, I’ve encountered the same problem. What did you mean by ‘used = worked!’?

  • rblon Mar 1, 2010 @ 15:22

    this advise SUCKS…
    I did the same as Tim and thommy and I am unable to get root access, because I am kicked out of the wheel group.
    Meanwhile ssh access for my root account is disabled, which means I just had to put in a support request with my data center.
    Of course, I should have know better before simply using the command.
    I advise anyone who wants to add a ‘user’ to a ‘group’ to use:
    pw groupmod ‘group’ -m ‘user’
    But I am afraid you will only read this comments after you screwed up…

  • rblon Mar 1, 2010 @ 15:25

    i meant “same as thommy and Will”

  • skvortsov Sep 9, 2010 @ 22:34

    Are you guys sure it kicked you from the wheel group? I tried this command and /etc/group no longer showed my user in wheel, but I was still able to su, and the command “id myname” showed that I was in fact a member of both groups. This is on FreeBSD 8.1.

  • sailorboy Jan 1, 2011 @ 13:43

    They filed to mention that the user gets pulled out of all their groups that isn’t listed in the list.
    So if user jerry is in the groups mail, pop, imap, and office
    and you execute
    # pw usermod jerry -G ftpusers
    Jerry is now in ONLY group ftpusers
    Need to issue the command
    # pw usermod jerry -G ftpusers, mail, pop, imap, office

  • fadm May 2, 2011 @ 1:49

    The correct command for this is “pw groupmod operator -m test”.
    This way user test will be added to group operator without replacing or overwriting anything.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum