How to Set Up a Git Server on Ubuntu

March 12, 2025

How to Set Up a Git Server on Ubuntu
Cheap Linux Server

 


How to Set Up a Git Server on Ubuntu

Version control is essential for managing code efficiently, and hosting your own Git server can provide greater control over your repositories. In this guide, we’ll walk you through setting up a Git server on Ubuntu.

Prerequisites

  • A server running Ubuntu (20.04 or later recommended)
  • SSH access to the server
  • Basic knowledge of Linux commands

Step 1: Install Git

First, update your package list and install Git:
update your package list and install Git

sudo apt update
sudo apt install git -y

Verify the installation:
Verify the git installation

git --version

Step 2: Create a Git User

For security purposes, create a dedicated Git user:
Create a Git User

sudo adduser git

Follow the prompts to set a password and user details.

Step 3: Set Up SSH Access

To allow remote access, add your public SSH key to the Git user’s authorized keys file.

Copy your local machine’s public key:
Copy your local machine’s public key

cat ~/.ssh/id_rsa.pub

Then, on the server:

sudo mkdir -p /home/git/.ssh
sudo nano /home/git/.ssh/authorized_keys

Paste the copied key and save the file. Set proper permissions:
Paste the copied key and save the file Set proper permissions

sudo chown -R git:git /home/git/.ssh
sudo chmod 700 /home/git/.ssh
sudo chmod 600 /home/git/.ssh/authorized_keys

Step 4: Create a Bare Repository

A bare repository stores version history but does not have a working directory.
Create a Bare Repository

sudo mkdir -p /home/git/repos
sudo chown -R git:git /home/git/repos
cd /home/git/repos
git init --bare myrepo.git

Step 5: Configure Git Server Permissions

Ensure the Git user has the correct permissions:

sudo chown -R git:git /home/git/repos/myrepo.git

To prevent unwanted modifications, enforce proper permissions:

cd /home/git/repos/myrepo.git
chmod -R 755 .

Step 6: Clone the Repository

From your local machine, run:

git clone git@git-server:/home/git/repos/myrepo.git

Replace git-server with your server’s IP or hostname.

Step 7: Push Changes

Navigate to your cloned repository, create a file, and push it to the remote server:
Push Changes

cd myrepo
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin master

Conclusion

Setting up a Git server on Ubuntu provides a secure and flexible way to manage code repositories. By following these steps, you can host your own Git server and collaborate with your team efficiently.


 

How to Monitor System Performance Using htop and atop (F.A.Q)

Can I use Git without SSH?

Yes, you can configure Git to use HTTP/HTTPS with a web server like Apache or Nginx, but SSH is the most secure and commonly used method.

How do I add more users to the Git server?

Create additional users and add their SSH keys to /home/git/.ssh/authorized_keys.

How do I back up my Git repositories?

You can back up by copying the entire /home/git/repos directory to another location using rsync or scp.

 

Can I install a Git web interface?

Yes, you can install GitLab, Gitea, or GitWeb for a web-based Git interface.

Patch and Update Windows Server Safely

Patch and Update Windows Server Safely

How to Patch and Update Windows Server Safely Keeping your Windows Server environment secure and stable is a top priority for any IT administrator. Regular patching and updates are essential to protect against vulnerabilities, improve performance, and maintain...

How to Monitor Server Performance with Performance Monitor

How to Monitor Server Performance with Performance Monitor

How to Monitor Server Performance with Performance Monitor Keeping your servers healthy is crucial for ensuring smooth business operations. Windows Server comes with a powerful built-in tool called Performance Monitor (PerfMon), which allows administrators to track...

0 Comments

Submit a Comment