FreeBSD

FreeBSD Add User to Groups

FreeBSD Add User to Groups
In this quick tutorial, we'll show you how to add users to a user group in your FreeBSD system using the account's username. A FreeBSD system can be logged into several different user accounts all at once, although only one user is allowed to give input and thus be in control of the machine. To log into a FreeBSD system, every user would do so with their user account.

Any number of user accounts can be made a part of a group in FreeBSD. Every group has a unique name and GID that identifies it. There is a UserID or UID for every process, and it is used along with the GroupID or GID to assess what is it allowed to change.

Adding a user to a FreeBSD group with the pw command

To add users to a group, we will be using the pw command-line utility. The intended purpose of this command is to provide the root user with a straightforward and intuitive interface to create and customize user groups.

So you'd need root access to follow the instructions in this guide. Use the sudo command as illustrated below:

$ sudo -i

Replacing a user's group membership

The instructions in this section will remove the previously held memberships of the group. To see what groups a certain user 'younis' is a part of, use the id command:

$ id younis

Let's add this user to a group named teamtwo. Enter the pw command as under:

$ pw usermod younis -G teamtwo

To add this user younis to two groups with just one line of command, use the combination below

$ pw usermod younis -G teamtwo,wwwusers

Where wwwusers is the second group, we want to add user younis to. The -G switch used in the command above specifies the first group as primary and the following groups as secondary.

Adding FreeBSD users to a new group without replacing pre-existing memberships

The command syntax presented below can add users to a new group without removing their existing group memberships.

$ pw group mod groupNameHere -m userNameHere
$ pw group mod groupNameHere -m userNameHere1,userNameHere2,…

Let's demonstrate with an example. Check the user groups with the id command:

$ id younis1

Then to add this user to the teamtwo group, use the command syntax as we've used below:

$ pw groupmod teamtwo -m younis1

You can make sure that the previous group memberships are retained by running the id command on that user.

$ id younis1

Adding a new user to a group in FreeBSD

This section will focus on creating a new user and adding it to a pre-existing group. You can use the pw command again to both create the user and add it to a group.

$ pw useradd younis2 -G testing

$ passwd younis2

With the commands above, we've created a new user named younis2, added him to the secondary testing group, and set a password for the user account.

To run a verification on the user account, use the id command again:

$ id younis2

Check the members in a FreeBSD group

The pw command can also be used to display the names of all the users in a certain group. To check the members in our testing group, we'd issue the command below:

$ pw groupshow testing

Alternatively, we can use the grep command on /etc/groupfile.

$ grep ^testing /etc/group

To list the groups a certain user is a part of, run the following group command:

$ groups younis2

You can use the id command to see the same result:

$ id younis2
$ id -G -n younis2

Conclusion

This tutorial was all about adding users to groups in FreeBSD. We learned how to replace memberships for user accounts and how to add users to new groups without replacing their memberships. You've also learned how to create and add new users to pre-existing groups on the go.

Gry OpenTTD vs Simutrans
OpenTTD vs Simutrans
Creating your own transport simulation can be fun, relaxing and extremely enticing. That's why you need to make sure that you try out as many games as...
Gry OpenTTD Tutorial
OpenTTD Tutorial
OpenTTD is one of the most popular business simulation games out there. In this game, you need to create a wonderful transportation business. However,...
Gry SuperTuxKart for Linux
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...