Setup you own GIT Server in 10 Minutes

5 minute read

Setting up a personal GIT Server with bare mode on Linux is easy

You probably already know how to setup you very local git repository. But what if you want to work on this super secret codebase with your friends and colleagues? It comes easy and cheap nowdays, that you setup a private git repository anywhere on the major git SaaS platforms.

Thereby it’s so easy to setup a shared git repository by yourself with just an inplace Linux or Unix Server. It could also just be your very local MacOS or Linux workstation. Here is what you need:

Caution: This is a very quick glitch to cater GIT to a very small group of close individuals who will have access to a machine. A professional public git infrastructure might need additional effort.

1. Install an Git and SSH Server on your system

Some systems might already have the SSH daemon installed. Here is how you install SSHD just in case you need it.

1# Ubuntu
2$ sudo apt install sshd git
3$ sudo /etc/init.d/sshd start
4
5# FreeBSD 
6$ sudo pkg install sshd git
7$ sudo sysrc sshd_enable=yes && service sshd start

2. Create a special place for your new repository somewhere on the Server Fileysstem

When using the future git remote (that’s git language for a repository on a git server) just by your own, it’s probably the easiest to place all your repositories in the home folder like this:

1# Any Unix style OS
2# single personal single user remote
3$ mkdir ~/repos

The moment you start thinking about sharing the remote with other people, they will have access to the computer system. So you might want to create a shared folder outside your home directory everyone can access. Otherwise people might potentially need access to your user’s homefolder aswell.

1$ mkdir /var/repos

Since your future remote’s identifier might look similiar to something like user@yourhost.com:/your/complete/path/repos/your-project-folder you might want to create a link in the directory root folder for later convenient access.

1$ ln -s ~/repos /r

Your repository will be available remote as user@yourhost:/r/your-project-folder. Remember to adjust the access rights to the repositories. Especially when you start working collaboratively with more than just one system user account, adjusting the local access rights is vital.

3. Initialize your repository in bare mode

The bare mode is a bit different from the default local mode. It’s purpose usually is limited to being the central repository, every other participant is cloning their local repository from. Bare repositories are not intended for local development work.

 1$ mkdir /r/a-new-repo
 2$ cd /r/a-new-repo
 3$ git init --bare
 4$ ls -l
 5total 32
 6drwxrwxr-x 2 glitch glitch 4096 Okt 29 21:53 branches
 7-rw-rw-r-- 1 glitch glitch   66 Okt 29 21:53 config
 8-rw-rw-r-- 1 glitch glitch   73 Okt 29 21:53 description
 9-rw-rw-r-- 1 glitch glitch   21 Okt 29 21:53 HEAD
10drwxrwxr-x 2 glitch glitch 4096 Okt 29 21:53 hooks
11drwxrwxr-x 2 glitch glitch 4096 Okt 29 21:53 info
12drwxrwxr-x 4 glitch glitch 4096 Okt 29 21:53 objects
13drwxrwxr-x 4 glitch glitch 4096 Okt 29 21:53 refs

The above listing shows that the root level of the bare git repository already contains files. If you create a local repository via git init it will be mostly empty except for the hidden .git folder. This hidden git folder will basically contain the very same file structure as what you see in the root folder of a bare git folder. It’s all about administrational files git needs to keep track of changes and versioning.

4. Create User Accounts for your friends (optional)

Just in case you use the GIT server just on you own, you can of course skip this step. As long as you have shell access with at least one account, this will work fine.

You can also potentially share the same login credentials with several other people for working on the remote git repository. It will work fine, since GIT does not keep track of the commiters identify via login credentials. GIT just receives your commits signed by the local git name and email configuration you can see with git config --list.

Let’s say you want to be able to restrict access for friends after a while.

1# FreeBSD and Ubuntu: adduser and follow instructions
2$ sudo adduser

It for a bit more sophisticated setup it opionally makes sense to create a separate group for al remote git users. This will allow you to set shared group access on filesystem level on your bare repositories

 1# FreeBSD
 2$ sudo pw groupadd -n gitters
 3$ sudo pw usermod glitch -G gitters
 4
 5
 6# Ubuntu
 7$ sudo groupadd gitters
 8$ sudo usermod -a -G gitters glitch
 9# Optionally for reloading group access without logout
10$ su - glitch
11
12# Both OS
13$ sudo chown -R glitch:gitters /r

Certainly you can also finetune this setup a bit longer. For instance you might want to limit full shell access but still enable everyone to use git.

To avoid the need for a password based login, each time you interact with the remote repository you can also use personal SSH keys per user.

This would be out of scope for a 10 minutes guide. So maybe next time.

5. Checkout the remote repository from the Server

Basically that’s it. You can finally now start checking out the remote repository like this.

1$ git clone glitch@remote-host:/home/glitch/repos/a-new-repo
2
3# or of you created the shortcut
4$ git clone glitch@remote-host:/r/a-new-repo

Since you login to the remote server using SSH under hood, the you local client might ask you if the remote server is trustable. If you are safe it is, you might want to say yes. This will add the remote server to the known hosts. Everytime you interact with the remote host, it will ask you for the user password. It’s the same you use for logging into this machine via shell.

YOLO Snippet

No time or interest for learning the ropes? Fair enough. Just run the below code in bash on Ubuntu. Should work out of the box but no guarantees given of course. Good luck!

Changelog

  • [2023/10/23 1.0.0] Finished the Article