Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to reload group assignment without logout and login

📅 2017-Oct-19 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ docker, group ⬩ 📚 Archive

Problem

I was working in a shell in KDE. I installed Docker as described here. To be able to run Docker commands, my username should be in the docker group, so I added that using this command:

$ sudo adduser joe docker

Now I need to logout and login out of my desktop environment for my username’s new group assignments to be picked up. Otherwise, running any Docker command throws this error:

$ docker images
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.32/images/json: dial unix /var/run/docker.sock: connect: permission denied

We can see that our user’s new group assignment is not reflected in the shell by running this command:

$ id

You will find that the docker group will not appear in the user’s group assignment, though the user has been already added to it. Creating a new shell or a child shell does not achieve what we want.

The only way to get the new group assignment is to logout and login from the desktop environment. The problem I have so many other GUI applications open in my desktop environment. I cannot afford to logout and login from my desktop environment, especially for a shell command to work!

Solution

So, what we want is a shell environment that is similar to what is presented when you login directly at a virtual terminal. Thankfully, we can do that by using the - option of the su command, like this:

$ su - joe
$ docker images
It works!

Another alternative is to run a command with a specific group or GID:

$ sg docker -c "docker images"

Tried with: Docker 17.09 and Ubuntu 16.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧