setting up git daemon

This tutorial explains how to set up a Git daemon on a typical VPS running Linux in two steps:

  1. systemd configuration
  2. firewall configuration

This setup will provide two different ways of accessing the service via the Git client:

systemd configuration

You will need to set up a systemd service to run git daemon on the background. A typical example of such service will look like the following:

# /etc/systemd/system/git-daemon.service
[Unit]
Description=Start Git Daemon

[Service]
ExecStart=/usr/bin/git daemon --reuseaddr --base-path=STORE --export-all --verbose STORE

Restart=always
RestartSec=1s

StandardOutput=journal
StandardError=journal
SyslogIdentifier=git-daemon

User=git
Group=git

[Install]
WantedBy=multi-user.target

You should replace STORE with the full path of the Git repository store you want to service. The --export-all flag is used to automatically service all Git repositories under your store: otherwise git daemon will only service the repositories containing a file named git-daemon-export-ok inside their root directories (i.e. the directory containing the actual bare repository).

To avoid any permission issues, the file /etc/systemd/system/git-daemon.service should be owned by the root user and should be under the root group. The file should have the following permissions: -rw-r--r--.

Once you create this file you will need to start the service by running:

# systemctl enable git-daemon.service
# systemctl start  git-daemon.service

firewall configuration

The Git protocol runs over TCP at port 9418. When running a firewall, do not forget to allow this port. For ufw you can use the following command:

# ufw allow 9418/tcp

To confirm the port is allowed, run:

# ufw status verbose | grep 9418

VPS providers typically set up a firewall on their fresh Linux installs, so you are likely running a firewall even if you never asked for it. The ufw binary is typically not in your users \$PATH, you need to run it under the root user or with sudo.

other resources