Assigning Users to Groups
Adding Users to Groups
To add a user to a group in Linux, you can use the usermod
command. The -a
(append) option ensures that the user is added to the specified group without being removed from their current groups.
## Add a user to a group
sudo usermod -a -G group_name username
For example, to add the user alice
to the finance
group:
sudo usermod -a -G finance alice
Removing Users from Groups
To remove a user from a group, you can use the gpasswd
command with the -d
(delete) option.
## Remove a user from a group
sudo gpasswd -d username group_name
For example, to remove the user bob
from the it
group:
sudo gpasswd -d bob it
Verifying Group Membership
You can use the id
command to check the groups a user belongs to.
## Check a user's group membership
id username
This will output the user's primary group and all the secondary groups they are a member of.
uid=1000(alice) gid=1000(alice) groups=1000(alice),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare),1001(finance)
By understanding how to add and remove users from groups, as well as how to verify group membership, system administrators can effectively manage user permissions and access rights in a Linux environment.