850

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.

0

8 Answers 8

1292

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.
10
  • 82
    sudo usermod -a -G [group-name] [user-name] : Just a quickie for those who only glance at the answer after reading the headline Nov 11, 2013 at 14:50
  • 45
    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, 2014 at 14:20
  • 21
    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, 2016 at 0:05
  • 18
    @con-f-use, if you can run sudo login -f YOURUSERNAME, it will start a new shell session. Use the id command to verify that the new session is in the correct set of groups. However, this isn't "global" - it doesn't apply to terminals that are already open. So, really, logging out is the best option, where possible Mar 31, 2017 at 14:57
  • 4
    You can use the newgrp [group-name] command to add a new group to the user of the current session without logging out and in again. Only the current session will be affected though.
    – stenix
    Nov 21, 2017 at 10:17
260

Adding a user to a group:

sudo adduser user group

Removing a user from a group:

sudo deluser user group
6
  • 5
    not exactly what this question being asked for
    – Tejendra
    Sep 3, 2014 at 8:10
  • 17
    @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, 2015 at 19:39
  • 16
    For me simpler interface is better, that's why I like this one.
    – Betlista
    Aug 11, 2016 at 10:35
  • 5
    This is exactly the question being asked. @knocte, yes, I think it is Debian specific
    – Auspex
    Nov 14, 2016 at 12:58
  • 3
    @Auspex I can confirm that it does not work on Red Hat.
    – Stibu
    Feb 22, 2017 at 9:43
72

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.

2
  • FYI: I think the id command should indicate you were added to the group without needing to exit. id myuser
    – ficuscr
    Jan 3, 2014 at 16:57
  • 6
    Logging out and back in was required for me on Ubuntu 14.10.
    – Adam D.
    Apr 1, 2015 at 0:32
34

I normally use

sudo gpasswd -a myuser mygroup
3
  • 3
    usermod was not available on my system ubuntu 14.04. This worked great!
    – Drew
    Jun 24, 2014 at 15:45
  • This worked great on Debian Stretch too, where usermod was not installed.
    – vhs
    Mar 29, 2019 at 6:36
  • Remember to log out and in again.
    – Flimm
    Jul 7, 2021 at 16:43
15

I'm posting this as an answer because I don't have enough reputation to comment. As @dpendolino's mentioned, for the effects of this command to persist:

sudo usermod -a -G groupName userName

...you need to logout/login again.

However, if you need a shortcut to start using your new group membership immediately (and you have the correct sudo privileges) I have found this work-around:

$ sudo su -
# su [userName]
$ groups

Explanation:

  • sudo su - will give you a root shell
  • su [userName] returns you to a shell with your user
  • groups when run now will show the group you added with the usermod -aG command

In my case I was trying to add the 'docker' group to my user

Before:

$ groups
userName adm cdrom sudo dip plugdev lpadmin sambashare wireshark lxd

After:

$ groups
userName adm cdrom sudo dip plugdev lpadmin sambashare wireshark lxd docker
1
  • Another option to apply the group membership is sudo login -f <username>, as per Aaron McDaid's comment on another answer. Jan 4, 2020 at 20:08
11
sudo usermod -a -G groupName userName

will work just fine, but I had to reboot entirely, just log out and log in again did not do the job...

1
  • I found this too, and I wonder if it's because I'm running Wayland.
    – Flimm
    Jul 7, 2021 at 16:55
8

On Ubuntu, since logging in as root is not enabled, 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.

1

To permanantely add a user to a group, run this command:

sudo usermod -a -G <groupname> <username>

Then log out and log in again (or reboot if necessary).

Explanation:

The usermod command will modify the /etc/group file to list the specified username in the line relevant to that group. If you run grep <groupname> /etc/group, you should see your username in the output.

In Linux, every process is assigned to a user and groups, and any child processes inherit the user and groups of the parent process. So, for this change to take effect and propogate, you need to log out and log in again (at which point /etc/group is read and used). On Wayland, I've found that a full reboot is necessary.

If you run the command groups, you can see if the current process has the expected groups or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .