There are a few ways to add a user to a group in Linux. I will cover two methods using the command line. Both methods require the ability to use the root account or the sudo command. Desktop environments often have GUI tools to do this. If you wish to use the GUI, check your desktop environment’s documentation for assistance.
Using usermod
The easiest way is to use the usermod command. The command below will add the user tyler to the vboxusers group.
# usermod -a -G vboxusers tyler
Check the results with the groups command:
# groups tyler
tyler : tyler cdrom floppy audio dip video vboxusers openldap
The -a option means you wish to add supplementary groups to the user instead of replace them. -G followed by a comma separated list of groups instructs usermod to add the user to the specified groups.
Using vigr
Another option is the vigr command. The vigr command opens the group configuration file, /etc/group, with your default text editor. If you do not have a default defined, you may be prompted to choose from those it finds on your system.
# vigr
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [1]:
Some systems use vi by default. If you aren't familiar with vi, I have a short tutorial covering the basics.
Once vigr opens the group file for editing, it will look something like this:
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
To add the users tyler and guest to the tty group, add them to the end of the appropriate line, separated by ,. The line should like like this:
tty:x:5:tyler,guest
Notice how the file is broken up into four fields separated with : characters. The table below describes the columns of the /etc/group file using our example.
Field | Description | tty | This is the name of the group. |
---|---|
x | This a place holder for a password hash. It is possible to control access to groups using passwords. Most systems now store the hashes in /etc/gshadow file. |
5 | This is the machine friendly group id number. It is how the operating system uses the group internally. |
tyler,guest | A list of users that are secondary members of this group. All users have a primary group defined in the file /etc/passwd |
Check the results with the groups command.
# groups tyler
tyler : tyler tty cdrom floppy audio dip video vboxusers openldap
# groups guest
guest : guest tty