Restrict Network & File Access for Local AI Agents
In this article
Local AI agents—tools that run on your machine to automate tasks—can be powerful, but they also pose a security risk. Without proper restrictions, an agent could read sensitive files, send data to the internet, or execute commands you didn't intend. This guide shows you how to sandbox your AI agents by limiting their network access and file system permissions, so they can only do what you explicitly allow.
What You'll Need
- A local AI agent (e.g., a Python script using an LLM, a tool like AutoGPT, or a custom agent) installed and running.
- Administrator or root access to your operating system (Windows, macOS, or Linux).
- Basic familiarity with your OS's command line and file permissions.
- Optional: Docker installed if you plan to use containerization.
Step-by-Step
- Identify the Agent's Process and Dependencies. Before restricting anything, know exactly what the agent runs as. On Linux/macOS, use
ps aux | grep agent_nameto find the PID. On Windows, use Task Manager. Note the user account the agent runs under—this will be the basis for permissions. - Create a Dedicated User Account. Create a new, unprivileged user (e.g.,
aiagent) and run the agent under that account. On Linux/macOS:sudo useradd -m aiagent. On Windows: Settings > Accounts > Family & other users > Add account. This isolates the agent from your personal files. - Restrict File System Access with Permissions. Set the agent's home directory to a specific workspace folder (e.g.,
/home/aiagent/workspace). Then, remove read/write/execute permissions for that user on all other directories. On Linux/macOS:sudo chmod 700 /home/aiagentandsudo chown -R aiagent:aiagent /home/aiagent. For system directories, ensure the agent user has no access (default is usually fine). On Windows, use NTFS permissions: right-click folder > Properties > Security > Edit > Remove the user or set Deny. - Use a Firewall to Block Network Access. The most direct way to restrict network is a firewall rule that denies outbound connections for the agent's user or process. On Linux, use
iptablesornftables. Example with iptables:sudo iptables -A OUTPUT -m owner --uid-owner aiagent -j DROP. This blocks all outbound traffic from that user. On macOS, use the Application Firewall (System Settings > Network > Firewall) but it only controls per-app, not per-user—better to use PF (Packet Filter) with a rule likeblock out from any user aiagent. On Windows, use Windows Defender Firewall with Advanced Security: create an outbound rule that blocks the agent's executable path. - Allow Only Specific Domains (If Needed). If the agent needs to reach an API (e.g., an LLM endpoint), create a firewall rule that allows only that IP/domain. On Linux, instead of dropping all, allow specific:
sudo iptables -A OUTPUT -m owner --uid-owner aiagent -d api.example.com -j ACCEPTthen a final DROP rule. On Windows, create an allow rule for the domain before the block rule. - Sandbox with Docker (Recommended for Isolation). Run the agent in a Docker container with no network and a read-only file system. Create a
Dockerfilewith your agent, then run:docker run --network none -v /path/to/workspace:/workspace:ro --read-only --user 1000:1000 agent-image. The--network nonedisables network,--read-onlymakes the container's filesystem read-only, and the volume mounts only the workspace as read-only. This is the strongest isolation. - Use AppArmor or SELinux (Linux). For finer control, create an AppArmor profile that denies network access and restricts file paths. Example profile:
profile aiagent /path/to/agent { network, deny network, /workspace/** rw, deny /** w, }then load withsudo apparmor_parser -r profile. On SELinux systems, usechconor set the agent's context to a confined domain. - Test the Restrictions. Run the agent and attempt to read a file outside the workspace (should fail) and try to ping an external server (should fail). If it succeeds, adjust your permissions or firewall rules.
- Monitor Logs. Check system logs for denied access attempts. On Linux,
journalctl -u aiagentordmesg. On Windows, Event Viewer > Windows Logs > Security. This helps you fine-tune rules.
Common Mistakes
- Forgetting to Apply Rules to the Correct User. If the agent runs as your main user, all restrictions fail. Always verify the user with
whoamiinside the agent's context. - Blocking All Network but Not Localhost. Some agents use local services (e.g., a local LLM). Ensure you allow loopback (127.0.0.1) if needed, or the agent may break.
- Using Relative Paths in Firewall Rules. On Windows, the executable path must be absolute and correct. A typo will cause the rule to be ignored.
- Not Testing After Changes. A rule that seems correct may not work due to ordering (e.g., DROP before ACCEPT). Always test with a simple command like
curl.
Tips
- Use a Virtual Machine for Maximum Isolation. If the agent is untrusted, a VM (like VirtualBox) with a snapshot is the safest—you can revert to a clean state.
- Set Environment Variables for API Keys. Don't hardcode keys in the agent's script. Pass them via environment variables, and ensure the agent user has no access to your shell's profile files.
- Use a Separate Workspace. Keep all files the agent needs in one directory. This makes permission rules simple and reduces the risk of accidental access.
- Document Your Rules. Save your firewall and permission commands in a script so you can reapply them after system updates.
Troubleshooting
- Agent Can't Start After Restrictions. Check if it needs to read a config file outside the workspace. Move the config into the workspace or adjust permissions. Also, ensure the agent has execute permissions on its own binary.
- Network Still Works. Verify the firewall rule is active. On Linux,
sudo iptables -L -vto see packet counts. On Windows, check the rule is enabled and the profile (Domain/Private/Public) matches your current network. - Agent Can Read Files It Shouldn't. Check if the agent is running as root or administrator. If so, your user-based restrictions won't apply. Use a non-root user or Docker with
--user. - Docker Container Has No Network but Agent Needs Localhost. Add
--network hostbut that exposes all network—instead, use--network bridgeand map only the needed port, or use--add-hostto resolve a hostname to 127.0.0.1.
Frequently Asked Questions
- Can I restrict network access on a per-process basis without Docker? Yes, on Linux you can use
iptableswith--uid-owneror--pid-owner(though PID changes). On Windows, you can use Windows Firewall rules per executable. It's possible but less robust than containerization. - What if my agent needs to access the internet for updates? Allow only the specific update server's IP/domain in the firewall. You can also run updates manually outside the restricted environment.
- Is it safe to give the agent read/write access to a specific folder? Yes, as long as that folder is isolated and contains no sensitive data. Ensure the agent cannot write to other parts of the system.
- How do I allow the agent to use localhost for a local LLM? In your firewall, allow outbound to 127.0.0.1. For Docker, use
--network bridgeand map the port, or use--add-host=host.docker.internal:host-gateway. - Do these restrictions affect the agent's performance? Minimal. File permission checks are fast; firewall rules add negligible overhead. Docker adds a small overhead but is acceptable for most tasks.
- Can I use AppArmor on macOS? No, AppArmor is Linux-only. On macOS, use the sandbox tool (
sandbox-exec) or a VM.