A quick guide on how to copy group member ship in Linux to another user. Which can be useful when setting up new users on a Linux machine to make sure that users. It is also a way to find out what groups a user is a member of.
Part 1 - Get a list of groups
To get a list of groups as user is a member of can be done by reading the /etc/group file and doing a little bit of processing. This can be done using the following command.
grep -E "(:|,)originaluser(:,|$)" /etc/group|cut -f1 -d:
The complex expression above is built to match specific username in each line of the file. Of which there is 3 different cases ":
This will output a list of group that "
Part 2 - Add another user to the same list of groups
Since we know that addgroup can be used to add a user to a group by doing "addgroup
for i in `grep -E "(:|,)<username>(:,|$)" /etc/group|cut -f1 -d:` ; do addgroup mynewuser $i done
This will then make sure that the user mynewuser is a member of all the same groups that originaluser is.
You should take care using the above as you may give somebody access well beyond what you thought you might have by giving them access to additional groups.
Did You find this page useful?
Thanks for the feeback. Please consider sharing with others.