Linux is unlike other major operating systems in that it gives you freedom. Freedom to use it in whatever way you like, freedom to study and change it, freedom to distribute it however you choose, and freedom to lock yourself out of vital groups.
Here’s what you need to do in case you’ve removed your only user from all groups on Ubuntu.
How Do Groups Work on Linux?
On Linux, groups contain privileged users who are variously able to read, write, or execute files without additional authorization. When you create your primary user account, you are automatically added to your primary groups. On a standard Ubuntu install, these are usually adm, cdrom, sudo, dipb, plugdev, and sambashare.
You can add yourself to other groups using the usermod command:
sudo usermod -aG group_name your_username
The -G switch means that you will be adding the user to a supplemental group, and the -a stands for append, meaning that the group will be added to your list of groups.
Executing the above command without the -a means that the new group will replace all of your old groups.
Most importantly the command will remove you from the sudo group, meaning that your powers will be severely limited. Without being part of the sudo group, you can’t add yourself to the sudo group, you can’t install software, or even update the system.
What to Do if You Accidentally Erase Your User’s Groups
It’s a small thing, but missing that -a can totally ruin your day. Fortunately, the solution is simple, but you can’t fix the mistake from within your Ubuntu install as you no longer have admin privileges. You need a root shell.
First, you will need to access the GRUB menu. Do this by rebooting your PC and repeatedly hitting Escape until the menu appears.
Choose Advanced Ubuntu Options, then Recovery Mode.
When the menu gives you an option for a root shell, choose this. The shell will appear at the bottom of the screen.
Commands executed in the root shell are carried out as root, and on a standard Ubuntu system, do not need a password.
Enter the following command to add yourself to the sudo group:
usermod -aG sudo your_username
Now reboot. When you log back into your system, you will again be part of the sudo group and able to carry out actions that require root privileges.
Rejoin Your Previous User Groups on Linux
Linux logs everything. This can be a problem when your logs grow to a ridiculous size, but if you need to know which groups your user was previously a part of, it can be invaluable.
sudo grep user-setup /var/log/installer/syslog
…will output every time a user was added to a group. Take note of the group names, then, re-add them with:
sudo usermod -aG sudo this_group,that_group,another_group your_user
Note that there is no space between the group names and the commas.
Log out and back in again, and your groups will be fully restored.
Groups Make Linux Administration Easy
So long as you don’t accidentally remove your own privileges, groups are an incredibly powerful tool on Ubuntu and other Linux distros. With them, you can manage large numbers of users and ensure that everyone has appropriate access and permissions for what they need and aren’t able to damage your system by accident.
By using groups, you can choose which people can be trusted enough to read, write, and execute files. It’s easy to move users between groups, and if you want someone to have complete freedom to use a particular file, you can even give them ownership.