Even if you’re a relatively experienced Linux user, groups may not be something you think about all that often. In many cases, the basic groups created when you create a new user while setting up your system are all you need.
Still, as you administer your system, you may encounter situations where you need to add a user to a group. It’s easy to add or delete a user from a group on Ubuntu, you just may not be aware of how to do so yet.
What Groups Are Used for in Linux
A group in Linux is exactly what it sounds like: a collection of users. These are used for managing permissions across a number of users instead of modifying them on a per-user basis. It’s worth knowing your way around groups as this concept is used across Unix-like operating systems as a whole, not just Linux.
A user needs to belong to at least one group. This is the user’s primary group. A user can and usually does belong to other groups, known as secondary groups. There is often a limit on how many secondary groups a user can belong to, but this isn’t something you’ll run into often.
Of course, groups aren’t the only way to handle Linux permissions. For dealing with permissions on a per-user basis, see our guide to the chmod command.
Add a User to a Group in Ubuntu
There are two commands you should know when you want to add a user to a group on Linux. These are the
groups command and the
usermod command. The former lets you see which groups you’re already assigned to. Simply run the following:
Before you add a user to a group, you want to make sure the user actually exists. To do this, you can use the
getent command. To list all groups, run the following:
You’ll see a list of groups, as well as what users are assigned to them.
To add a user to a group, use the
usermod command. Typically, you’d do this with a command like the following:
sudo usermod -a -G group username
With the above command, replace
group with the name of the group you want to add the user to, and
username with the name of the user. For example:
sudo usermod -a -G sudo kwouk
This would allow the user in question to use the
sudo command. Since only users in that group can use
sudo, you can only run the
usermod command if you’re already in that group.
While this guide focuses on Ubuntu, to add a user to a group on other distros, you would use the same exact steps.
Creating a New Group in Ubuntu
Sometimes you’ll run into situations where you need to add a user to a group, but that group doesn’t exist. In this case, you’ll need to create the group yourself, which is where the
groupadd command comes in.
This command is very easy to use. Simply run it under
sudo with the name of the group you’d like to create. For example:
sudo groupadd newgroup
The above would create a group known as “newgroup.” This command isn’t one you may ever need to run, but installing certain software can require you to create new groups. To add a user to a group you just created, use the same steps outlined above.
Remove a User From a Group in Ubuntu
Now you know how to add a user to a group on Linux, but what about removing them from a group? This can vary across Linux distributions, but fortunately it’s easy on Ubuntu to remove a user from a group.
deluser command, as the name implies, is often used to delete a user entirely. You can also use it on Ubuntu to delete a user from a group, as well as on other distros like Debian where the package is also available. To remove a user from a group, use the command the following way:
sudo deluser username groupname
Always make sure to double check that the group name is present. If you run the command without a group name, it will simply delete the user entirely. For example, the following command will remove the user from the group “sudo.”
sudo deluser kwouk sudo
If you omitted the final “sudo” from the above command, it would delete the user “kwouk.”
It can help to double check what groups a user is already assigned to. To do this, simply run the following:
sudo groups username
An Easier Way to Add and Remove Users From Groups
Plenty of people would prefer using a graphical tool on Ubuntu to add users and remove them from groups. If you frequently need to do this and don’t prefer working from the command line, this can make your like much easier. Fortunately, not only is this possible, but easy to do as well.
To do this, you’ll need to install the “gnome-system-tools” package, which contains a few different utilities. We’re only interested in the “Users and Groups” application, but we need to install the entire package anyway. To do this, run the following command:
sudo apt install gnome-system-tools
Answer yes to the prompts, then wait for the packages to download and install. Once the installation is complete, launch the Users and Groups application.
On the left side, you’ll see a list of the users on your system. To add and remove users from groups, click on Manage Groups. You’ll see a list of all the groups available in your installation of Ubuntu. Add users to groups by clicking Properties and checking the checkbox next to their names. Delete users from groups by unchecking the box.
New Ways to Manage Permissions in Ubuntu
While its Unix roots date back to the 1970s, Linux is constantly evolving. This doesn’t just mean better hardware support and performance tweaks either. As Linux evolves, we’re seeing more ways to manage users and permissions.
Among other things, Ubuntu 19.10 brings Gnome version 3.32 which adds app permission control. This will let you use smartphone-style app permissions. While this feature is still in its infancy, it could end up making the Linux desktop more secure. For a look at other new things on the way, see our guide to the new features coming in Ubuntu 19.10.