How to add existing user to an existing group?
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
Solution 5
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 shellsu [userName]
returns you to a shell with your usergroups
when run now will show the group you added with theusermod -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
Related videos on Youtube
Sparky1
Updated on September 18, 2022Comments
-
Sparky1 over 1 year
I want to add the Apache user (
www-data
) to theaudio
group. I've read the man page foruseradd
, 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 foruseradd
:$ 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.
-
Programster over 10 years
sudo usermod -a -G [group-name] [user-name]
: Just a quickie for those who only glance at the answer after reading the headline -
ficuscr over 10 yearsFYI: I think the
id
command should indicate you were added to the group without needing to exit.id myuser
-
Drew almost 10 yearsusermod was not available on my system ubuntu 14.04. This worked great!
-
wilhil over 9 yearsWhat 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?
-
ctbrown over 9 yearsI think the preferred way now is
sudo adduser user group
. It is simpler and cleaner syntax. See the response from @Bai. -
Tejendra over 9 yearsnot exactly what this question being asked for
-
Adam D. about 9 yearsLogging out and back in was required for me on Ubuntu 14.10.
-
sage over 8 years@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.
-
Adam Michalik about 8 years@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." -
con-f-use about 8 yearsIs there a way to get around the "logout and back in" part? Some type of update-groups command, maybe?
-
knocte over 7 yearsthis is debian/ubuntu specific?
-
Auspex over 7 yearsThis is exactly the question being asked. @knocte, yes, I think it is Debian specific
-
Stibu about 7 years@Auspex I can confirm that it does not work on Red Hat.
-
Aaron McDaid about 7 years@con-f-use, if you can run
sudo login -f YOURUSERNAME
, it will start a new shell session. Use theid
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 -
stenix over 6 yearsYou 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. -
russoue over 5 years@AaronMcDaid, thanks for the nice tip! There are situations when logging out is not an option and your tip saved me some troubles!
-
vhs about 5 yearsThis worked great on Debian Stretch too, where
usermod
was not installed. -
user643605 over 4 yearsAnother option to apply the group membership is
sudo login -f <username>
, as per Aaron McDaid's comment on another answer. -
klaas over 3 years@con-f-use: I think a
su - USERNAME
will directly activate a new shell with the new groups settings available. From the docs: SYNOPSYS: su [options] [-] [user [argument...]] DESCRIPTION su allows commands to be run with a substitute user and group ID. The-
is a short form for thelogin
parameter. -
Flimm almost 3 yearsRemember to log out and in again.
-
Flimm almost 3 yearsI found this too, and I wonder if it's because I'm running Wayland.