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

103

29

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

quackingduck

Posted 2008-09-02T21:46:41.590

Reputation: 2 461

Answers

133

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

quackingduck

Posted 2008-09-02T21:46:41.590

Reputation: 2 461

24

usermod -a -G group1,group2,group3 username

sysrqb

Posted 2008-09-02T21:46:41.590

Reputation: 344

8

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.

Rob Wells

Posted 2008-09-02T21:46:41.590

Reputation: 400

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).

wds

Posted 2008-09-02T21:46:41.590

Reputation: 210

1"How do I add a user to multiple groups in Ubuntu?" – Gauthier – 2015-02-13T10:06:23.977

@Gauthier run the above twice, once for each group. I figured that was obvious. – wds – 2015-02-16T10:19:44.820

3

usermod -a -G groupname username

user26850

Posted 2008-09-02T21:46:41.590

Reputation: 143

3

Use usermod with the -a and -G options.

John Millikin

Posted 2008-09-02T21:46:41.590

Reputation: 501

1

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: Stev.Org | Linux - List / Copy group membership for users

user199751

Posted 2008-09-02T21:46:41.590

Reputation:

0

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

Ram Prasad

Posted 2008-09-02T21:46:41.590

Reputation:

-5

First, as an infosec professional I completely agree with the additional security benefits and best practices SUDO provides.
However, the self-proclaimed experts who repeatedly belch forth the tired "I-know-how-but-I'm-not-going-to-tell-you-so-go-learn-SUDO-&-sudoers" mantra are the same individuals who post explicit command-line configurations for complex OS & service installations or modifications INCLUDING THE SUDO command, without considering that newbies are simply copying & pasting these commands into their installations without understanding ANY of the risks or the increased attack surface the changes may be creating. Such behavior creates MORE security issues than sharing the information to allow a user to create alternate root accounts.

**Before using the following commands, it is your responsibility to understand the ramifications of creating and using a root-level account. Use may result in a security compromised system or you may render your system COMPLETELY UNUSABLE.

** You agree by using the information contained herein that any negative impact or results to any system, data or network are entirely your responsibility.

Create additional root-level accounts by running these commands from a terminal session:

sudo su

useradd -G root <newusername>

passwd <newusername>

id <newusername>

You should see something similar to: uid=1001(newusername) gid=1001(newusername groups=0(root).

usermod -o -u 0 -g 0 <username>

(in the usermod command string, the first switch is the letter "O". The characters following the -u and -g switches are each "zero")

id <username>

You should now see something similar to: uid=0(root) gid=0(root) groups=0(root).

I suggest bouncing your system prior to logging on with the newly created root-level user account.

shutdown -r now

These commands work well in most main stream *nix and BSD distributions. Use them wisely.

darkrift2012

Posted 2008-09-02T21:46:41.590

Reputation: