The Linux Page

Adding a group to an already logged in Linux user

A group

Once in a while I want to add my user to a group and then right away make use of that group.

Unfortunately, in general Unix kind of forces you to re-log in for the new group to be attached to your account.

However, a command has been added, a while back, to do just that on the fly. Not something you would want to use in a script, but at least, it works for me when doing a few things here and there.

In my case, I wanted to run QEMU, which I hadn't done in a while, and the system told me that it could not open the KVM, the error: kvm permission denied.

The problem was that I was not part of the KVM group, so I ran the following two commands, tried again and it worked! (that is, that permission denied error was not to be seen again)

usermod -a -G kvm alexis
newgrp kvm

Note that it is important that you use usermod to add the group. If you don't, it probably won't work because the gshadow file needs to be updated too. (i.e. I often like to just edit the /etc/group file and add my name at the end of the line of the group where I want to be added. That's easy to remember compared to the usermod command which, if you forget the -a option, can be a killer).

To see that the newgrp worked, use either the id or the groups commands.

Source: Unix Stack Exchange