Jump to content

FreeBSD Add User to Groups


Linux Hint

Recommended Posts

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

word-image-54.jpeg

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

word-image-55.jpeg

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

$ pw usermod younis -G teamtwo

word-image-56.jpeg

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

$ pw usermod younis -G teamtwo,wwwusers

word-image-57.jpeg

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

word-image-58.jpeg

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

$ pw groupmod teamtwo -m younis1

word-image-59.jpeg

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

$ id younis1

word-image-60.jpeg

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

word-image-61.jpeg

$ passwd younis2

word-image-62.jpeg

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

word-image-63.jpeg

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

word-image-64.jpeg

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

$ grep ^testing /etc/group

word-image-65.jpeg

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

word-image-66.jpeg

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...