Last modified: July 10, 2024
This article is written in: 🇺🇸
Setting Up Your Own Git Server
Creating your own Git server offers increased control, enhanced security, and a tailor-made environment for your repositories. It's a great alternative to relying on services like GitHub or GitLab, especially for personal projects or within an organization. Here's an expanded guide to set up a Git server on a Debian-based system.
+-------------------+ +-------------------+
| Git Client 1 | | Git Client 2 |
| - Clone | | - Push |
| - Pull | | - Pull Requests |
| - Push | | - Merge |
+--------+----------+ +----------+--------+
| |
| |
+----+----------------------+-------+
| |
| Git Operations |
| |
+----v----------------------v----+
| |
| Git Server (Bare) |
| - Central Repository Storage |
| - Access Control |
| - Version History Management |
| - Branches & Tags Handling |
| |
+--------------------------------+
Pre-requisites
When preparing to set up the server, certain requirements and configurations are essential:
- To begin with, a Debian-based operating system should be installed on a physical or virtual server to meet the server requirements.
- It's important to ensure that an SSH server is installed and running to allow secure remote access for configuration and management.
- Administrative access is required, so having root or sudo privileges is necessary for performing the installation and configuration tasks.
Installing Git
To get Git installed on your server:
sudo apt update
sudo apt install git-core
Setting Up a Repository
I. Decide on a location for your repositories
Choose a directory where your repositories will be stored. For this guide, we are using /opt/git/
:
sudo mkdir -p /opt/git/myrepo.git
This command creates the directory structure for your repository.
II. Move to the Directory
Navigate to the newly created directory:
cd /opt/git/myrepo.git
This changes your current directory to the location where you will set up your repository.
III. Initialize as a Bare Repository
Set up the repository as a bare repository, which is suitable for sharing:
sudo git init --bare
A bare repository does not have a working directory and is used for remote repository access.
Configuring User Access
For enhanced security, it is recommended to use a dedicated git
user.
I. Create the Git User
Create a new user named git
:
sudo adduser git
This command creates a new user account named git
.
II. Assign a Password
Set a password for the git
user:
sudo passwd git
Follow the prompts to set and confirm the password.
III. Prepare SSH for the Git User
To enable SSH access for the git
user, perform the following steps:
sudo su git
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
sudo su git
switches to thegit
user.mkdir ~/.ssh
creates the.ssh
directory in thegit
user's home directory.chmod 700 ~/.ssh
sets the appropriate permissions for the.ssh
directory.touch ~/.ssh/authorized_keys
creates theauthorized_keys
file.chmod 600 ~/.ssh/authorized_keys
sets the appropriate permissions for theauthorized_keys
file.
IV. Add Authorized Users
Add the public SSH keys of users who are allowed to access the repository:
echo "public_key_content" >> ~/.ssh/authorized_keys
Replace public_key_content
with the actual public SSH key from the user's client machine. This allows the specified users to authenticate via SSH.
Using the Repository
With the server ready, users can now clone, push, and pull:
git clone git@yourserver:/opt/git/myrepo.git
Replace yourserver with your server's IP or domain name.
Additional Tips for a Robust Git Server
- Consider GitWeb or cgit for repository management via a web interface.
- Develop a strategy for regular repository backups.
- Utilize Git hooks and deploy keys for enhanced security.
- Incorporate Continuous Integration and Continuous Deployment for streamlined development processes.
- Implement monitoring and logging solutions for server performance and security.
- Keep your server and Git installation up-to-date with the latest security patches.
- Implement firewall rules and network security best practices to safeguard your server.