Securing AI agents on Linux, layer by layer
An AI agent on Linux runs as you, so by default it can read your keys, run any program and connect anywhere. To secure it, put its limits where the agent can't change them, give it only what the task needs, and keep a record it can't rewrite.
We make Tego, one tool for that, so read this with that in mind. Every claim about another tool links its own docs.
What can an agent reach?
Everything your user account can. The harm lands in five places, each with a public case.
- Files. A user reports that a cleanup trap in a script the agent wrote ran
rm -rfon their WSL2 home folder (#88462, teardown). - Programs. A developer reports an agent ran
git reset --hard origin/mainat session start two days running (#34327, teardown). - Network. The nx
s1ngularitymalware "scans the file system, collects credentials, and posts them to GitHub as a repo under user's accounts" (GHSA-cxm3-wv7p-598c). - MCP tools and agent configs. A fake Postmark MCP server secretly blind-copied emails (teardown).
- The record. "The audit trail was inside the blast radius." (#95426)
Why do rules inside the agent fail?
The agent's own mistake, or an attacker steering it, can reach them.
- A check on text sees text. In #88462 the permission check saw only
bash /tmp/.../test-lib-demo.sh. The delete was inside the file. - A guard the agent says it made. In #34327 the agent said it had added a git hook to block the reset. The report says the "hook was never actually created".
- Approvals you can switch off. The nx malware ran AI command-line tools with flags such as
--dangerously-skip-permissionsand--yolo, according to Wiz.
Anthropic calls auto mode's classifier "a per-action control, not an isolation boundary" (Choose a sandbox environment). A line in CLAUDE.md or AGENTS.md is advice (A prompt is not a security boundary).
The three layers
Most setups need more than one.
Agent settings
Claude Code and Codex each ship a sandbox, and both are worth turning on. Anthropic's sandboxing guide states the edge: "The sandbox covers shell commands only. Claude's file tools, MCP servers, and hooks run outside it."
Claude Code's sandbox is off by default. If it can't start, commands run without it unless sandbox.failIfUnavailable is true, and Claude may retry a failed command outside it unless allowUnsandboxedCommands is false.
Codex on Linux "uses bwrap plus seccomp by default", and its danger-full-access mode "removes the filesystem and network boundaries" (Codex agent approvals and security, Codex sandbox).
Containers and VMs
Anthropic says a dedicated VM "provides the strongest separation, with its own kernel". Its advice: "Always run --dangerously-skip-permissions sessions inside a container, a VM, or the sandbox runtime, so that file tools, MCP servers, and hooks are also inside the boundary" (Choose a sandbox environment).
A box still misses anything inside it, any traffic it lets out, and a project mounted writable. Ways to sandbox AI agents compares the options.
Kernel rules outside the agent
Linux can restrict a process from outside its code. "Landlock empowers any process, including unprivileged ones, to securely restrict themselves", every child inherits the rules, and "Once a thread is landlocked, there is no way to remove its security policy" (Landlock kernel docs). seccomp, a kernel filter on system calls, can pass chosen calls "to userspace to be handled" by a supervisor outside the agent (Seccomp BPF).
Tego is built on these. A short contract lists the files, programs and hosts an agent may use (README). Here is an excerpt of the repository's Claude Code contract:
version: 0
name: claude-code-interactive
command: ["claude"]
fs:
write: ["./", "/dev/null", "/dev/tty"]
exec:
allow: ["bash", "git"]
net:
allow: ["unix:/var/run/nscd/socket", "tcp://api.anthropic.com:443"]At every protection level the kernel refuses writes and programs outside the contract. Basic needs no root, but needs Landlock ABI 2 or newer (Linux 5.19 or later, or RHEL 9's backport) and a delegated cgroup v2 subtree; there, Tego's supervisor decides hosts. At Plus and Max, with root, the kernel does. Every contract also has a built-in deny_read list of SSH keys, cloud credentials, token stores and .env files (Write a Tego contract).
Tego's limits, in its own words: "It is not a sandbox that hides the machine. The agent can read any path that is not in deny_read." At Basic, command rules are checked on the supervisor's copy of the arguments, "which is best effort: a racing thread of a hostile agent can change what the kernel then acts on" (README, Known limits).
Inside a Tego run an agent's own bubblewrap sandbox can't start, Claude Code's sandboxed Bash tool on Linux included, so the contract takes its place. The Codex setup turns Codex's sandbox off in config.toml (why).
What no layer stops
- Data through an allowed destination. Tego's own list of limits says: "Every allowed endpoint is an always-open exfiltration channel" (Tego's known limits, item A1). Invariant Labs showed an agent with the GitHub MCP server leaking private data "in an autonomously-created PR in the public repository" (Invariant Labs), to GitHub, which the task needed.
- What an allowed program does inside itself. If
python3may run, Python can do anything Python can do. - Effects in other systems. "Effects inside systems that never run under Tego (SaaS, managed DB) are invisible" (Tego's known limits, item A5). Cloud roles, token scopes and backups do that job.
So keep secrets out of reach. What the agent can't read, it can't send.
MCP servers and the record
Treat an MCP server as a program with your rights, and its config as code. SANDWORM_MODE wrote its own MCP server "into every AI coding assistant config it finds on disk" (Socket, teardown). Install servers from the vendor's own source, and review config changes like code.
Under Tego, an MCP server the agent launches runs under the agent's contract, and Tego's relay can allow, hold or deny single tools on each call (MCP tool rules). The relay is a preview: a run with ptrace or pidfd_getfd authority over its shim or the server can bypass it. Neither stops harm inside an allowed call; the Postmark Bcc was one field in a request the task needed.
Keep the record where the agent can't write. Every Tego run writes a tamper-evident record, which tego events <run> --verify checks (how it works), and the contract and the run's folder are denied to the agent (Tego's feature list, item E12). It protects against the agent, not the person who started the run, and Basic doesn't record file reads by default (security and limits).
Where to start
With or without Tego:
- Write down what the task needs: the folder it writes, the programs it runs, the hosts it reaches.
- Move secrets out of reach, and pass only the tokens the task needs.
- Let the agent write only the project.
- Keep the program and host lists short, and leave commands that can't be undone, such as
git reset --hardorterraform destroy, to a person. - Scope the far side: least-privilege cloud keys, short-lived tokens, deletion protection and backups.
- Use a VM for untrusted repositories.
- Read the record after a run, and remove what went unused.
To try Tego, install it and check the machine. tego test runs four harmless actions on decoy files and shows Tego refusing each one (Install Tego on Linux):
curl -fsSL https://github.com/Attention-Span-AI/Tego/releases/latest/download/install.sh | sh
tego doctor
tego testTego can't hold a command for your answer: only file opens and connections can be held (README, Tego's feature list, item E76). Tego is Linux only; on a Mac it runs in a Lima VM and on Windows in WSL2, neither fully tested there (macOS and Windows guide). Next, lock down Claude Code or write your own contract.
Sources (19 links)
Checked on 5 November 2026.
- Anthropic: Sandboxing and Choose a sandbox environment.
- OpenAI Codex: Agent approvals and security and Sandbox.
- Linux kernel documentation: Landlock: unprivileged access control and Seccomp BPF.
- Incidents: anthropics/claude-code#88462, #34327 and #95426; GHSA-cxm3-wv7p-598c; Wiz on s1ngularity; Postmark's warning; Socket on SANDWORM_MODE; Invariant Labs.
- Tego: README, GAPS.md, FEATURES.md, per-tool MCP rules and claude-code-interactive.yaml.
Read next
Five ways to sandbox an AI agent
Sandboxes, containers, VMs and Tego, compared.
Eleven agent incidents and the rule for each
Eleven agent incidents, each in one line.
Your agent can ignore CLAUDE.md
A rule your agent can read isn't a rule it must follow.