You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Git用户权限以兼容www-data所属的文件?

Fixing Git <-> www-data Permission Conflicts for WordPress JSON Files

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:

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:

  1. Add your user to the www-data group
    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 $USER
    
  2. Set proper ownership on your directory
    Make the parent directory owned by your user (so Git works) and grouped to www-data (so the plugin can write):

    sudo chown -R $USER:www-data parent-directory/
    

    Add the setgid bit 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/
    
  3. Grant group read/write permissions
    Ensure both you and www-data can 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 {} \;
    
  4. Fix the .git directory
    Since Git needs exclusive access to its own files, reset ownership of the .git folder 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:

  1. 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 acl to /etc/fstab to make this permanent.)

  2. Set ACL permissions for both users
    Grant read/write/execute access to both your user and www-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:

  1. Create the hook file:

    nano parent-directory/.git/hooks/post-checkout
    
  2. Add this content (replace parent-directory with 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/
    
  3. Make the hook executable:

    chmod +x parent-directory/.git/hooks/post-checkout
    

    Repeat this for post-merge to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 08:37:55