OpenClaw - Part 02: OS preparation and containerization
- A dedicated Rocky Linux 10 VM as the first security layer.
- Installing Podman in rootless mode and creating an account without privileges.
- Configuring SELinux and d-bus, testing container isolation.
- Even if compromised, an agent will not be able to obtain root privileges.
Introduction
After presenting the objective of setting up a secure environment for an OpenClaw AI agent, it's time to start the implementation.
This first part will set up the necessary prerequisites.
As explained previously, the idea is to dedicate a VM running Rocky Linux to execute multiple agents in containerized environments.
Dedicating a VM provides a basic but important initial layer of security, defining a boundary instead of running agents on a server or workstation used for other tasks.
Creating the VM
I won't detail the installation of Rocky Linux; in my case, I chose a standard deployment using V10.
I simply created a VM on my XCP-ng pool with the following hardware configuration:
- 4 vCPU
- 6 GB of RAM
- 80 GB of disk space
Click the image to enlarge.
On the network side, the VM is on my standard LAN. We could have dedicated a specific network zone, but we will use firewalld on the server itself to have at least some control over what traffic goes to and from the VM.
Note that the VM is not exposed externally, unlike many VPS instances that were quickly identified by Shodan…
Click the image to enlarge.
There's nothing special to report regarding OS installation; the installer is easy to follow and conforms to RedHat standards.
Click the image to enlarge.
After a few minutes, we have a VM named prdlinage501 with a standard user account, but with sudo access for commands that require it.
Prerequisites
System update
As always after an installation, we start by updating the system:
sudo dnf update -y
and the subsequent reboot to ensure we're starting from a solid foundation:
sudo reboot
Click the image to enlarge.
Click the image to enlarge.
Podman
We begin by installing additional packages. Since we want to use containers to isolate the agents, we start with:
sudo dnf install -y container-tools
Click the image to enlarge.
This will allow us to implement Podman. Podman has been the default containerization choice for Red Hat operating systems for some time now. Rocky Linux is no exception. I usually tend to go back to Docker because I'm historically more familiar with that solution.
However, in this project, sticking with Podman is an extra security guarantee, because, unlike Docker, Podman is rootless by default, which means you don't need administrator privileges on the machine to manage containers. Sometimes this makes things more complex, but we will try to stay in this mode (Docker can now also be rootless, but it's not its default mode).
It is worth noting that other differences exist between Podman and Docker, but both historically use runc as the runtime. However, in Rocky v10, Podman has switched to crun, a rewrite of runc (historically written in Go) in C, which is lighter and faster.
(Feel free to browse this article to fully understand the different layers implemented for containerization.)
Still with security in mind, there are no plans to disable SELinux. However, we will need to adapt some rules to achieve what we want, so we will install the following package:
sudo dnf install -y policycoreutils-python-utils
Click the image to enlarge.
Finally, we add a few useful tools for later:
sudo dnf install -y curl jq tree
The goal is to achieve this result while verifying the different versions of the tools:
podman --version
buildah --version
slirp4netns --version
Click the image to enlarge.
We've already talked about Podman. buildah will allow us to build our images, and slirp4netns will enable network usage for our rootless containers because it simulates a TCP/IP stack in user mode. (Another more performant tool, Pasta, is normally available for this purpose, but I encountered some difficulties with it during the project.)
Please note that your versions may vary depending on when you read this article.
We can now proceed with creating the account dedicated to managing the agents.
Account creation
The idea is indeed not to use the account created during the installation of Rocky Linux, which has the necessary sudo privileges for privileged commands.
Setting up the home directory and permissions
We will use a specific openclaw account (it's not very original, but at least we know what it's used for).
All containers will be managed and launched with this user.
Its personal directory will not be found in its usual location, as it is a somewhat special account for which we want to have a specific structure for our purposes. For this reason, we will use the path /var/lib/openclaw.
Here are the commands to create the account:
sudo useradd --system --create-home \
--home-dir /var/lib/openclaw \
--shell /bin/bash \
--comment "OpenClaw containerized agents" \
openclaw
Click the image to enlarge.
Immediately after, we disable password usage to prevent any direct connection with this account:
sudo passwd -l openclaw
Click the image to enlarge.
We will apply another security measure, namely forcing the home directory permissions to 700. This will ensure that only the owner, i.e., openclaw, can access its home directory (i.e., /var/lib/openclaw).
sudo chmod 700 /var/lib/openclaw
Click the image to enlarge.
Indeed, secrets could be available in this directory structure (used by the agents). Therefore, it is very important that it is as secure as possible.
When we are logged in as the openclaw user, even without root privileges, we will still need some monitoring capabilities, such as access to systemd logs.
Therefore, we grant the user the appropriate permissions:
sudo usermod -aG systemd-journal openclaw
Click the image to enlarge.
Linger
Next, we need to address another issue: ensuring that processes launched by the openclaw user remain active after we exit its context.
Indeed, with systemd (which is responsible for centralizing and managing the lifecycle of all system services and resources), by default, when a user logs out, associated processes are terminated.
However, it is the openclaw user who will launch the containers associated with the agents; therefore, these must survive its logout.
We will therefore enable lingering for the openclaw user using the following command:
sudo loginctl enable-linger openclaw
Click the image to enlarge.
In essence, this instruction allows the openclaw account to host persistent services without requiring root privileges.
We can verify that the function is active with this command:
sudo loginctl show-user openclaw | grep Linger
User Namespace Mapping
Still related to Podman's rootless mode, we need to define the User Namespace Mapping. To simplify, we need to give the openclaw user the right to reserve uid (User Identifier) and gid (Group Identifier) values.
Indeed, when a container is launched, often, the process inside believes it is running as the root user, i.e., uid 0.
However, with Podman, since we are in rootless mode, it is not possible to be associated with the system's uid 0. Therefore, we need to trick the system and map the container's uid 0 to a different uid on the system.
You need to allow the openclaw user to use UIDs for their own purposes, specifically for the containers they will launch.
To do this, we will run the following command:
sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 openclaw
This provides some flexibility for openclaw, allowing it to access these UIDs and GIDs.
You can verify the success of the configuration with the following command:
grep openclaw /etc/subuid
grep openclaw /etc/subgid
Click the image to enlarge.
SELinux and subtree
Next, we enter a configuration area where I am never entirely comfortable: that of SELinux.
Indeed, we will need to map volumes within the containers, and these volumes will be directly taken from the home directory tree of our openclaw user (/var/lib/openclaw).
If we do not configure SELinux, this type of mapping will be blocked. Therefore, we will allow the user to manipulate their home directory for volume mapping in the containers.
But first, let's create the subdirectories that will allow us to properly organize our agents and configurations:
sudo -u openclaw mkdir -p /var/lib/openclaw/instances
sudo -u openclaw mkdir -p /var/lib/openclaw/vault
sudo -u openclaw mkdir -p /var/lib/openclaw/.config/containers/systemd
The /var/lib/openclaw/instances directory will be used to create a subdirectory for each agent, clearly separating them from each other. For example, within /var/lib/openclaw/instances, there will be a subdirectory named it-claw for the first agent described in the introduction of this series of articles.
Next, /var/lib/openclaw/vault will be used for configuring our Vault agents (not in the AI sense), which is our chosen solution for storing secrets useful to the agents (AI this time).
Finally, /var/lib/openclaw/.config/containers/systemd will be used for storing the startup files of our containers associated with our agents (again, AI). But we'll have time to discuss this in more detail later.
Once these directories are created, they need to be declared from the SELinux side:
# Déclarer les contextes SELinux pour ces arborescences
sudo semanage fcontext -a -t container_file_t "/var/lib/openclaw/instances(/.*)?"
sudo semanage fcontext -a -t container_file_t "/var/lib/openclaw/vault(/.*)?"
# Appliquer immédiatement
sudo restorecon -Rv /var/lib/openclaw/instances
sudo restorecon -Rv /var/lib/openclaw/vault
The directories can then be labeled as being associated with container usage, which will prevent SELinux from blocking access despite having the correct permissions on the directories.
Click the image to enlarge.
d-bus
Another issue related to Podman and rootless mode exists: the use of D-Bus. Normally, D-Bus serves as internal messaging between processes. The openclaw user must use this bus to allow Podman to communicate with systemd regarding resource management.
If we switch to the context of the openclaw user using the standard command su -, the system will not create a new D-Bus session for the openclaw user.
Therefore, it is necessary to be able to start a real session with the startup of the dbus-daemon process for the openclaw user.
To do this, we will use the machinectl command. This is a way to create a new and isolated session, as if the openclaw user were connecting physically or via SSH to the server.
By default, the machinectl utility is not included. It is necessary to install the systemd-container package to obtain it:
sudo dnf install systemd-container
Click the image to enlarge.
Checks and initialization
Once this prerequisite is met, we can finally position ourselves within the context of the openclaw user on the virtual machine: machinectl shell openclaw@
Click the image to enlarge.
From here, we must immediately verify that a D-Bus session is in place:
# Variables d'environnement systemd user (doivent être non vides)
echo "XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR"
echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
# Le bus user doit répondre sans erreur
systemctl --user status 2>&1 | head -10
Click the image to enlarge.
We can check that access to logs is possible:
journalctl --user -n 1 --no-pager 2>&1 | head -3
Click the image to enlarge.
Next, we proceed with initialization and tests related to the use of Podman under the identity of the openclaw user:
podman info > /dev/null
This instruction forces Podman to create its entire file tree and configurations before attempting to run containers.
Then, we can verify that everything is correct:
# Vérifier le mode rootless
podman info --format '{{.Host.Security.Rootless}}'
# Attendu : true
# Vérifier l'emplacement du stockage (doit être dans le home openclaw)
podman info --format '{{.Store.GraphRoot}}'
# Attendu : /var/lib/openclaw/.local/share/containers/storage
# Vérifier le driver de stockage (overlay recommandé)
podman info --format '{{.Store.GraphDriverName}}'
# Attendu : overlay
# Vérifier le driver réseau
podman info --format '{{.Host.NetworkBackend}}'
# Attendu : netavark
Click the image to enlarge.
We will also test whether SELinux rules are correctly applied and whether access to the necessary folders from a volume mounted in a container is supported. We start by creating a test file in /var/lib/openclaw/instances, which is within the context of the openclaw user, ~/instances
echo "test selinux volume" > ~/instances/test.txt
Then, we start from an Alpine image (an ultra-light OS) to mount the folder in question into /data within the container and then test access to the file from the container using the cat command
podman run --rm -v ~/instances:/data:Z docker.io/library/alpine:latest \
cat /data/test.txt
Click the image to enlarge.
The ultimate test will be to try running a container that attempts to access the Ollama API hosted on the PC that will run the LLM model we want to use.
To do this, we again start from an Alpine image, with a simple curl to the target server as the argument; in my case, my PC with an NVIDIA GPU:
podman run --rm docker.io/library/alpine:latest \
sh -c "apk add --no-cache curl > /dev/null 2>&1 && \
curl -s http://192.168.10.2:11434/api/tags"
Click the image to enlarge.
If the test is successful, we should see the list of supported models returned by Ollama.
This demonstrates that the use of Podman within the dedicated openclaw user context is functional.
Finally, there is only one thing left to do: improve the ergonomics of future sessions with the openclaw user through bash customization (thank you, AI):
cat >> ~/.bashrc <<'EOF'
# --- OpenClaw environment ---
# Raccourci pour la session systemd user
export SYSTEMD_PAGER=
# Éviter que systemctl utilise less quand on ne le veut pas
# PATH : binaires npm si jamais on en installe en user (rare en containerisé)
export PATH="$HOME/.local/bin:$PATH"
# Alias utiles
alias oclogs='journalctl --user -f'
alias ocstatus='systemctl --user status'
alias ocps='podman ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
EOF
Click the image to enlarge.
Finally, we exit the session using exit to return to the current user.
We can ensure that the systemd branch of the openclaw user account remains active (which will allow services to run) using the following command:
sudo systemctl status "user@$(id -u openclaw).service" --no-pager | head -10
If the output includes "active (running)", then everything is fine.
Click the image to enlarge.
Conclusion
We have now reached the end of this section dedicated to preparing the operating system and the containerization components required for the project. Security always comes at a cost, and staying in a rootless environment requires some configuration complexities.
We are now on solid ground for what follows. Starting by ensuring your execution environment is well-maintained and that you have an up-to-date operating system, correctly configured, is a top priority for any production deployment.
Our future containerized agents will already be isolated from the system itself, with no ability to acquire root privileges even in the event of compromise.
Next, we will tackle Vault and Nginx. But that's planned for a future article.