Setting up a self-hosted Git server

Table of Contents

Objective

This is the first part of an experiment to set up a private self-hosted Git server for a small development team of 4–7 members. The Git server will host bare upstream repositories, and each member of the team will have a fork of the upstream repositories that will serve as the remote for their local working copies. Additionally, the upstream repositories will be mirrored to a public forges such as GitHub and Codeberg.

Theory

Basic terms (repostitory, bare, upstram, remote, working copy, fork, mirror)

Setup

  • Arch Linux
  • Vagrant FreeBSD generic/13
  • Git
  • Gitweb
  • Mini HTTPD

Procedure

Provision FreeBSD Vagrant box

mkdir -p gitserver
cd gitserver
vagrant init generic/freebsd13
vagrant up

Install required packages in FreeBSD box

vagrant ssh
sudo pkg install -y git
sudo pkg install -y mini_httpd

Setup Git user

git config --global init.defaultBranch main
sudo adduser git
su git
cd
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
exit

Setup Gitweb

sudo mkdir -p /usr/local/www/cgi-bin
cd /usr/local/www/cgi-bin
sudo ln -s /usr/local/share/examples/git/gitweb/gitweb.cgi .
cd /usr/local/www
sudo touch index.html
echo '<html><body><h1>Hello, world!</h1></body></html>' | sudo tee index.html
sudo mkdir -p /pub/git

Setup test repository

cd /pub/git
sudo mkdir testrepo.git
cd testrepo.git
sudo git init --bare

Setup Mini HTTPD

cd /usr/local/etc
sudo cp mini_httpd.cnf mini_httpd.cnf~
echo 'host=localhost' | sudo tee mini_httpd.cnf
echo 'port=80' | sudo tee -a mini_httpd.cnf
echo 'user=git' | sudo tee -a mini_httpd.cnf
echo 'nochroot' | sudo tee -a mini_httpd.cnf
echo 'user=nobody' | sudo tee -a mini_httpd.cnf
echo 'dir=/usr/local/www' | sudo tee -a mini_httpd.cnf
echo 'cgipat=cgi-bin/*'

Add SSH authorized keys for users

su git
KEY='...'
OPT='no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty'
echo "$OPT $KEY" >> /home/git/.ssh/authorized_keys
exit

Secure Git account

sudo chsh git -s $(which git-shell)

Create bare repository

Create local repo and add remote on local repo

Now clone remote origin

Secure the git account

Results

References

Quick Links

Author: Abhishek Chakravarti

Created: 2022-08-23 Tue 12:57

Validate