home | whoami

How to create simple self-hosted git repositories

tags: linux, git

Server Setup

On Debian/Ubuntu run:

sudo apt-get install git

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

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

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

sudo -u git mkdir /home/git/.ssh
sudo -u git touch /home/git/.ssh/authorized_keys

Create directory and initialize empty git repository. This command should be performed for every new repository.

sudo -u git /bin/sh -c \
    "export GIT_REPO=dotfiles.git; mkdir /home/\$USERNAME/\$GIT_REPO; git init --bare /home/\$USERNAME/\$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 just like me, 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

Links