Git私有服务器推送失败:Permission Denied错误排查求助
You’ve already tried tweaking file and directory permissions (even setting them to 777!) and still hit this frustrating error when pushing to your private Git server:
remote: fatal: Unable to create temporary file '/var/dev/xsl/hub/.git/./objects/pack/tmp_pack_XXXXXX': Permission denied
error: remote unpack failed: index-pack abnormal exit
To own_server:/var/dev/xsl/hub
! [remote rejected] current -> current (unpacker error)
error: failed to push some refs to 'marcin@own_server:/var/dev/xsl/hub'
Let’s break down the less obvious causes and step-by-step fixes for this issue— I’ve troubleshooted similar problems countless times:
1. SELinux or AppArmor Blocking Git Operations
Most modern Linux distributions run security modules like SELinux (RHEL/CentOS) or AppArmor (Ubuntu/Debian) that enforce access rules independently of file permissions. Even with 777 permissions, these modules can block Git from creating temporary pack files in your repository.
Fix:
- Test temporarily: Disable SELinux with
sudo setenforce 0(or stop AppArmor withsudo systemctl stop apparmor) and try pushing again. If it works, the security module was the culprit. - Permanent SELinux fix:
- Label your repository directory with the correct SELinux context:
sudo semanage fcontext -a -t git_system_content_t "/var/dev/xsl/hub(/.*)?" - Apply the context to existing files:
sudo restorecon -Rv /var/dev/xsl/hub
- Label your repository directory with the correct SELinux context:
- Permanent AppArmor fix:
Edit the Git profile (usually/etc/apparmor.d/usr.bin.git) to add allow rules for your repository path, then reload AppArmor withsudo systemctl reload apparmor.
2. Misconfigured core.sharedRepository Git Setting
Git has its own internal permission system for shared repositories. If this setting isn’t tuned correctly, even proper directory permissions won’t let users create pack files during pushes.
Fix:
- On your Git server, navigate to the repository root:
cd /var/dev/xsl/hub - Set the shared mode (use
groupif your users share a common group, orallfor full open access):git config core.sharedRepository group - Sync the repository’s ownership and permissions with your user group (replace
dev-teamwith your actual group):sudo chown -R :dev-team /var/dev/xsl/hub sudo chmod -R g+w /var/dev/xsl/hub
3. Missing Execute Permissions on Parent Directories
You can’t access a directory even if it has full permissions if any parent directory in the path lacks execute (x) permission for your user. For example, if /var/dev/xsl is locked down, marcin can’t reach the hub repository inside it.
Fix:
Check permissions for every directory in the path:
ls -ld /var /var/dev /var/dev/xsl /var/dev/xsl/hub
Ensure the marcin user (or their group) has the execute permission for each directory. Fix missing permissions with:
sudo chmod o+x /var/dev/xsl # Example: add execute access for other users
4. Disk Space or Inode Exhaustion
Sometimes "Permission denied" is a red herring—your server might be out of disk space or inodes (file system entries). When the system can’t create new files, it often returns permission errors instead of the actual issue.
Fix:
- Check available disk space:
df -h - Check inode usage (file entries):
df -i
If either is full, clean up log files, old backups, or expand your server’s storage.
5. Git Shell Restrictions (If Using git-shell)
If you’ve set up git-shell as the login shell for Git users to restrict access, it might be blocking push operations accidentally.
Fix:
- Verify
git-shellis listed in/etc/shells(add it withecho "/usr/bin/git-shell" | sudo tee -a /etc/shellsif missing). - Ensure your user’s shell is set correctly:
sudo chsh -s /usr/bin/git-shell marcin - Double-check the repository doesn’t have push restrictions like
receive.denyCurrentBranchenabled unless intended.
内容的提问来源于stack exchange,提问作者maw2be

