Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 select to make this work.

share|improve this question
up vote 349 down vote accepted

The useradd command will try to add a new user. 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 user will need to logout and log back in to see their new group added.

share|improve this answer
42  
sudo usermod -a -G [group-name] [user-name] : Just a quickie for those who only glance at the answer after reading the headline – Programster Nov 11 '13 at 14:50
2  
What does the -a do? It seems to be depreciated / doesn't work with it but the command works with it removed. I can't seem to find reference in a few versions of man... but, I have seen it in so many examples - what does it do? – wilhil Aug 9 '14 at 1:05
13  
I think the preferred way now is sudo adduser user group. It is simpler and cleaner syntax. See the response from @Bai. – ctbrown Aug 14 '14 at 14:20
1  
@wilhil: man usermod: "-a, --append - Add the user to the supplementary group(s). Use only with the -G option.; -G, --groups GROUP1[,GROUP2,...[,GROUPN]]] [...] If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list." – Adam Michalik Jan 26 at 15:03
1  
Is there a way to get around the "logout and back in" part? Some type of update-groups command, maybe? – con-f-use Mar 19 at 0:05

Adding a user to a group:

sudo adduser user group

Removing a user from a group:

sudo deluser user group
share|improve this answer
    
not exactly what this question being asked for – Tejendra Sep 3 '14 at 8:10
3  
@Tejendra's comment seems to presuppose that using useradd is desirable/mandatory to answer OP's question; Perhaps this answer is just missing words to indicate that adduser/deluser is a better alternative. – sage Jul 28 '15 at 19:39

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.

share|improve this answer
11  
Please make it the part "need to logout and login" bold. – Jin Kwon Dec 17 '13 at 6:36
    
FYI: I think the id command should indicate you were added to the group without needing to exit. id myuser – ficuscr Jan 3 '14 at 16:57
5  
Logging out and back in was required for me on Ubuntu 14.10. – A.Danischewski Apr 1 '15 at 0:32

I normally use

sudo gpasswd -a myuser mygroup
share|improve this answer
1  
usermod was not available on my system ubuntu 14.04. This worked great! – Drew Jun 24 '14 at 15:45

On vmware ESXi (usermod (pwdutils) 3.2.15, dated 2006), it is usermod -A GROUP USER.

share|improve this answer

On Ubuntu, since there is no root user, users in the sudo group can elevate privileges for certain restricted commands. Any restricted command must be prepended with sudo to elevate privilege.

sudo usermod -a -G group user 

will add the existing user user to a supplemental group named group. The user's primary group will remain unchanged.

share|improve this answer

to add user to new group without affecting the current group use + before the group name, ofcourse this will not work with the primary group, user should have only one primary group and any others group.

in this example create new group called "asmdba" and add it to the other group

$ id -a
uid=1001(oracle) gid=100(oinstall) groups=100(oinstall),101(dba)
$ groupadd -g 102 asmdba
$ usermod -G +asmdba oracle
$ id -a   
uid=1001(oracle) gid=100(oinstall) groups=100(oinstall),101(dba),(102)asmdba

note the new group added and it did not replace the dba group

if I did not add the + it will override all current others group. note this test done is Solaris 11 OS.

share|improve this answer
    
With Linux, you have to use usermod -a -G. And since this site is for Ubuntu, this answer is dangerous. – muru Apr 6 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.