Quick way to add user to another user group in Linux

Here is how to add user to be a member of another user’s group in GNU/Linux.
Everytime I have to add user to a group I always try to remember for a few seconds how I did it last time.
Therefore I finally decided to blog it here that I possibly help to somebody out there and help myself easily look for it among the posts in case I forget sometime in the future:
There are two ways to add an user to a group.

1. Add user to a group directly editting /etc/groups

Let’s assume you are in need to add user test to the www-data groupHere is how:

linux-server:~# vim /etc/groups
Put in the file
www-data:x:33:test
Save the file and that should be enough, in the next login with user test you can observe the user is added to the www-data group
By typing the id command in the command line.


linux-server:~$ idid=1000(test) gid=1000(test)
groups=1125(test),1124(www-data)

2. Add user to another user group using the useradd and usermod commands

If you’re creating a brand new user to be membering a group use:

linux-server:~# useradd -G
linux-server:~# useradd -G www-data test
linux-server:~# passwd test

If for clarity you’d like to add an already existing user using a linux command consider using usermod

linux-server:~# usermod -a -G www-data test

Share this on:

Download PDFDownload PDF

Tags:

Leave a Reply

CommentLuv badge