Claude Code reset to first-run after sudo: ~/.claude.json lost
Running Claude Code as root can leave ~/.claude.json root-owned; the next start silently resets it. Check ownership, chown it back, restore from backups.
When Claude Code runs as root against your own config, through sudo -E, a CLAUDE_CONFIG_DIR handed to root, or a container or IDE that connects as root, the files it rewrites become root’s. Your next start as yourself cannot read ~/.claude.json, shows the first-run welcome and theme picker, and replaces the file with a fresh one, with no message: trusted folders, MCP servers and settings are gone. There is no fix from Anthropic for this case as of November 20, 2026. Before starting Claude Code again, give the files back with sudo chown -R "$USER": ~/.claude ~/.claude.json, then restore from ~/.claude/backups if a copy of your old config is still there. Run Claude Code as the user who owns the config.
What happens
Claude Code keeps two things in your home directory. The folder ~/.claude holds settings, session history, plugins and, on Linux, your login in .credentials.json with mode 0600. The file ~/.claude.json, which Anthropic’s docs say holds “your account details, MCP server configurations, per-project state such as trust decisions” and the options /config writes, is rewritten often. With CLAUDE_CONFIG_DIR set, both live under that directory instead.
A process running as root that rewrites these files leaves them owned by root with mode 600, so your own user can no longer open them. In the open report on Anthropic’s tracker, labelled data-loss and reproduced on 2.1.326, one root run left .claude.json, sessions/, backups/, plugins/ and the new backup all owned by root. The owner’s next start printed nothing on screen or stderr, opened the welcome screen and theme picker of a first start, and replaced the file with a 425-byte config holding only first-start keys. The project entries and settings were gone, and no new backup was made. Making the file root’s with chown, with no root run at all, gave the same result.
The difference from a damaged file is the copy. When .claude.json cannot be parsed, the docs say Claude Code copies it to ~/.claude/backups/.claude.json.corrupted.<timestamp> and, in an interactive session, asks whether to reset. When it cannot be read at all, the reports show no question and no copy.
Why sudo usually is not enough on its own
A plain sudo claude normally does not touch your files. By default sudo resets the environment and, in the sudoers manual’s words, initialises HOME “based on the target user”, so root gets /root/.claude and /root/.claude.json. Your config is reached when root’s run is pointed at it:
sudo -E claude, which keeps your environment,HOMEincluded, or a sudoers setup that keepsHOME.sudo env CLAUDE_CONFIG_DIR=… claudewith your own directory; the reporter notes that with it set “nothing goes to /root/.claude”.- A container running as root with your config mounted into it, or an IDE that reconnects a dev container as root: the reporter’s own trigger was IntelliJ doing so (JetBrains issue IJPL-261990).
Whether the 2.1.329 fix covers it
The 2.1.329 changelog has the line:
Fixed ~/.claude.json sometimes being reset to defaults when a network stall or file lock made it unreadableCopiedIt names a network stall and a file lock, not permissions. A user on the issue reran the unreadable-file test on the official 2.1.329 build (on macOS, with the file at mode 000 rather than owned by root): the file was replaced with an 84-byte first-start config and no backup was created. They asked whether the fix was meant to cover permission errors; no one from Anthropic has answered, and the issue is open. Anthropic’s settings, authentication and directory docs say nothing about running Claude Code as root or with sudo.
How to tell it happened
The sign is the first-run welcome and theme picker on a machine where Claude Code was already set up, after something ran it as root. If you see it, quit before choosing anything and look at who owns the files:
ls -la ~/.claude.json ~/.claude/backups
find ~/.claude ~/.claude.json -xdev ! -user "$USER"CopiedAny path the second command prints belongs to someone else, usually root. If you set CLAUDE_CONFIG_DIR, check that directory instead. A ~/.claude.json of a few hundred bytes that is already yours again means the reset has happened.
Put ownership right, then restore
These are ordinary Linux steps and users’ reports, not instructions from Anthropic. Close every Claude Code session first, including editor extensions and the desktop app, which write to the same files. Then give everything back to your user:
sudo chown -R "$USER": ~/.claude ~/.claude.jsonCopiedWith a custom directory, it is sudo chown -R "$USER": "$CLAUDE_CONFIG_DIR". If you did this before starting Claude Code as yourself, nothing was lost: the file root wrote still has your content.
If the reset already happened, the old content may survive in a backup. In the report, the root run left a copy of the original in backups/, owned by root until the chown above. The docs say Claude Code keeps the five newest copies there, and the reporter warns that with several sessions running, the good one rotates out within minutes, so look straight away. List them by size; the reset ones are small, and one holding your projects is the one to restore:
ls -laS ~/.claude/backups/
install -m 600 ~/.claude/backups/.claude.json.backup.<time> ~/.claude.jsonCopiedReplace <time> with the name of the copy you chose. install -m 600 writes the file as your user, readable by no one else. If no good copy is left, the settings have to be made again: trust each folder as you open it, re-add MCP servers, and set your options in /config.
Keep your own copy as well, since backups/ can rotate past the one you need. The config can hold your account details and, as one user on the issue points out, OAuth tokens and MCP credentials, so the copy stays readable only by you and out of anything synced or committed:
install -m 600 ~/.claude.json ~/.claude.json.keepCopiedIf ~/.claude itself went to root, your login file in it was root’s too; after the chown it is yours again. If Claude Code still says you are signed out, the not-logged-in guide covers which credential it is reading.
Running the agent as the right user
Run Claude Code as the user who owns its config, and keep root out of that config. From a root shell or a script that runs as root:
sudo -iu alice claude
docker exec -it -u alice <container> claudeCopiedHere alice stands for the owner’s login name. For a dev container, the reporter’s fix was setting "remoteUser" in devcontainer.json and recreating the container, so the IDE connects as that user. If root genuinely needs its own Claude Code, let it keep its own config, which a plain sudo -i gives it in /root, and never pass it -E or your CLAUDE_CONFIG_DIR. Separate users per agent are also how a server keeps one agent’s keys from another.
If the reason for sudo was one step that needs root, such as installing a package, run that step yourself rather than the whole agent as root. When Claude Code’s own sandbox is what refuses sudo, that is the “no new privileges” flag, a separate case.