How to Configure Sudoers to Allow Git User to Run ‘git pull’ as www-data Without Syntax Errors


12 views

When trying to configure sudo permissions for the 'git' user to execute 'git pull' as 'www-data', many administrators encounter unexpected syntax errors. The core issue stems from how sudoers interprets special characters in usernames.

# This will cause syntax errors:
git ALL=(www-data) git pull

The sudoers file has specific requirements for username formatting. While Unix/Linux allows hyphens in usernames, sudo's parser treats the hyphen as a minus operator, causing syntax errors. This isn't well-documented in most sudo references.

Here are three approaches to solve this problem:

1. Escaping the Hyphen

The most straightforward solution is to escape the hyphen in the username:

git ALL=(www\-data) git pull

2. Using UID Instead of Username

You can reference the user by their UID instead:

# First find www-data's UID:
$ id -u www-data
33

# Then use in sudoers:
git ALL=(#33) git pull

3. Creating an Alias

For complex scenarios, create a User_Alias:

User_Alias WEBUSER = www\-data
git ALL=(WEBUSER) git pull

When implementing this in production:

  • Always use visudo for editing to prevent syntax errors
  • Test with sudo -u www-data git pull after changes
  • Consider restricting the command path for security

Here's a tested configuration that works:

# Allow git user to run git pull as www-data
Cmnd_Alias GIT_PULL = /usr/bin/git pull
git ALL=(www\-data) GIT_PULL

If you still encounter problems:

  • Verify the command path with which git
  • Check for duplicate entries in sudoers
  • Ensure the target directory has proper permissions

When attempting to configure sudo permissions for the git user to execute git pull as the www-data user, many administrators encounter syntax errors related to the hyphen in the username. The standard approach:

git ALL=(www-data) git pull

Triggers a syntax error in visudo because of the hyphen character in "www-data". This is a common pain point when working with default web server users on Linux systems.

The sudoers file has specific syntax requirements for usernames:

  • Usernames containing hyphens must be enclosed in quotes
  • This applies to both the user being granted privileges and the target user
  • The restriction exists to prevent syntax ambiguity in the sudoers file

Here are three valid approaches to solve this problem:

# Solution 1: Quoted username
git ALL=("www-data") /usr/bin/git pull

# Solution 2: Using the numeric UID instead (often 33 for www-data)
git ALL=(#33) /usr/bin/git pull

# Solution 3: Full command path with arguments
git ALL=(www-data) /usr/bin/git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo pull

When implementing this in production:

# 1. Always use visudo for editing
sudo visudo

# 2. Add the line with proper quoting
git ALL=("www-data") NOPASSWD: /usr/bin/git pull

# 3. Consider restricting to specific repositories
git ALL=("www-data") NOPASSWD: /usr/bin/git --git-dir=/var/www/html/.git --work-tree=/var/www/html pull

If you still encounter problems:

  • Verify the git binary path with which git
  • Check www-data's permissions on the repository
  • Test with sudo -u www-data git pull first
  • Ensure the repository has proper SSH keys if using that protocol

When granting sudo access:

  • Always specify the full path to binaries
  • Consider using NOPASSWD only when absolutely necessary
  • Restrict to specific repositories when possible
  • Regularly audit sudo privileges