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:

Contain the blast radius

A short checklist

A free one-page version with a command or test for each check: Server security checklist.