Docker, Linux: Disable Docker Commands for a User but Allow Him Running a Docker Container
Posted May 1, 2020 by Yaroslav Grebnov ‐ 2 min read
Task
At a Linux (CentOS8) machine, create an environment with Docker commands disabled for a particular user, but with a possibility for this user to run a Docker container
Solution approach
Use a restricted shell with modifications disabling some of well-known possibilities to break out from it
Solution details
- Create a user with restrictions:
sudo adduser ruser
- Set a password for ruser (type the password twice):
sudo passwd ruser
- Create a restricted shell:
sudo cp /bin/bash /bin/rbash
- Modify ruser settings. Force him to use restricted shell:
sudo usermod -s /bin/rbash ruser
- Verify correctness of ruser settings in /etc/passwd:
sudo cat /etc/passwd
There should be a line containing:
ruser:...:/home/ruser:/bin/rbash
- Create a directory which will contain commands available for ruser:
mkdir /home/ruser/commands
- Modify PATH environment variable in ruser’s .bash_profile:
vi /home/ruser/.bash_profile
Add the following line to the bottom of the file:
export PATH=$HOME/commands
- Log in as ruser (or execute a su):
su - ruser
- Verify that the majority of commands are not available for ruser:
cd
as the shell is restricted, outputs:
-rbash: cd: restricted
ls
as ls is absent in /home/ruser/commands, outputs:
-rbash: ls: command not found
docker
just like for ls, due to the fact that docker is absent in /home/ruser/commands, outputs:
-rbash: docker: command not found
rbash and vi commands do not work either:
-rbash: rbash: command not found
-rbash: vi: command not found
trying to modify PATH variable does not work:
export PATH=/bin
outputs:
-rbash: PATH: readonly variable
Login as a user with sudo privileges
Create a script allowing ruser to run a Docker container:
vi /home/ruser/commands/runContainer
Add the following code to the runContainer file:
#!/bin/bash
export PATH=/bin
docker run -it someImageID
- Add ruser to docker group:
sudo usermod -aG docker ruser
Log in as ruser
Execute runContainer:
runContainer
Observe that the interactive bash shell has been executed on the container.
- Exit from the container:
exit
- Verify that the PATH contains /home/ruser/commands:
echo $PATH
- Verify that the docker command in the runContainer file cannot be executed:
docker run -it someImageID
outputs:
-rbash: docker: command not found
In case you have a question or a comment concerning this post, please send them to: