8/6/2015 - 1:07 PM

Add users to the same group in Unix

Add users to the same group in Unix

Add users to the same group in Unix

This might be useful if you want apache and the user account used for administration (i.e. not root, due to SSH security risks) to both have permissions to do things to a file or directory, but not allow every other user to do the same. It can get tiresome when you have to change ownership every-time you need to make a change to a server file that apache needs ownership of.

First create the group:

sudo groupadd <group_name>

Show all users to ensure the user you want to add to the group is there with:

awk -F: '{ print $1 }' /etc/passwd

Add the user to a secondary group (don’t remove them from other groups, this would be tragic if you removed yourself from sudo accidentally and blocked SSH root login…):

usermod -G <group_name> <user_name>

Check user has been added to group:

groups <user_name>

Repeat for the other users, then change access group for the file/directory (-R for directory child items too):

chgrp <group_name> <path>

Now change the access rights for the file/directory to suitable access rights for the group (775 gives write access to both owner and group, but not anyone else) (again with -R if needed):

chmod 775 <path to change>

Warning: Only do this on files that another user NEEDS (like apache) to run, do not to it to every file, it will be a security risk. One could gain access to apache… and group users have the power to add other members to the group without super user permission!