Git is a widely-used version control system that tracks changes in source code, enabling efficient collaboration among multiple developers. Hosting your own Git server provides full control over repositories, access permissions, and code sharing within your team or organization.
In this guide, you’ll learn how to install and configure a Git server on an Ubuntu 22.04 system.
Step 1: Install Git
Begin by updating your system’s package list and installing Git:
sudo apt update
sudo apt install git -y
After installation, verify the Git version:
git --version
Step 2: Create a Dedicated Git User
Create a new system user specifically for Git repository management:
sudo adduser git
Step 3: Set Up SSH Directory
To allow secure access via SSH, create a .ssh directory in the Git user’s home folder:
sudo mkdir /home/git/.ssh
sudo chown git:git /home/git/.ssh
sudo chmod 700 /home/git/.ssh
Step 4: Add a User’s SSH Public Key
To enable a user to connect to the Git server, you need to add their SSH public key to the Git user’s authorized_keys file:
sudo nano /home/git/.ssh/authorized_keys
Paste the public key into the file, then save and close it.
Set the proper permissions:
sudo chown git:git /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
Step 5: Create a Git Repository
Switch to the Git user account:
sudo su - git
Create a new bare Git repository:
git init --bare myproject.git
A bare repository is used for sharing; it doesn’t contain a working directory, making it ideal for servers.
Step 6: Connect from a Client Machine
From a client system, you can now clone the repository using:
git clone git@your-server-ip:/home/git/myproject.git
Replace your-server-ip with the actual IP address of your Git server.
Conclusion
You’ve now successfully installed and configured a Git server on Ubuntu 22.04. Hosting your own Git server enhances security, gives you complete control over your repositories, and improves collaboration within your development team.