Operations 8 min read

Set Up a Private Git Server on Linux in 5 Simple Steps

This guide walks you through installing Git, creating a dedicated git user, configuring SSH keys, preparing a repository directory, initializing a bare repository, and applying essential security hardening to run a private Git server on a Linux machine.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Set Up a Private Git Server on Linux in 5 Simple Steps

Git is a widely used open‑source version control system. While public hosting services like GitHub are convenient, hosting your own private Git server on Linux can improve privacy, customizability, and security.

Prerequisites

You need access to a separate machine (physical or cloud) that runs a Linux distribution and has at least 1 GB of RAM.

Step 1: Install Git on the Linux server

Use the package manager of your distribution:

sudo apt install git
sudo pacman -S git
sudo dnf install git

Step 2: Create a dedicated git user account

Connect to the server via SSH (or use the console) and add a new user:

ssh username@address
sudo useradd git

Switch to the new user:

su git

Step 3: Set up the .ssh directory and authorized keys

While logged in as the git user, create the .ssh directory and restrict its permissions:

mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys

Generate an SSH key pair on the client if needed and copy the public key into .ssh/authorized_keys:

ssh-keygen -t rsa   # if you don't have id_rsa.pub
cat id_rsa.pub >> .ssh/authorized_keys

Step 4: Create a directory to store repositories

Choose a location that will hold all Git repositories and create it:

mkdir /srv/git

Step 5: Initialise a bare repository and start using it

Create a new project directory and initialise a bare repository:

cd /srv/git
mkdir new_project.git
cd new_project.git
git init --bare

On your local machine, add the remote and push an initial commit:

git remote add origin git@address:/srv/git/new_project.git
touch testfile
git add testfile
git commit -m "test file"
git push origin master

Clone to verify the setup:

git clone git@address:/srv/git/new_project.git

Security Tips for the Git Server

Disable password authentication for SSH.

Set the default shell to git-shell to restrict non‑Git commands.

Use a custom SSH port.

Disable root login.

Regularly back up repository data.

Implementing these measures helps protect your private Git server from unauthorized access and potential attacks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSecurityServer
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.