Home » Questions » Computers [ Ask a new question ]

How do I add a user to multiple groups in Ubuntu?

How do I add a user to multiple groups in Ubuntu?

What's the command line utility and the arguments it requires?

Asked by: Guest | Views: 432
Total answers/comments: 5
bert [Entry]

"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"
bert [Entry]

"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."
bert [Entry]

"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)."
bert [Entry]

"Another way of doing this is by copying the group membership of one user to another user like this:

for i in `grep -E ""(:|,)<username>(:,|$)"" /etc/group|cut -f1 -d:` ; do
addgroup <newuser> $i
done

Source: Stevdotorg | Linux - List / Copy group membership for users"
bert [Entry]

"Adding Groups

groupadd group1
groupadd group2

Adding user to the group

useradd -G group1,group2 -d /home/user1 -s /usr/bin/bash user1

id user1

will show the details of user user1"