Running Claude Code safely on a server
The safeguards that matter, in the order that gives the most protection per hour of work.
Many people now run Claude Code on a machine that matters: a small VPS with a website, a home server, a box that holds a database. The tool is built for the interactive case, where you approve each action. The moment you want it to work unattended, you are trading approval prompts for your own safeguards. This guide lists the safeguards that matter most, in the order that gives you the most protection per hour of work.
Know the permission modes
Claude Code has permission modes that decide how much it may do without asking. In the default mode it stops to ask before most actions that change files, run shell commands or reach the network. acceptEdits lets it edit files and run common filesystem commands (mkdir, touch, mv, cp and the like) without asking. plan lets it read, explore and write a plan without editing your source. bypassPermissions, also reachable with --dangerously-skip-permissions, runs everything without asking, and the docs list it for isolated containers and VMs only. For a fully unattended run they require a container, a VM or the sandbox runtime, and a non-root user on Linux and macOS.
The honest reading: if the machine is also your production server, do not run in bypass mode on it. Either keep the prompts, or build the isolation first.
Never run it as root
Create a dedicated user for Claude Code and give it only what the job needs: its project directory, and a narrow sudo rule for the two or three commands it truly requires (restart one service, reload the web server), not ALL. Whatever that user can do, a mistaken command can do. A wrong rm as a normal user wrecks a project folder. As root it wrecks the machine.
sudo adduser --disabled-password claude-work
sudo visudo -f /etc/sudoers.d/claude-work
# claude-work ALL=(root) NOPASSWD: /bin/systemctl restart myapp
Write deny rules for your secrets
Permission rules in settings.json take the form Tool or Tool(specifier) and live under permissions in lists such as allow, ask and deny. Rules are evaluated in order: deny, then ask, then allow. The first match decides, and an allow rule cannot carve an exception out of a deny rule. The docs also say that deny rules block in every mode, including bypassPermissions. A starting point:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./secrets/**)",
"Bash(git push --force *)"
]
}
}
Check the current syntax in the permissions page of the official docs before you rely on a pattern, and test each rule by asking Claude to do the forbidden thing. Remember the limit: a rule that stops Claude from reading a file does not help if the secret is also sitting in a script or in the shell environment. Keep real secrets out of the project directory and out of the environment of the user Claude runs as.
Add a hook as a second lock
A PreToolUse hook can inspect each Bash command before it runs and refuse the destructive ones, and a hook that denies a call blocks it even in bypass mode. Exit code 2 blocks the call and sends your stderr message back to Claude. This catches honest mistakes (rm -rf on the wrong path, a force push, a dropped table). It does not stop a determined attack, because it reads command text. We wrote a step-by-step guide: Claude Code hooks: block dangerous commands.
Back up first, and prove the restore
Every lock above reduces the chance of a bad command. None reduces it to zero, so the real safety net is a restore you have tried. Before you give Claude more freedom on a machine:
- Take snapshots or backups that the Claude user cannot delete (a different account, or a remote target it has no credentials for).
- Restore one onto a scratch machine and check that the service starts. A backup that has never been restored is a hope.
- Keep the configuration in Git, so a bad edit is a revert and not an archaeology project.
Contain the blast radius
- Staging first. Let Claude change a copy and promote the change yourself, or via a deploy step with its own checks.
- A container or VM for risky work. The docs' own dev container reference notes the limit: it does not stop a malicious project from exfiltrating anything accessible inside the container. Mount only what the job needs and give it no credentials it should not use.
- The sandbox. Claude Code ships a sandboxed Bash mode that restricts file system and network access at the operating system level. Read the current sandboxing page for what it covers and what it does not; hooks and the built-in file tools are separate paths.
- Alerts that ship out. If a job fails at night, a message must reach you. A log nobody reads is not monitoring.
A short checklist
- Dedicated non-root user, narrow sudo rule.
- No bypass mode on a machine that is also production.
- Deny rules for secrets and force pushes, each one tested.
- A PreToolUse hook for destructive commands, with a test list.
- Backups the Claude user cannot touch, and one restore you have actually done.
- Everything important in Git; staging before production; an alert that reaches a human.