How to create simple self-hosted git repositories

tags: linux, git

Server Setup

Use your package manager to install git:

Debian/Ubuntu:

 apt-get install git

OpenBSD:

pkg_add git

Create git user account with git-shell as a login shell:

 useradd -m -d /home/git -s `which git-shell` git

-m Create a new home directory for the new user.

Become a git user with doas -u git -s on OpenBSD, or sudo -u git -s on Debian/Ubuntu.

Create .ssh directory and authorized_keys file where your public ssh keys will be put:

mkdir /home/git/.ssh
touch /home/git/.ssh/authorized_keys

Additionally set stricter access mode:

chmod 0700 /home/git/.ssh
chmod 0600 /home/git/.ssh/authorized_keys

Create and initialize empty git repository

This command should be performed for every new repository as a user git:

su -s /bin/sh git

While being the git user, run:

export GIT_REPO=${HOME}/dotfiles.git
mkdir -p "$GIT_REPO" && git init --bare "$GIT_REPO"

Clone your newly created repository:

git clone git@git.svyrydiuk.eu:dotfiles.git

Possible issues

If you use Dropbear as a SSH server, probably you will face following problems.

git clone git@git.svyrydiuk.eu:dotfiles.git


Cloning into 'dotfiles'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

And something like this in the /var/log/auth.log on your server:

May  28 18:52:46 localhost dropbear[16844]: User 'git' has invalid shell, rejected
May  28 18:52:46 localhost dropbear[16844]: Exit before auth (user 'git', 0 fails): Exited normally

The problem could be fixed by adding git-shell to /etc/shells:

 which git-shell >> /etc/shells

Useful links