How To Add A User To A Group In FreeBSD?

In FreeBSD, a group is a list of users. It makes it easier to define the permissions such as read, write, and execute for a system resource that is to be shared among the users of that group. You can set permission to ensure that files are accessible to users of a particular group like accounts, hr, marketing, etc.

Kernel Identifies a user by its UID and group by its GID. The group name to GID mapping is given in /etc/group file. A superuser can modify it by using a text editor. Alternatively, pw  can be used to add and modify a group in FreeBSD.

How to list the groups in a terminal

The list of the groups with their group id is given in the /etc/group file. To display the list of groups in your terminal use the following command –

less /etc/group

How to add a group in FreeBSD

To add a group in FreeBSD we will use pw utility. The pw can be used to add, remove, modify, and display the users and groups. It has various command-line options that you can use in shell scripts.

Now to add a group use the following command –

pw groupadd group_name

For example –

pw groupadd management

A group with the name management will be added to the system.

You can check the newly added group by using –

pw groupshow management

How to add a user to a group

By using pw we can add a user to a group. Now if you use pw groupshow command you will find that there are no members are added in the newly created group. Use the following command to add a user to a group –

pw groupmod group_name -M user_name

For example –

pw groupmod management -M lalit

The option -M will add a user to an empty group or it will replace the existing user of a group.

Use the following command to see the members of a group –

pw groupshow management

As you can see in the image above user lalit is now a member of the management group.

How to add multiple users in a group

To add multiple users to a group you will have to use the option -m with the given command. Now to add another user named vaibhav to the management group run the following command in your terminal-

pw groupmod management -m vaibhav

If you run the following command you will see that the multiple users are added to the group-

pw groupshow management

If you want to know more about the uses of pw  then see its manual page by running following command in your terminal –

man pw

Ok, that’s all for now, I discussed removing a user from a group in FreeBSD in another article. If you have anything to say on this topic then you can write to us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.