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.
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 gitStep 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 gitSwitch to the new user:
su gitStep 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_keysGenerate 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_keysStep 4: Create a directory to store repositories
Choose a location that will hold all Git repositories and create it:
mkdir /srv/gitStep 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 --bareOn 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 masterClone to verify the setup:
git clone git@address:/srv/git/new_project.gitSecurity 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
