如何调整Git用户权限以兼容www-data所属的文件?
Got it, let's tackle this permission conflict between Git and your WordPress plugin—this is a super common headache when dealing with web servers and version control. Here are a few solid solutions that'll let you stop switching ownership back and forth:
1. Use Group-Based Permissions (Most Recommended)
This is the cleanest approach for most setups. The idea is to give both your user and www-data shared access via a common group:
Add your user to the
www-datagroup
This lets your account inherit permissions from the web server's group. Run this command, then log out and back in for the change to take effect:sudo usermod -aG www-data $USERSet proper ownership on your directory
Make the parent directory owned by your user (so Git works) and grouped towww-data(so the plugin can write):sudo chown -R $USER:www-data parent-directory/Add the
setgidbit to the directory—this ensures any new files created (by either Git or the plugin) inherit the directory's group instead of the creator's primary group:sudo chmod g+s parent-directory/Grant group read/write permissions
Ensure both you andwww-datacan read and write files in the directory:sudo chmod -R g+rw parent-directory/For directories, you also need execute permissions to navigate into them—fix that with:
find parent-directory/ -type d -exec chmod g+x {} \;Fix the
.gitdirectory
Since Git needs exclusive access to its own files, reset ownership of the.gitfolder back to your user:sudo chown -R $USER:$USER parent-directory/.git/
2. Use ACLs (Access Control Lists) for Fine-Grained Control
If group-based permissions feel too broad, ACLs let you assign specific permissions to individual users without relying on groups. This is great if you have multiple users or complex access rules:
Enable ACL support (if not already active)
Most modern Linux filesystems (like ext4) support ACLs by default, but double-check your mount options. If needed, remount with ACL enabled:sudo mount -o remount,acl /(Add
aclto/etc/fstabto make this permanent.)Set ACL permissions for both users
Grant read/write/execute access to both your user andwww-data, and set default rules so new files inherit these permissions:# Apply to existing files/directories sudo setfacl -R -m u:www-data:rwX parent-directory/ sudo setfacl -R -m u:$USER:rwX parent-directory/ # Set default ACLs for new files/directories sudo setfacl -R -d -m u:www-data:rwX parent-directory/ sudo setfacl -R -d -m u:$USER:rwX parent-directory/The
X(capital X) means execute permission only for directories, not files—safer than giving all files execute access.
3. Git Hooks (Quick Workaround, Not Ideal)
If you need a temporary fix while setting up the above solutions, you can use Git hooks to auto-adjust permissions after pull/merge operations. Create a post-checkout and post-merge hook in your .git/hooks directory:
Create the hook file:
nano parent-directory/.git/hooks/post-checkoutAdd this content (replace
parent-directorywith your actual path):#!/bin/bash sudo chown -R $USER:www-data parent-directory/ sudo chmod -R g+rw parent-directory/ find parent-directory/ -type d -exec chmod g+x {} \; sudo chown -R $USER:$USER parent-directory/.git/Make the hook executable:
chmod +x parent-directory/.git/hooks/post-checkoutRepeat this for
post-mergeto cover pull operations.
Note: This is a band-aid solution—you'll still need to enter your sudo password every time you run a checkout/merge, so it's not as clean as the first two options.
Testing the Fix
After setting up either solution 1 or 2:
- Try running a Git pull/commit to confirm no permission errors.
- Have the WordPress plugin write to the JSON file, then check the file's ownership/permissions to ensure both users can access it.
内容的提问来源于stack exchange,提问作者lowercasename

