How to Add a New Raspberry Pi Group

How do you add a new Raspberry Pi group?

You can create a new Raspberry Pi group with the addgroup command.

For an example of how to do that, see the steps below.

Step 1. Get to the command line

Remote login over ssh or open up a terminal window via the desktop.

Step 2. Add a new group

To add a new group, run this command (and give sudo your password when prompted):

$ sudo addgroup robots

You should see a response like this (the GID may be different on your system):

Adding group `robots' (GID 1003) ...
Done.

Step 3. Verify the group exists using getent

You can verify a group exists using the getent command:

$ getent group robots

You should see a response like this:

robots:x:1003:

The last value should match the GID returned when you created the group.

For more information on how the getent command works, see may article How to List Raspberry Pi Users.

Step 4. Add a user to the new group

To add a user to the new group use the adduser command:

$ sudo adduser pi robots

Don’t worry. It won’t create a new user if one already exists.

You should see a response like this:

Adding user `pi' to group `robots' ...
Adding user pi to group robots
Done.

Step 5. Verify the user has been added to the new group

To verify that the user has been added to the new group, use the groups command:

$ groups pi

You should see a response like this:

pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio robots

Notice that the last group is the group created above.

Step 6. Add a user to groups using usermod

Another way to add a user is to use the usermod command.

sudo usermod -a -G audio,video luke

The command above adds user luke to the audio and video groups.

You can verify that the user was added to the group using the groups command:

$ groups luke

You should see a response like this:

luke : luke audio video gpio 

Step 7. List members of a group

Once you have added users to a group, you can see who has been added to the group by again using the getent command:

$ getent group robots

You should see a response like this:

robots:x:1003:luke,pi

That shows that user luke and pi are members of the robots group.

Troubleshooting

If you would like to see all of the groups in the system, dump the contents of the /etc/group file:

$ cat /etc/group

You can filter down to non system groups using getent again:

getent group {1000..6000}

You should see a response like this:

pi:x:1000:
mitch:x:1001:
darth:x:1002:
robots:x:1003:luke,pi
luke:x:1004:

When a user is created, a group that matches their username is also created. So the values listed are actually all group names.

Conclusion

In this article you learned how to:

  • Add a new group
  • Verify the new group exists
  • Add a user to a new group
  • Verify that the user had been added to the group
  • List and filter groups



About the Author

Mitch Allen works for a robotics company in New England.