Docker: permission denied on /var/run/docker.sock

Linux medium dockerpermissionslinux
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.45/version": dial unix /var/run/docker.sock: connect: permission denied

What you see

docker ps fails immediately with permission denied on /var/run/docker.sock, while sudo docker ps works. Nothing is wrong with the daemon.

Why it happens

The socket is owned by root:docker with mode 0660. Your user can read the file but cannot open it, because it is not in the docker group. A re-login is required after the group is added; an open shell keeps the old group list.

Fix

1. Confirm the socket and its group.

ls -l /var/run/docker.sock

Expected: srw-rw---- 1 root docker.

2. Add your user to the group.

sudo usermod -aG docker "$USER"

3. Apply the group in the current shell.

newgrp docker

Or log out and back in. id -nG must list docker.

4. Test without sudo.

docker ps

5. If the group does not exist, the daemon may be rootless.

systemctl --user status docker
ls -l "$XDG_RUNTIME_DIR/docker.sock"

Rootless Docker uses a per-user socket and needs DOCKER_HOST set, not group membership.

6. If the daemon runs in a VM or WSL, check inside that context.

WSL users often add the group on Windows but run the CLI inside the distribution, or the reverse.

A warning about the group

Membership in docker is effectively root: anyone in it can mount the host filesystem into a container. On a shared server, prefer rootless Docker or a sudo rule limited to the commands you need.

Verify

docker version --format '{{.Server.Version}}'
docker run --rm hello-world