[BUG]
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Problem
The Edit and Write tools do not preserve file ownership and permissions. When editing a file owned by e.g. root:apache with mode 640, the file becomes root:root with mode 644 after the edit.
This indicates the tools replace the file (write new file + rename) rather than modifying it in-place, which resets ownership to the process owner.
Impact
On systems where file ownership matters — particularly web servers running PHP — this causes immediate breakage:
- Blank pages / HTTP 500 errors when the web server (running as apache) can no longer read PHP files that were changed to root:root 640
- Every single edit requires a manual chown/chmod fix afterward
- Easy to forget, leading to hard-to-diagnose production issues
- Must be documented in CLAUDE.md as a workaround, adding cognitive overhead to every session
What Should Happen?
The Edit and Write tools should preserve the original file's ownership (uid:gid) and permissions (mode) after modification — the same way sed -i, patch, or any in-place editor does.
Suggested fix
After writing the file, restore the original ownership and permissions:
import os, stat
# Before writing: capture original metadata
st = os.stat(filepath)
original_uid = st.st_uid
original_gid = st.st_gid
original_mode = stat.S_IMODE(st.st_mode)
perform the write/replace ...
After writing: restore metadata
os.chown(filepath, original_uid, original_gid)
os.chmod(filepath, original_mode)
Alternatively, write to the file in-place (open + truncate + write) instead of using a temp file + rename pattern.
Environment
- Claude Code CLI running as root on RHEL 9
- Editing PHP files owned by root:apache with mode 640
- Both Edit and Write tools exhibit this behavior
Error Messages/Logs
Steps to Reproduce
Create a file with specific ownership
echo "test" > /tmp/testfile.txt
chown root:apache /tmp/testfile.txt
chmod 640 /tmp/testfile.txt
ls -la /tmp/testfile.txt
# -rw-r----- root apache
Use the Claude Code Edit tool to modify the file
ls -la /tmp/testfile.txt
-rw-r--r-- root root _**<-- ownership and permissions changed**_
Claude Model
Opus
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.79 (Claude Code)
Platform
Anthropic API
Operating System
Other Linux
Terminal/Shell
Xterm
Additional Information
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗