How to add existing user to an existing group?

I want to add the Apache user (www-data) to the audio group. I've read the man page for useradd, but I'm not having any luck. I'm running xubuntu 11.10. Here's what I'm doing:

$ sudo useradd -G audio www-data
useradd: user 'www-data' already exists

If I leave out the -G option, bash, prints the help info for useradd:

$ sudo useradd  audio www-data
Usage: useradd [options] LOGIN
Options: -b, --base-dir BASE_DIR       base directory for the home directory...

It's not clear to me from the man page what options I should use to make this work.


Solution 1:

The useradd command will try to add a new user. Since your user already exists this is not what you want.

Instead: To modify an existing user, like adding that user to a new group, use the usermod command.

Try this:

sudo usermod -a -G groupName userName
  • The -a (append) switch is essential. Otherwise, the user will be removed from any groups, not in the list.

  • The -G switch takes a (comma-separated) list of additional groups to assign the user to.

In general (for the GUI, or for already running processes, etc.), the user will need to log out and log back in to see their new group added. For the current shell session, you can use newgrp:

newgrp groupName
  • newgrp adds the group to the current shell session.

Solution 2:

Adding a user to a group:

sudo adduser user group

Removing a user from a group:

sudo deluser user group

Solution 3:

After adding to a existing user:

usermod -a -G group user  

You may need to logout and login to get the groups permissions from /etc/group.

Solution 4:

I normally use

sudo gpasswd -a myuser mygroup