How to Set Up a Private Git Server on Linux

A Git server hosts a project’s repository, which contains the source code and other core files. While you can mostly rely on world-renowned Git hosting services like GitHub, in some cases it’s better to host your personal Git server for extra privacy, customizability, and security.


Let’s learn how to set up a private Git server on Linux.

Requirements for setting up a Git server

Before you start setting up your private Git server, you must have access to a spare computer or subscribe to cloud providers. This is important because you’re setting up the backup machine to act as a Git server, which you connect to from your local machine and run Git operations.

While there are no clearly defined system requirements, one gigabyte of RAM should be enough for the Git server to work. Also make sure the computer is running a Linux distribution.

Step 1: Download Git and install it on the Linux server

Needless to say, you must have Git installed on your Linux server as a preliminary step. Launch a terminal and use your Linux distribution’s package manager to install Git:

On Debian/Ubuntu derivatives:

sudo apt install git

On Arch-based distributions:

sudo pacman -S git

On CentOS/RHEL/Fedora:

sudo dnf install git

Once Git is installed on your system, continue with the next steps to configure your Linux system to host your Git repositories as a Git server.

Step 2: Set up a Git user account

Connect to your Linux server using SSH, RDP, or another remote access protocol. If you’re using a free computer as a server, turn it on and create a new user account to manage your repositories.

ssh username@address
sudo useradd git

After the new user is added, switch to them with so Command:

su git

Creating a dedicated git User account is a security protocol that ensures clients connecting to your Git server have limited visibility and access to the resources on the computer. This allows you to securely collaborate on group projects where multiple team members access your server.

Step 3: Create .ssh directory and add authorized keys

A… create .ssh Directory is necessary to store public keys and other important data that determines who gets access to this Git server. First, login to the Git user account created earlier, create the .ssh directory and restrict access to the Git user only:

ssh git@address
mkdir .ssh
chmod 700 .ssh/
touch .ssh/authorized_keys

Secure directory access permissions with the chmod command to ensure that no one can make changes to it but you. Move into the .ssh directory and create a new file “authorized_keys” with the touch Command.

cd .ssh
ssh-keygen -t rsa
cat id_rsa.pub

You need to update this file with the SSH public keys of the clients you want to give access to the Git server. Break the SSH session and open the .ssh/id_rsa.pub File on your local computer with a text editor or the cat command. This file contains your public encrypted key which, when written to the authorized_keys file, gives you access to the Git server without a password.


cd .ssh
vi authorized_keys

Copy the public key and make a new SSH connection to the Git server. Move into the .ssh open the “authorized_keys” file with a text editor and paste the public key. Save changes and exit.

From then on, you should be able to connect to the server without a password. Repeat this step for each computer that connects to the server.

Step 4: Create a directory to store all your repositories

Access the Linux server and create a directory or use a built-in directory as the root directory. Remember that this is the directory where all your repositories will be stored. This is a best practice for organizing projects more clearly.

mkdir directory_name

After creating the directory, continue to the last step in this guide to finish setting up the Git server.

Step 5: Start development by adding a new project

You are now practically done setting up the Git server. Now all you have to do is start developing by initializing repositories and adding the remote origin to your local machine. Use to switch to the parent directory CD command and create a .git Project directory:

cd parent_directory
mkdir new_project.git

Now initialize a bare git repository:

git init 

With the repository initialized, it’s time to add the remote origin on your local machine:

git remote add origin name git@address:new_project.git

That’s all you need to do on the server side. Now any authenticated client can do regular Git operations like push, pull, merge, clone and more. To start new projects, you need to repeat this step every time you create a new project.

Test its functionality by doing a git push:

touch testfile
git add testfile
git commit -m "test file"
git push name master
git clone git@address:new_project.git

Your file is successfully transferred to the remote origin. To verify that the push worked, you can clone the repository and you should find the test file in the repository.

Security tips for your Git server

Once the Git server is up and running, you need to pay close attention to its security status as it is your personal server and it is your sole responsibility to maintain it and protect it from external threats. Some of the best security practices are:

  • Disable password login
  • Change the default shell to git-shell. This prevents the logged in user from issuing non-Git commands
  • Use a custom port for SSH
  • Disable root user login
  • Back up data regularly

There are many such security configurations and security measures that you can implement on your Linux server to protect it from attackers and prevent unauthorized access.

Leave a Reply

Your email address will not be published. Required fields are marked *