How to Set Up a Git Server on Debian 12 Bookworm
Categories:
5 minute read
Git has become the de facto standard for version control systems, enabling efficient collaboration and tracking of code changes. While platforms like GitHub, GitLab, and Bitbucket are widely used, there are scenarios where hosting your own Git server is more desirable — such as maintaining privacy, having control over data, or working within a closed network.
This article guides you through setting up a basic but functional Git server on Debian 12 Bookworm, suitable for teams or personal projects.
Why Host Your Own Git Server?
Before diving into setup, let’s briefly discuss why you might want to host your own Git server:
- Privacy & Security: Your data stays on your infrastructure.
- Customization: Tailor configurations and workflows to your needs.
- Offline Availability: Operate within local or air-gapped networks.
- Cost Savings: Avoid fees associated with private repositories.
Prerequisites
Make sure the following requirements are met:
- A system running Debian 12 Bookworm
- Root or sudo access to the system
- Git installed on your local development machine
- SSH client on your development machine
We’ll be using a headless setup — meaning, no GUI. Everything will be done via terminal.
Create a Git User
It is a good practice to create a dedicated user to manage Git repositories.
sudo adduser git
Follow the prompts to set the password and other optional details. This user will own the Git repositories and manage SSH access.
Installing Git on Debian 12
Install Git using the package manager:
sudo apt update
sudo apt install git
Verify the installation:
git --version
You should see an output similar to:
git version 2.39.2
Setting Up the Git Repository
Now, switch to the git
user and create a directory for hosting repositories:
sudo su - git
mkdir repos
cd repos
Create a bare repository — this is the standard for a central Git server:
git init --bare myproject.git
A bare repository contains no working files (no checked-out source files) and is designed only to accept pushes and allow clones.
Directory structure after this command:
repos/
└── myproject.git/
├── HEAD
├── branches
├── config
└── ...
Configuring SSH Access
To allow others (and yourself) to push and pull from the Git server over SSH, you need to configure SSH key-based authentication.
Step 1: Generate SSH Key on Client Machine
On the client machine, generate an SSH key if you haven’t already:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
By default, this creates:
~/.ssh/id_rsa (private key)
~/.ssh/id_rsa.pub (public key)
Step 2: Add Public Key to Git Server
Copy your public key to the Git server under the git
user:
ssh-copy-id git@your-server-ip
Alternatively, manually append the key:
cat ~/.ssh/id_rsa.pub | ssh git@your-server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Ensure permissions are correct on the server:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Testing Git Access via SSH
On the client machine, test access:
ssh git@your-server-ip
You should get a shell prompt as the git
user. Exit it by typing exit
.
Now try cloning the repository:
git clone git@your-server-ip:/home/git/repos/myproject.git
You now have a fully functional Git server via SSH.
Advanced Option: Setting Up Gitolite
If you want advanced access control and easier repository management, Gitolite is an excellent tool.
Step 1: Install Gitolite
sudo apt install gitolite3
During installation, it will ask for an admin SSH key. Provide the path to your public key.
Gitolite automatically sets up its admin repo, which can be cloned and edited to add users and repositories.
Step 2: Clone Admin Repo
git clone git@your-server-ip:gitolite-admin.git
From here, you manage users and repositories by editing the conf/gitolite.conf
file and committing your changes.
Optional: Setting Up a Web Interface
If you’d like a GUI to view your repositories, you can install a lightweight Git web viewer.
Option 1: GitWeb
sudo apt install gitweb apache2
Configure GitWeb to point to your Git repository directory by editing:
sudo nano /etc/gitweb.conf
Set:
$projectroot = "/home/git/repos";
Restart Apache:
sudo systemctl restart apache2
Navigate to http://your-server-ip/cgi-bin/gitweb.cgi
to view.
Option 2: Gitea (For a full-featured GitHub-like experience)
sudo apt install git wget
wget -O gitea https://dl.gitea.io/gitea/1.21.4/gitea-1.21.4-linux-amd64
chmod +x gitea
sudo mv gitea /usr/local/bin/
Create necessary directories and users, then set up Gitea as a service. Refer to the official documentation for full configuration.
Securing Your Git Server
Here are some tips to keep your Git server secure:
- Disable password SSH login: Only allow key-based access.
- Use a firewall: Restrict access to only trusted IPs.
- Set up fail2ban: Helps prevent brute-force SSH attacks.
- Keep the system updated: Regularly apply security patches.
- Monitor access logs: Keep an eye on
/var/log/auth.log
for suspicious activity.
Sample SSH security hardening:
Edit /etc/ssh/sshd_config
:
PermitRootLogin no
PasswordAuthentication no
AllowUsers git
Restart SSH:
sudo systemctl restart ssh
Conclusion
Setting up your own Git server on Debian 12 Bookworm provides flexibility, control, and security. Whether you’re managing internal development projects or experimenting with DevOps automation, a self-hosted Git setup gives you a solid foundation.
With just Git and SSH, you can build a simple and efficient version control system. Tools like Gitolite, Gitea, and GitWeb allow you to scale as needed. Most importantly, maintaining your own server helps cultivate deeper understanding and mastery over your development workflow.
Ready to go further? You might want to integrate CI/CD pipelines, add webhooks, or even host your own GitLab instance for a comprehensive platform.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.