Docker says permission denied on docker.sock? Add your user to the docker group, log in again, and skip chmod 666. Why it works.
You typed docker ps and got permission denied while trying to connect to the docker API at unix:///var/run/docker.sock. Docker is installed and running. Your user just isn't allowed to talk to it. The fix takes one command and one re-login, and below we explain why it works and what it costs you in security.
TL;DR. The Docker daemon listens on a socket file that only
rootand thedockergroup can open. Add yourself to that group withsudo usermod -aG docker deploy(replacedeploywith your username), then log out and log back in. Already-open sessions do not pick up the new group. Do not runchmod 666on the socket. Membership in thedockergroup is effectively root access, so only add people you trust.
We reproduced all of this on a fresh test server: Ubuntu 24.04, Docker 29.1.3 from the docker.io package in apt, a regular user deploy without sudo rights. The commands and outputs in the main steps come from that run. We did not test chmod 666, so that one is advice only.
This is the exact message on a current Docker:
The docker command you type is only a client. The real work is done by the Docker daemon, a background service that runs as root. The client reaches it through a socket: a special file, /var/run/docker.sock, that programs use to talk to each other on the same machine. The message means the client found the file and was refused when it tried to open it. Older Docker versions word the message differently and at greater length, but the cause is the same.
Note what this is not: it is not a network problem and not a broken install. The daemon is up and answering; it simply won't let you in.
Look at who owns the socket. This command lists the file with its permissions:
ls -l /var/run/docker.sockYou should see:
srw-rw---- 1 root docker ... /var/run/docker.socks at the start: this is a socket, not a regular file.rw-rw----: the owner (root) and the group (docker) can read and write; everyone else gets nothing. In octal that is 0660.root docker: owner and group. Only these two may use the socket.This is set on purpose: the unit file /lib/systemd/system/docker.socket contains SocketMode=0660 and SocketGroup=docker. Now check who is in the group:
getent group dockerOn the test server it printed docker:x:112:. Nothing after the last colon means the group is empty. The number 112 (the group ID) can be different on your machine. Your user is not root and not in the group, so the kernel says no.
You need one thing first: access to an account that can use sudo (or the root account), because changing group membership is an admin action. Then run:
sudo usermod -aG docker deployusermod edits a user account.-G docker sets the supplementary group; -a means "append". Never drop the -a: without it usermod replaces all the user's groups with just docker, and you can lose sudo access.deploy is the account being changed. Not sure of yours? Run whoami.The command prints nothing on success. Confirm with id deploy: the list should now end with 112(docker) (or your own group number).
This is where most people think the fix failed. Linux reads a user's groups once, at login. A terminal or SSH session opened before usermod keeps the old list. We checked it: with a deploy session open before the change, id -nG still showed deploy users and docker ps was still denied.
The clean solution is to log out and back in. For an SSH session, type exit and connect again. A new login sees the group and docker ps works.
If you cannot reconnect right now, there are two workarounds. Both worked in our test:
sg docker -c "docker ps"sg runs one command with the docker group switched on, then returns you to normal. Good for a one-off check.
newgrp dockernewgrp starts a subshell where the group is active. Run your Docker commands there; type exit to leave. It does not change your original session, so a fresh login is still the proper fix.
Method | When to use it | Catch |
|---|---|---|
Log out and back in | The normal case, always | You lose the current session |
| One command right now | Only that command gets the group |
| Several commands, cannot reconnect | Works inside a subshell only |
| Quick one-off on an account with sudo | See the next section |
After the new login, confirm that the group is active and that Docker answers:
id -nGdocker psThe first command should list docker among your groups. The second prints a table header (CONTAINER ID IMAGE ...), followed by your containers or nothing at all if none are running. An empty table is success. The error is gone.
This part matters. Whoever can talk to the Docker daemon can ask it to start a container with any folder of the host mounted inside. The daemon is root, so the container is too. We ran this as the unprivileged deploy user:
docker run --rm -v /:/host alpine:3 chroot /host id -un-v /:/host mounts the whole server disk into the container at /host.chroot /host makes that folder the container's root, so it behaves like the real system.id -un prints the current user name.It printed root. A user with no sudo rights just became root on the host. So treat the docker group like the sudo group: add your own deploy account and people you would trust with the root password. Do not add a shared "app" user, a web-facing service account, or a contractor you met last week.
chmod 666 /var/run/docker.sock. It looks like a shortcut: everyone can now open the socket. That also removes the only protection, so any account on the server, including a compromised website process, gets root through Docker. As a rule the change also disappears when docker.socket restarts, so you get a hole and a fix that breaks later. We did not apply it in the lab; this is advice, not a test result.
Typing sudo before every command, forever. It works if your account has sudo rights. A user without them gets a failed password prompt in a terminal, and sudo: a password is required in a script (we saw the second on deploy). Even where it works, it does not fix scripts, cron jobs or Compose files run by that user, and files that containers write into mounted folders can end up owned by root. Use sudo for a quick look, and put the user in the group for daily work.
Logging in as root just to run Docker. It sidesteps the error, and also the habit of working under a limited account. Keep root for administration.
Your current session was started before the change, and Linux reads groups at login. Log out and back in, or use sg docker -c "docker ps" for a one-off check. In our test an open session kept denying access until we reconnected.
Only for users you would trust with root. Group members can start a container with the host disk mounted and become root on the server, which we demonstrated with a single docker run command. If that is not acceptable, give the user no Docker access at all and run the containers yourself.
You can, but you should not. chmod 666 lets every account on the machine control Docker, which means root. It is also likely to be reset when the socket restarts. Use group membership instead.
No. The daemon keeps running as is. Only the user's session needs to be refreshed, because the new group shows up at the next login.
We have not tested those, so we will not guess. This article covers the docker.io package from Ubuntu's apt repository on Ubuntu 24.04. A rootless setup uses a different socket and a different permission model.
/var/run/docker.sock (root:docker, mode 0660) refused your user, who is not in the docker group.sudo usermod -aG docker deploy, then log out and back in. Keep the -a.sg and newgrp are temporary workarounds.docker group equals root on the host. Add only trusted accounts.chmod 666 the socket, and do not rely on endless sudo for scripts.With Docker working for your user, you can run several services together in Docker Compose on one VPS. And when images and logs start eating the disk, see how to find what takes the space and clean it safely.