How do I add a user to multiple groups in Ubuntu?
Solution 1
The utility is usermod
and is used like:
usermod -a -G group1,group2 username
Where username
is the user you want to modify and group1
and group2
are the new groups you want that user to join. Running the command without the -a
argument will remove that user from all groups except group1
and group2
.
To check a users group memberships use the groups
command:
groups username
Solution 2
usermod -a -G group1,group2,group3 username
Solution 3
Assuming the user already exists, the easiest way is to just open the file /etc/group
and add the username to the relevant groups that you want them to be a member of. The usernames are comma separated from the other usernames in the group.
You can check by doing a id -G username
to verify if they are members of the groups you intended.
Solution 4
On Debian, and I assume on Ubuntu as well, the canonical way of adding users and adding users to groups is through the adduser script, not useradd. To add a user to a group, just use:
adduser user group
Though using useradd or usermod works as well of course and is probably more cross platform (but the adduser script reads settings from /etc/adduser.conf and is hence usually preferable).
Solution 5
Use usermod
with the -a
and -G
options.
Related videos on Youtube
sage han
Developer based in Sydney. Work mostly with Ruby & Javascript. Using stackoverflow to write down all the weird, rarely used tecnhical incantations that I don't use enough to memorize.
Updated on September 17, 2022Comments
-
sage han over 1 year
What's the command line utility and the arguments it requires?
-
Gauthier over 9 years"How do I add a user to multiple groups in Ubuntu?"
-
wds over 9 years@Gauthier run the above twice, once for each group. I figured that was obvious.
-
Ariel about 3 yearsObviously you can run a command to add a user to a single group twice, but given the way the question is phrased I would expect any answer to explain how to add a user to two groups using a single command once.
-
Admin about 2 yearsThis is completely unrelated to the question asked.