Oct 19, 2023 5 min read

How to Setup a Git Server on Linux

Setup a Git Server on Linux with our step-by-step tutorial. The Git server can be installed on any Linux machine, including remote or local.

Setup a Git Server on Linux
Table of Contents

Introduction

Before we begin talking about how to set up a Git server, let’s briefly understand - What is Git?

Git is a popular open-source distributed version control system that can handle a number of things right from small to very large projects with great speed and efficiency. The fact that it is user-friendly and easy to learn makes it stand out.

Setting up a Git Server allows you to build private repositories without being limited by the free plans offered by other providers.

We'll show you how to set up a minimal Git server on Linux in this tutorial. If you only have a few repositories and your collaborators are tech-savvy, this solution is ideal. Otherwise, you should consider using a self-hosted git client like Gitea, Gogs, or Gitlab.

In this tutorial, you will set up a Git server on Linux. We will also address a few FAQs on how to setup a Git Server on Linux.

Advantages of Git Server

  1. Collaboration: Git servers enable easy collaboration among developers, allowing them to work on the same codebase simultaneously.
  2. Version Control: With Git servers, you can easily manage and track versions of your code, making it simple to revert changes and track progress.
  3. Backup and Restore: Git servers provide a secure and centralized location to store your code, offering protection against data loss and the ability to restore previous states.
  4. Access Control: Git servers offer robust access control mechanisms, allowing you to manage permissions and restrict access to repositories based on user roles.
  5. Remote Access: With Git servers, developers can access repositories remotely, making it convenient for distributed teams to work together regardless of their physical location.

Setting Up the Git Server

Installing Git on your server is the first step.

If you're using Debian or Ubuntu, run the following commands as sudo user to update the local package index and install git:

sudo apt update && sudo apt install git

Type the following command to install the git package on CentOS servers:

sudo yum install git

Type the following command to install the Git package on CentOS servers:

sudo useradd -r -m -U -d /home/git -s /bin/bash git

/home/git is the user's home directory. This directory will house all of the repositories. We didn't create a password for the user "git," so we'll have to rely on the ssh keys to log in.

Using the su command, change to the user "git":

sudo su - git

Run the following commands to create the SSH directory and set the correct permissions :

mkdir -p ~/.ssh && chmod 0700 ~/.ssh

Create a file called /.ssh/authorized_keys to store the SSH keys for the authorised users:

touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys

That is all there is to it. The server configuration is complete. You can now start working on your own Git repository.

To create a new empty repository, run the following command:

git init --bare ~/projectname.git

You are free to name the directory whatever you wish. The most critical step is to set up the repository in the "git" user's home directory.

Output

Initialized empty Git repository in /home/git/projectname.git/

Configure Local Git Repository

You'll need to add your local user SSH public key to the remote "git" user's authorized keys file to be able to push local git changes to the Git server.

If you already have an SSH key pair on your local system, type the following to see the public key:

cat ~/.ssh/id_rsa.pub

You will get an output like below:

Output

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDd/mnMzHwjUzK8g3ldfsfRpJuC16mhWamaXRk8ySQrD/dzpbRLfDnZsLxCzRoq+ZzFHGwcQlJergtergdHGRrO8FE5jl3IWRRp+mP12qYw== [email protected]

If you receive an error message that says "No such file or directory," it means you haven't produced an SSH key pair on your local system.

Use the following command to create a new SSH key pair:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Return to the Git server console with the output from the cat command.

Open your text editor on the server and paste the public key from your local machine into the /.ssh/authorized keys file:

sudo nano /home/git/.ssh/authorized_keys

On a single line, the complete public key text should be written.

We'll assume you already have the Git package installed on your local PC. If not, follow the steps outlined in the preceding sections to install it.

Navigate to the project directory if you already have an unversioned project. Create the project directory and go to it if you're starting from scratch:

cd /path/to/local/project

Now, initialize a git repository:

git init .

Add the git remote to your local repository as the final step:

git remote add origin git@git_server_ip:projectname.git

Replace git_server_ip with the hostname or IP address of your Git server.

Create a test file to ensure that everything is set up correctly:

touch test_file

Changes to the staging area should be made as follows:

git add .

Now, commit the changes:

git commit -m "descriptive message"

Push changes from a local repository to a remote repository:

git push -u origin master

The output should look something like this if everything is set up correctly:

Output

Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git_server_ip:projectname.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Simply copy a new collaborator's public SSH key into the /.ssh/authorized_keys file of the "git" user.

To create new repositories, follow the same processes. Before you can add the git remote to your local repository, the remote repository must already exist.

FAQs to Setup a Git Server on Linux

Which protocols can be used for remote access to a Git server on Linux?

The commonly used protocols for remote access to a Git server on Linux are SSH and HTTP(S), both of which provide secure and reliable communication.

Can I configure user authentication for my Git server on Linux?

Yes, you can configure user authentication for your Git server on Linux. SSH can use public key authentication, while HTTP can employ HTTP basic authentication or more advanced methods.

Are there any specific hardware requirements for setting up a Git server on Linux?

There are no specific hardware requirements for setting up a Git server on Linux. It can run on standard hardware and does not require high resources.

Can I host multiple repositories on a single Git server?

Yes, you can host multiple repositories on a single Git server. Each repository is stored as a separate folder or directory.

How can I ensure the security of my Git server on Linux?

You can enhance the security of your Git server on Linux by using secure protocols like SSH or HTTPS, implementing user authentication, and regularly updating the server software.

Is it possible to migrate an existing Git repository to a Git server on Linux?

Yes, you can migrate an existing Git repository to a Git server on Linux by cloning the repository onto the server or by pushing the repository from a local machine.

Are there any graphical user interface (GUI) tools available for managing a Git server on Linux?

Yes, there are several GUI tools like GitLab, Gitolite, and Gitea that provide an intuitive interface to manage and administer a Git server on Linux.

Conclusion

We hope this detailed tutorial helped you understand how to set up a Git server on Linux.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.