OS-level sandboxing confines an AI agent to a tightly controlled environment, restricting which files, network resources, and system calls it can touch. This guide shows you how to set up such a sandbox on Linux, macOS, or Windows, giving you a practical layer of protection against accidental damage or malicious behavior from an AI you run locally.
What You’ll Need
- A computer running Linux (with systemd), macOS, or Windows 10/11 Pro or Enterprise.
- Administrator (root) access to install software and create users.
- Basic comfort with the command line (for Linux/macOS) or PowerShell (for Windows).
- A specific AI agent binary or script you intend to run (e.g., a Python-based agent or a compiled model runner).
Step-by-Step
Linux (using systemd and AppArmor)
- Create a dedicated system user. Run
sudo useradd -r -m -d /home/aiagent aiagent. This creates a system account with no login shell, home directory at/home/aiagent. - Create a sandbox directory. Make a folder the agent can write to:
sudo mkdir -p /srv/aiagent/dataand give ownership to the new user:sudo chown aiagent:aiagent /srv/aiagent/data. - Write a systemd service file. Create
/etc/systemd/system/aiagent.servicewith content like:Description=AI Agent Sandboxed Service After=network.target [Service] User=aiagent Group=aiagent WorkingDirectory=/srv/aiagent ExecStart=/usr/bin/python3 /srv/aiagent/agent.py NoNewPrivileges=yes PrivateTmp=yes PrivateDevices=yes ProtectSystem=strict ProtectHome=yes ReadWritePaths=/srv/aiagent/data RestrictAddressFamilies=AF_INET AF_INET6 RestrictRealtime=yes MemoryMax=2G [Install] WantedBy=multi-user.target
- Enable and start the service. Run
sudo systemctl daemon-reload, thensudo systemctl enable --now aiagent. - Apply an AppArmor profile (optional but recommended). Create
/etc/apparmor.d/aiagentwith a profile that restricts file access further, then runsudo apparmor_parser -r /etc/apparmor.d/aiagentand addAppArmorProfile=aiagentto the service file.
macOS (using sandbox-exec)
- Create a sandbox profile file. Write a file, e.g.,
aiagent.sb, with:(version 1) (deny default) (allow network-outbound) (allow file-read*) (allow file-write* (subpath "/Users/you/aiagent-data")) (allow process*) (allow sysctl-read)
- Run the agent under the sandbox. Use
sandbox-exec -f aiagent.sb /path/to/agent. This restricts file writes to the specified directory and blocks network access except outbound connections. - Note:
sandbox-execis deprecated but still works on recent macOS versions. For stricter control, consider using a dedicated macOS app sandbox via Xcode, but that requires more setup.
Windows (using AppContainer or Windows Sandbox)
- Use Windows Sandbox (easiest). Enable it via Control Panel > Programs > Turn Windows features on or off > Windows Sandbox. Restart. Then launch Windows Sandbox and run your AI agent inside that virtualized environment. It is isolated from the host.
- For a more persistent solution, create a low-privilege user. Open PowerShell as Administrator and run
New-LocalUser -Name 'aiagent' -NoPassword. Then create a folder for the agent and grant only that user write permissions. - Run the agent as that user. Use
runas /user:aiagent "cmd /c your_agent_command". This limits file access to what that user can reach. - For network restrictions, use Windows Firewall. Create an outbound rule that blocks the agent’s executable from making network connections unless you explicitly allow specific IPs or ports.
Common Mistakes
- Forgetting to set
NoNewPrivileges=yeson Linux. Without this, the agent could escalate privileges via setuid binaries. - Giving the sandboxed user write access to the agent’s own code directory. If the agent is compromised, it could modify its own script and persist. Keep code read-only.
- Allowing all network access. Many agents need internet, but you should restrict to specific domains or ports if possible. On Linux, use
IPAddressAllowin the service file. - Not testing the sandbox before running the real agent. Run a harmless command like
whoamiortouch testfileinside the sandbox to verify it works as expected. - On Windows, using the default user profile. If you run the agent as your own user, it can read your documents and browser history. Always create a separate user.
Tips
- Start with the most restrictive policy and open up only what breaks. This is easier than trying to close holes after the fact.
- Use a separate network namespace on Linux. Add
PrivateNetwork=yesto the systemd service to give the agent no network at all, then use a proxy or a virtual interface if it needs outbound access. - Monitor the sandbox logs. On Linux, use
journalctl -u aiagentto see what the agent is doing. Unexpected file access attempts will show up as denials. - Consider using a full VM or container (like Docker) for extra isolation. OS-level sandboxing is lighter but not as strong as a hypervisor. Combine both if you need high security.
- Keep the sandbox user’s shell set to
/usr/sbin/nologinon Linux to prevent interactive logins.
Troubleshooting
- The agent cannot write files. Check the permissions on the data directory. On Linux, ensure the
ReadWritePathsin the service file matches the actual path. On macOS, verify the subpath in the sandbox profile is correct. - The agent cannot connect to the internet. On Linux, if you set
IPAddressAllow, make sure you included the necessary IPs. If you usedPrivateNetwork=yes, you must disable it or set up a virtual network. On Windows, check the firewall rule. - The service fails to start on Linux. Run
sudo systemctl status aiagentto see the error. Common issues include missingExecStartpath or the user not having execute permission on the script. - On macOS,
sandbox-execgives a syntax error. Ensure the profile file is well-formed. Usesandbox-exec -nto test the profile without running a command. - On Windows, the agent runs but has no network. If you created a firewall block rule, check the scope. You might have blocked all outbound traffic, including DNS. Allow DNS (port 53) separately.
Frequently Asked Questions
- Can I use OS-level sandboxing to completely prevent an AI from leaking data? No. It limits what the agent can access, but if the agent has internet access, it could exfiltrate data it can read. Combine with network egress filtering and consider a full VM for high-security needs.
- Is systemd sandboxing enough for a malicious AI? It provides a strong baseline, but it is not a security boundary as strong as a hypervisor. For truly untrusted code, use a VM.
- Does sandboxing affect AI performance? Minimally. The overhead is negligible for CPU and memory, but network restrictions might slow down model downloads or API calls.
- Can I use AppArmor and systemd together? Yes, they complement each other. systemd handles resource limits and namespace isolation, while AppArmor restricts file paths and capabilities.
- What is the easiest way on Windows? Windows Sandbox is the simplest and most isolated option. It is a lightweight VM that resets on each launch.
- Will the agent be able to access the GPU? On Linux, you may need to add
DeviceAllowrules for the GPU device. On Windows Sandbox, GPU access is limited. For GPU-heavy agents, consider a container with GPU passthrough instead.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Channel TLD Websites: What They Are & How to Get One5.00 min/km
- Django Class Based Views CRUD REST API
- Most Secure OS for Privacy: 3 Picks by Device
- Ryobi One+ Battery System: Is the Switch Worth It?
- Tmux Terminal Upgrade: Is It Worth It?
- Refurbished Tech Buying Checklist