如何恢复chmod配置,重新以root身份执行Docker命令
Got it, let's get this sorted for you. You mentioned you ran sudo chmod +x /usr/bin/docker to skip typing sudo for Docker commands, but now you want to revert that setup to enforce sudo usage for security reasons. Here's how to do it properly:
Step 1: Restore the Docker binary's default permissions
First, let's fix the permissions on the Docker executable. By default, /usr/bin/docker has permissions set to 755 (readable and executable by all, writable only by root). If you modified this (including adding SUID/SGID bits which might have been the actual way you skipped sudo), run these commands to reset it:
- Reset to standard 755 permissions:
sudo chmod 755 /usr/bin/docker - If you had set a SUID bit (to let non-root users run it as root), remove it:
sudo chmod u-s /usr/bin/docker - If you had set a SGID bit, remove that too:
sudo chmod g-s /usr/bin/docker
Step 2: Remove your user from the docker group (if applicable)
Another common way to skip sudo for Docker is adding your user to the docker group. If you did this earlier, you'll need to remove yourself from it to enforce sudo again:
- Check if you're in the
dockergroup:groups $(whoami) - If
dockerappears in the list, remove your user:sudo gpasswd -d $(whoami) docker - Log out and log back in for this change to take effect.
Step 3: Verify the setup
Test if the change worked by running a Docker command without sudo, like:
docker ps
You should get a permission error (something like permission denied while trying to connect to the Docker daemon socket). Running the same command with sudo should work normally:
sudo docker ps
That's it! Your Docker setup is now back to requiring sudo for all commands, closing the security loophole you mentioned.
内容的提问来源于stack exchange,提问作者Carlos Andres

