Add an existing user to an existing group in Linux

If you already have a user on your Linux system and want to add that to an already existing Group on your Linux machine, you can add that user via the usermod command.

If your user is named ‘jack’ and you want to give it a secondary group of ‘www-data’, you can use this command.

$ usermod -a -G www-data jack

Each user also has a primary group assigned to it, you can change that for the user ‘jack’ as such.

$ usermod -g www-data jack

To list all the users that belong to a group, you can use the getent command. This reads the group database and works on every modern distribution.

$ getent group www-data
www-data:x:33:jack

(This post originally pointed at the lid command from the libuser package. That’s since been renamed to libuser-lid and isn’t installed by default on most distributions anymore, so getent group is the portable replacement.)

If you want to list all the groups that this user belongs to, you can use the groups command.

$ groups jack
jack : jack www-data